コード例 #1
0
        public int num_of_char;//ترتيبه

        static public nodes create_new_node(nodes n1, nodes n2, int periority, int last_num_of_char)
        {
            nodes new_node = new nodes();

            new_node.str         = n1.str + n2.str;
            new_node.left        = n1.str;
            new_node.right       = n2.str;
            new_node.count       = n1.count + n2.count;
            new_node.periroity   = periority;
            new_node.check       = 0;
            new_node.num_of_char = last_num_of_char;
            return(new_node);
        }
コード例 #2
0
        private void btn_decode_Click(object sender, EventArgs e)
        {
            decode_txt.Text = "";
            get_code();
            string input_code;
            int    tr;

            string[] arr1 = { "0", "01", "001", "0001", "00001", "000001", "0000001" }; //char left side
            string[] arr2 = { "1", "11", "101", "1001", "10001", "100001", "1000001" }; //char right side

            string[] arr3 = { "0", "00", "000", "0000", "00000", "000000" };            // new of left side
            string[] arr4 = { "0", "10", "100", "1000", "10000", "100000" };            //new of right side

            List <nodes> new_node  = new List <nodes>();
            nodes        node_temp = new nodes();
            nodes        node_swap = new nodes();
            char         input_char;
            bool         check_new_char = false;
            int          index;
            int          temp;
            int          temp_index = 0;
            int          temp_level;

            #region First_code
            //Fetch the First Character
            get_code();
            tr = listBox2.FindString(input);
            listBox1.SelectedIndex = tr;
            input_code             = listBox1.SelectedItem.ToString();
            decode_txt.Text       += input_code;

            node_temp.str   = Convert.ToChar(input_code);
            node_temp.count = 1;
            node_temp.pos   = "right";
            node_temp.code  = "1";
            node_temp.level = 0;

            // fetch code from initial table
            index = listBox1.FindString(input_code);
            listBox2.SelectedIndex = index;
            prev_symbole_code      = listBox2.SelectedItem.ToString();

            // Update Initial table for symbole
            listBox2.Items[index] = node_temp.code;

            // fetch previous new code
            index = listBox1.FindString("new");
            listBox2.SelectedIndex = index;
            prev_new_code          = listBox2.SelectedItem.ToString();

            //output Code in output table

            /*list_symbole_result.Items.Add("new");
             * list_code_result.Items.Add(prev_new_code);
             * list_symbole_result.Items.Add(input[0].ToString());
             * list_code_result.Items.Add(prev_symbole_code);*/

            // Update Initial table for new
            listBox2.Items[index] = "0";

            //new_node.Add(node_temp);
            new_node.Add(new nodes()
            {
                str = node_temp.str, pos = node_temp.pos, code = node_temp.code, count = node_temp.count, level = node_temp.level
            });

            //End
            #endregion


            cc++;                        // for new code select
            for (int i = 1; i < cv; i++) // -1
            {
                check_new_char = true;

                // Take Character from input
                get_code();
                tr = listBox2.FindString(input);
                listBox1.SelectedIndex = tr;
                input_code             = listBox1.SelectedItem.ToString();
                decode_txt.Text       += input_code.ToString();


                // Check if it is new char or old
                for (int j = 0; j < new_node.Count; j++)
                {
                    // is existing char
                    if (new_node[j].str == Convert.ToChar(input_code))
                    {
                        new_node[j].count++;

                        // Output Symbole Code
                        // fetch code from initial table
                        index = listBox1.FindString(input_code);
                        listBox2.SelectedIndex = index;
                        prev_symbole_code      = listBox2.SelectedItem.ToString();

                        check_new_char = false;
                        break;
                    }
                }

                // Is new char
                if (check_new_char == true)
                {
                    node_temp.str   = Convert.ToChar(input_code);
                    node_temp.count = 1;
                    node_temp.pos   = "left";//****

                    if (new_pos == "left")
                    {
                        nodes obj = new_node[new_node.Count - 1]; // last node
                        node_temp.level = obj.level + 1;          // insert new level for new char
                        temp            = obj.level + 1;
                        node_temp.code  = arr1[node_temp.level];

                        //new_node.Add(node_temp);
                        new_node.Add(new nodes()
                        {
                            str = node_temp.str, pos = node_temp.pos, code = node_temp.code, count = node_temp.count, level = node_temp.level
                        });

                        //rich_output_txt.Text += new_node[new_node.Count - 1].str; // show save char

                        // fetch code from initial table
                        index = listBox1.FindString(input_code);
                        listBox2.SelectedIndex = index;
                        prev_symbole_code      = listBox2.SelectedItem.ToString();

                        // fetch previous new code
                        index = listBox1.FindString("new");
                        listBox2.SelectedIndex = index;
                        prev_new_code          = listBox2.SelectedItem.ToString();

                        //output Code in output table

                        /*list_symbole_result.Items.Add("new");
                         * list_code_result.Items.Add(prev_new_code);
                         * list_symbole_result.Items.Add(input.Substring(i, 1));
                         * list_code_result.Items.Add(prev_symbole_code);*/


                        //Check for Conditions (Update tree)
                        // only condition of checking the left and right tree counts



                        //break;
                    }
                }


                #region Update_Tree
                // pos, level, count

                if (new_node.Count >= 2 && new_node[new_node.Count - 1].count > new_node[new_node.Count - 2].count)
                {
                    temp_level = new_node[new_node.Count - 2].level;
                    int d = new_node.Count - 2;
                    // to obtain who object will swap with current "Far object"
                    for (int n = 0; n < new_node.Count - 1; n++)
                    {
                        if (new_node[new_node.Count - 1].count > new_node[n].count)
                        {
                            //MessageBox.Show("Here!!");
                            temp_level = new_node[n].level;
                            d          = n;
                            break;
                        }
                    }

                    //rich_output_txt.Text += "ok";
                    node_swap.str   = new_node[new_node.Count - 1].str;                                                                                                //c
                    node_swap.count = new_node[new_node.Count - 1].count;                                                                                              //2
                    new_node[new_node.Count - 1].str   = new_node[d].str;                                                                                              //d
                    new_node[new_node.Count - 1].count = new_node[d].count;                                                                                            //1
                    new_node[d].str   = node_swap.str;                                                                                                                 //c
                    new_node[d].count = node_swap.count;                                                                                                               //2

                    MessageBox.Show(new_node[new_node.Count - 1].str.ToString() + " " + new_node[new_node.Count - 1].code + " " + new_node[new_node.Count - 1].count); //c
                    MessageBox.Show(new_node[d].str.ToString() + " " + new_node[d].code + " " + new_node[d].count);                                                    //d

                    // Condition of that if right tree not >= left tree
                    if (new_node[0].pos == "right")
                    {
                        int count_sum = 0;
                        for (int m = 1; m < new_node.Count; m++)
                        {
                            count_sum += new_node[m].count;
                        }

                        if (new_node[0].count < count_sum)
                        {
                            new_node[0].pos  = "left";
                            new_node[0].code = arr1[0];
                            MessageBox.Show(new_node[0].str.ToString() + " " + new_node[0].code);
                            for (int v = 1; v < new_node.Count; v++)
                            {
                                new_node[v].pos  = "right";
                                new_node[v].code = arr2[v];
                                MessageBox.Show(new_node[v].str.ToString() + " " + new_node[v].code);
                            }
                        }
                    }
                }



                // Update Initial table for symbole or all symbols

                for (int h = 0; h < new_node.Count; h++)
                {
                    // fetch code from initial table
                    index = listBox1.FindString(new_node[h].str.ToString());
                    listBox2.SelectedIndex = index;

                    // Update Initial table for symbole
                    listBox2.Items[index] = new_node[h].code;
                }
                #endregion


                int mm;

                // Update Initial table for new
                if (new_node[0].pos == "right")
                {
                    mm = new_node[new_node.Count - 1].level;

                    listBox2.Items[0] = arr3[mm];
                    prev_new_code     = arr3[mm];
                    temp_index++;
                }
                else
                {
                    mm = new_node[new_node.Count - 1].level;
                    listBox2.Items[0] = arr4[mm];
                    prev_new_code     = arr4[mm];
                    temp_index++;
                }

                cc++;
            }
        }
コード例 #3
0
        private void btn_submit_Click(object sender, EventArgs e)
        {
            if (txt_input.Text != "")
            {
                txt_input.ReadOnly   = true;
                txt_symbole.ReadOnly = true;
                txt_code.ReadOnly    = true;
                btn_submit.Visible   = false;
                input = txt_input.Text;
                string[] arr1 = { "0", "01", "001", "0001", "00001", "000001", "0000001" }; //char left side
                string[] arr2 = { "1", "11", "101", "1001", "10001", "100001", "1000001" }; //char right side

                string[] arr3 = { "0", "00", "000", "0000", "00000", "000000" };            // new of left side
                string[] arr4 = { "0", "10", "100", "1000", "10000", "100000" };            //new of right side

                List <nodes> new_node = new List <nodes>();
                //nodes[] node_temp = new nodes[input.Length];
                nodes node_temp = new nodes();
                nodes node_swap = new nodes();
                char  input_char;
                bool  check_new_char = false;
                int   index;
                int   temp;
                int   temp_index = 0;
                int   temp_level;

                #region First_char
                //Fetch the First Character
                node_temp.str   = input[0];//Convert.ToChar(input.Substring(1, 1))
                node_temp.count = 1;
                node_temp.pos   = "right";
                node_temp.code  = "1";
                node_temp.level = 0;

                // fetch code from initial table
                index = list_symbole.FindString(input[0].ToString());
                list_code.SelectedIndex = index;
                prev_symbole_code       = list_code.SelectedItem.ToString();

                // Update Initial table for symbole
                list_code.Items[index] = node_temp.code;

                // fetch previous new code
                index = list_symbole.FindString("new");
                list_code.SelectedIndex = index;
                prev_new_code           = list_code.SelectedItem.ToString();

                //output Code in output table
                list_symbole_result.Items.Add("new");
                list_code_result.Items.Add(prev_new_code);
                list_symbole_result.Items.Add(input[0].ToString());
                list_code_result.Items.Add(prev_symbole_code);

                // Update Initial table for new
                list_code.Items[index] = "0";

                //new_node.Add(node_temp);
                new_node.Add(new nodes()
                {
                    str = node_temp.str, pos = node_temp.pos, code = node_temp.code, count = node_temp.count, level = node_temp.level
                });

                //End
                #endregion
                //MessageBox.Show(input[0].ToString());
                list_output.Items.Add(input[0] + " New");

                for (int i = 1; i < input.Length; i++)  // -1
                {
                    check_new_char = true;

                    // Take Character from input
                    input_char = Convert.ToChar(input.Substring(i, 1));
                    //rich_output_txt.Text += input_char;

                    // Check if it is new char or old
                    //foreach (nodes item in new_node)//.Where(x => x.str == input_char).Take(1).ToList()
                    for (int j = 0; j < new_node.Count; j++)
                    {
                        // is existing char
                        if (new_node[j].str == input_char)
                        {
                            list_output.Items.Add(input_char + " Exist");

                            new_node[j].count++;

                            // Output Symbole Code
                            // fetch code from initial table
                            index = list_symbole.FindString(input.Substring(i, 1));
                            list_code.SelectedIndex = index;
                            prev_symbole_code       = list_code.SelectedItem.ToString();

                            list_symbole_result.Items.Add(input.Substring(i, 1));
                            list_code_result.Items.Add(prev_symbole_code);

                            #region try
                            //Check for Conditions (Update tree)
                            // all condition must detect

                            /*#region Update_Tree
                             * // pos, level, count
                             *
                             * if (new_node.Count>=2 && new_node[new_node.Count - 1].count > new_node[new_node.Count - 2].count)
                             * {
                             *  temp_level = new_node[new_node.Count - 2].level;
                             *  int d = new_node.Count - 2;
                             *  // to obtain who object will swap with current "Far object"
                             *  for (int n = 0; n < new_node.Count -1; n++)
                             *  {
                             *      if (new_node[new_node.Count - 1].count > new_node[n].count)
                             *      {
                             *          //MessageBox.Show("Here!!");
                             *          temp_level = new_node[n].level;
                             *          d = n;
                             *          break;
                             *      }
                             *  }
                             *
                             *  //rich_output_txt.Text += "ok";
                             *  node_swap.str = new_node[new_node.Count - 1].str;//c
                             *  node_swap.count = new_node[new_node.Count - 1].count;//2
                             *  new_node[new_node.Count - 1].str = new_node[d].str;//d
                             *  new_node[new_node.Count - 1].count = new_node[d].count;//1
                             *  new_node[d].str = node_swap.str;//c
                             *  new_node[d].count = node_swap.count;//2
                             *
                             *  MessageBox.Show(new_node[new_node.Count - 1].str.ToString() + " " + new_node[new_node.Count - 1].code + " " + new_node[new_node.Count - 1].count);//c
                             *  MessageBox.Show(new_node[d].str.ToString() + " " + new_node[d].code + " " + new_node[d].count);//d
                             *
                             *  // Condition of that if right tree not >= left tree
                             *  if (new_node[0].pos == "right")
                             *  {
                             *      int count_sum = 0;
                             *      for (int m = 1; m < new_node.Count; m++)
                             *      {
                             *          count_sum += new_node[m].count;
                             *      }
                             *
                             *      if (new_node[0].count < count_sum)
                             *      {
                             *          new_node[0].pos = "left";
                             *          new_node[0].code = arr1[0];
                             *          MessageBox.Show(new_node[0].str.ToString() + " " + new_node[0].code);
                             *          for (int v = 1; v < new_node.Count; v++)
                             *          {
                             *              new_node[v].pos = "right";
                             *              new_node[v].code = arr2[v];
                             *              MessageBox.Show(new_node[v].str.ToString() + " " + new_node[v].code);
                             *          }
                             *      }
                             *  }
                             *
                             * }
                             *
                             *
                             *
                             *
                             * // Update Initial table for symbole or all symbols
                             *
                             * for (int h = 0; h < new_node.Count; h++)
                             * {
                             *  // fetch code from initial table
                             *  index = list_symbole.FindString(new_node[h].str.ToString());
                             *  list_code.SelectedIndex = index;
                             *
                             *  // Update Initial table for symbole
                             *  list_code.Items[index] = new_node[h].code;
                             * }
                             #endregion*/
                            #endregion

                            check_new_char = false;
                            break;
                        }
                    }

                    // Is new char
                    if (check_new_char == true)
                    {
                        list_output.Items.Add(input_char + " New");

                        node_temp.str   = input_char;
                        node_temp.count = 1;
                        node_temp.pos   = "left";//****

                        if (new_pos == "left")
                        {
                            nodes obj = new_node[new_node.Count - 1]; // last node
                            node_temp.level = obj.level + 1;          // insert new level for new char
                            temp            = obj.level + 1;
                            node_temp.code  = arr1[node_temp.level];

                            //new_node.Add(node_temp);
                            new_node.Add(new nodes()
                            {
                                str = node_temp.str, pos = node_temp.pos, code = node_temp.code, count = node_temp.count, level = node_temp.level
                            });

                            //rich_output_txt.Text += new_node[new_node.Count - 1].str; // show save char

                            // fetch code from initial table
                            index = list_symbole.FindString(input.Substring(i, 1));
                            list_code.SelectedIndex = index;
                            prev_symbole_code       = list_code.SelectedItem.ToString();

                            // fetch previous new code
                            index = list_symbole.FindString("new");
                            list_code.SelectedIndex = index;
                            prev_new_code           = list_code.SelectedItem.ToString();

                            //output Code in output table
                            list_symbole_result.Items.Add("new");
                            list_code_result.Items.Add(prev_new_code);
                            list_symbole_result.Items.Add(input.Substring(i, 1));
                            list_code_result.Items.Add(prev_symbole_code);


                            //Check for Conditions (Update tree)
                            // only condition of checking the left and right tree counts



                            //break;
                        }

                        /*else // new on right
                         * {
                         *  nodes obj = new_node[new_node.Count - 1]; // last node
                         *  node_temp.level = obj.level + 1;  // insert new level for new char
                         *  temp = obj.level + 1;
                         *  node_temp.code = arr2[node_temp.level];
                         *
                         *  //new_node.Add(node_temp);
                         *  new_node.Add(new nodes() { str = node_temp.str, pos = node_temp.pos, code = node_temp.code, count = node_temp.count, level = node_temp.level });
                         *
                         *  // fetch code from initial table
                         *  index = list_symbole.FindString(input.Substring(i, 1));
                         *  list_code.SelectedIndex = index;
                         *  prev_symbole_code = list_code.SelectedItem.ToString();
                         *
                         *  // fetch previous new code
                         *  index = list_symbole.FindString("new");
                         *  list_code.SelectedIndex = index;
                         *  prev_new_code = list_code.SelectedItem.ToString();
                         *
                         *  //output Code in output table
                         *  list_symbole_result.Items.Add("new");
                         *  list_code_result.Items.Add(prev_new_code);
                         *  list_symbole_result.Items.Add(input.Substring(i, 1));
                         *  list_code_result.Items.Add(prev_symbole_code);
                         *
                         *  //Check for Conditions (Update tree)
                         *
                         *
                         *  // Update Initial table for symbole
                         *  //list_code.Items[index] = node_temp.code;
                         *
                         *  // Update Initial table for new
                         *  //list_code.Items[index] = arr4[temp];
                         *
                         *  //break;
                         * }*/
                    }


                    #region Update_Tree
                    // pos, level, count

                    if (new_node.Count >= 2 && new_node[new_node.Count - 1].count > new_node[new_node.Count - 2].count)
                    {
                        temp_level = new_node[new_node.Count - 2].level;
                        int d = new_node.Count - 2;
                        // to obtain who object will swap with current "Far object"
                        for (int n = 0; n < new_node.Count - 1; n++)
                        {
                            if (new_node[new_node.Count - 1].count > new_node[n].count)
                            {
                                //MessageBox.Show("Here!!");
                                temp_level = new_node[n].level;
                                d          = n;
                                break;
                            }
                        }

                        //rich_output_txt.Text += "ok";
                        node_swap.str   = new_node[new_node.Count - 1].str;                                                                                                //c
                        node_swap.count = new_node[new_node.Count - 1].count;                                                                                              //2
                        new_node[new_node.Count - 1].str   = new_node[d].str;                                                                                              //d
                        new_node[new_node.Count - 1].count = new_node[d].count;                                                                                            //1
                        new_node[d].str   = node_swap.str;                                                                                                                 //c
                        new_node[d].count = node_swap.count;                                                                                                               //2

                        MessageBox.Show(new_node[new_node.Count - 1].str.ToString() + " " + new_node[new_node.Count - 1].code + " " + new_node[new_node.Count - 1].count); //c
                        MessageBox.Show(new_node[d].str.ToString() + " " + new_node[d].code + " " + new_node[d].count);                                                    //d

                        // Condition of that if right tree not >= left tree
                        if (new_node[0].pos == "right")
                        {
                            int count_sum = 0;
                            for (int m = 1; m < new_node.Count; m++)
                            {
                                count_sum += new_node[m].count;
                            }

                            if (new_node[0].count < count_sum)
                            {
                                new_node[0].pos  = "left";
                                new_node[0].code = arr1[0];
                                MessageBox.Show(new_node[0].str.ToString() + " " + new_node[0].code);
                                for (int v = 1; v < new_node.Count; v++)
                                {
                                    new_node[v].pos  = "right";
                                    new_node[v].code = arr2[v];
                                    MessageBox.Show(new_node[v].str.ToString() + " " + new_node[v].code);
                                }
                            }
                        }
                    }



                    // Update Initial table for symbole or all symbols

                    for (int h = 0; h < new_node.Count; h++)
                    {
                        // fetch code from initial table
                        index = list_symbole.FindString(new_node[h].str.ToString());
                        list_code.SelectedIndex = index;

                        // Update Initial table for symbole
                        list_code.Items[index] = new_node[h].code;
                    }
                    #endregion


                    int mm;

                    // Update Initial table for new
                    if (new_node[0].pos == "right")
                    {
                        mm = new_node[new_node.Count - 1].level;

                        list_code.Items[0] = arr3[mm];
                        prev_new_code      = arr3[mm];
                        temp_index++;
                    }
                    else
                    {
                        mm = new_node[new_node.Count - 1].level;
                        list_code.Items[0] = arr4[mm];
                        prev_new_code      = arr4[mm];
                        temp_index++;
                    }
                }//end for loop

                //rich_output_txt.Text = "";
                for (int i = 0; i < new_node.Count; i++)
                {
                    rich_output_txt.Text += new_node[i].str + "\t";
                }
            }// end if

            ///////////// for decoding /////////////////
            cv = list_code_result.Items.Count;
        }
コード例 #4
0
        private void btn_encode_Click(object sender, EventArgs e)
        {
            string sentence          = txt_encode.Text;
            string sentence_distinct = new String(sentence.Distinct().ToArray());

            int nodes_num        = sentence_distinct.Length - 1;
            int last_num_of_char = 0;

            //convert sentence into nodes
            for (int i = 0; i < sentence_distinct.Length; i++)
            {
                nodes node = new nodes();
                node.periroity = 0;
                node.str       = sentence_distinct.Substring(i, 1);
                node.left      = null;
                node.right     = null;
                int cc = sentence.Count(f => f == Convert.ToChar(sentence_distinct.Substring(i, 1)));
                node.count       = cc;
                node.check       = 0;
                node.num_of_char = i + 1;
                last_num_of_char = i + 1;
                nodes_list.Add(node);
            }

            //sort list
            nodes_list = nodes_list.OrderBy(x => x.count).ToList();

            //create new nodes
            #region create new nodes
            for (int i = 0; i < sentence_distinct.Length - 1; i++)
            {
                //select first 2 elements that not checked
                List <nodes> new_node = new List <nodes>();
                //new_node = nodes_list.Where(x=> x.check==0).Take(2).ToList();
                nodes node1             = new nodes();
                nodes node2             = new nodes();
                nodes new_node_genrated = new nodes();


                foreach (var item in nodes_list.Where(x => x.check == 0).Take(1).ToList())
                {
                    node1.str         = item.str;
                    node1.left        = item.left;
                    node1.right       = item.right;
                    node1.count       = item.count;
                    node1.periroity   = item.periroity;
                    node1.check       = 1;
                    item.check        = 1;
                    node1.num_of_char = item.num_of_char;
                }

                foreach (var item in nodes_list.Where(x => x.check == 0).Take(1).ToList())
                {
                    node2.str         = item.str;
                    node2.left        = item.left;
                    node2.right       = item.right;
                    node2.count       = item.count;
                    node2.periroity   = item.periroity;
                    node2.check       = 1;
                    item.check        = 1;
                    node2.num_of_char = item.num_of_char;
                }

                if (node1.num_of_char < node2.num_of_char)
                {
                    MessageBox.Show(node1.num_of_char.ToString() + "<" + node2.num_of_char.ToString() + " case1");
                    new_node_genrated = nodes.create_new_node(node1, node2, i + 1, node2.num_of_char);
                }
                else
                {
                    MessageBox.Show(node1.num_of_char.ToString() + ">" + node2.num_of_char.ToString() + " case2");
                    new_node_genrated = nodes.create_new_node(node2, node1, i + 1, node1.num_of_char);
                }
                nodes_list.Add(new_node_genrated);

                //sort array
                nodes_list = nodes_list.OrderBy(x => x.count).ToList();



                screen_text.Text += "##############################New Level#############################" + "\n";

                foreach (var item in nodes_list)
                {
                    screen_text.Text += (item.str + " LEFT: " + item.left + " RIGHT: " + item.right + " COUNT: " + item.count + " PERIORITY: " + item.periroity + " CHECK: " + item.check + " Num OF CHAR: " + item.num_of_char + "\n");
                    screen_text.Text += ("__________________________________________________________________" + "\n");
                }
            }
            #endregion

            //extract the code

            List <nodes> primary_nodes = new List <nodes>();
            primary_nodes = nodes_list.Where(x => x.periroity == 0).ToList();

            for (int i = 0; i < sentence_distinct.Length; i++)
            {
                string code = "";
                string str  = primary_nodes.First().str;
                //string str="a";
                List <nodes> searched_for_str = nodes_list.Where(x => x.str.Contains(str)).ToList();
                searched_for_str.OrderBy(x => x.periroity);

                foreach (var item in searched_for_str)
                {
                    if (str == item.left)
                    {
                        code += "0";
                        str   = item.str;
                    }
                    if (str == item.right)
                    {
                        code += "1";
                        str   = item.str;
                    }
                }

                screen_text.Text += ("Code of " + primary_nodes.First().str + " is= " + Reverse(code) + "\n");
                primary_nodes.RemoveAt(0);
            }
        }