예제 #1
0
        static void Main(string[] args)
        {
            string      input       = "h shdh s dsh dhsh dalsdasdk a";
            HuffmanTree huffmanTree = new HuffmanTree();


            huffmanTree.Build(input);


            BitArray encoded = huffmanTree.Encode(input);

            Console.Write("Encoded: ");
            foreach (bool bit in encoded)
            {
                Console.Write((bit ? 1 : 0) + "");
            }
            Console.WriteLine();


            string decoded = huffmanTree.Decode(encoded);

            Console.WriteLine("Decoded: " + decoded);

            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.Write("Enter Text․");
            string      input       = Console.ReadLine();
            HuffmanTree huffmanTree = new HuffmanTree();

            huffmanTree.Build(input);

            BitArray encoded = huffmanTree.Encode(input);

            Console.Write("Encoded Text․ ");
            foreach (bool bit in encoded)
            {
                Console.Write((bit ? 1 : 0) + "");
            }
            Console.WriteLine();
            Console.WriteLine();

            //string decoded = huffmanTree.Decode(encoded);
            foreach (var item in huffmanTree.Frequencies)
            {
                Console.Write(item.Key + "  " + item.Value + "  ");
                List <bool> key = huffmanTree.Root.Traverse(item.Key, new List <bool>());
                foreach (var bit in key)
                {
                    Console.Write((bit ? 1 : 0));
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
예제 #3
0
        private void btnZakoduj_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text != string.Empty)
            {
                var saveFile = new SaveFileDialog();
                saveFile.ShowDialog();
                var        fileName = saveFile.FileName;
                FileStream fs       = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                try
                {
                    var         sw          = new StreamWriter(fs);
                    HuffmanTree huffmanTree = new HuffmanTree();

                    // Build the Huffman tree
                    huffmanTree.Build(richTextBox1.Text);

                    // Encode
                    BitArray encoded = huffmanTree.Encode(richTextBox1.Text);

                    string encodedS = "";
                    foreach (bool bit in encoded)
                    {
                        encodedS += (bit ? 1 : 0) + "";
                    }
                    Node.setCodeToTheTree("", huffmanTree.Root);
                    // Writing symbols
                    huffmanTree.PrintfLeafAndCodes(huffmanTree.Root);
                    string symbols       = "";
                    var    bity_slownika = 0;
                    foreach (var node in huffmanTree.nodesToWrite)
                    {
                        symbols       += node.Symbol + node.Code + ";";
                        bity_slownika += 8 + node.Code.Length;
                    }

                    sw.WriteLine(symbols);
                    sw.WriteLine(encodedS);

                    var wynikForm = new Wynik_Kodowania_form(richTextBox1.Text, encodedS, symbols, huffmanTree.nodesToWrite, bity_slownika);
                    //  var zajetosc = sizeof(char) * txtBoxHaslo.Text.Length * 8;// bo sizeof podaje w bajtach a chcemy bity
                    //   MessageBox.Show("Nie zakodowany tekst zajmuje "+zajetosc.ToString()+" bitów");
                    wynikForm.ShowDialog();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                MessageBox.Show("Wpisz hasło do zakodowania");
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            string textFromFile;

            using (FileStream fstream = File.OpenRead(@"../../text.txt"))
            {
                byte[] array = new byte[fstream.Length];
                fstream.Read(array, 0, array.Length);
                textFromFile = Encoding.Default.GetString(array);
            }

            string      input       = textFromFile;
            HuffmanTree huffmanTree = new HuffmanTree();

            huffmanTree.Build(input);

            BitArray encoded = huffmanTree.Encode(input);

            byte[] bytes = new byte[encoded.Length / 8 + (encoded.Length % 8 == 0 ? 0 : 1)];
            encoded.CopyTo(bytes, 0);
            File.WriteAllBytes(@"../../encoded.bin", bytes);

            /*
             * Console.Write("Encoded: ");
             * foreach (bool bit in encoded)
             * {
             *  Console.Write((bit ? 1 : 0) + "");
             * }
             * Console.WriteLine();
             */

            string decoded = huffmanTree.Decode(encoded);

            using (FileStream fstream = new FileStream(@"../../decoded.txt", FileMode.OpenOrCreate))
            {
                byte[] array = Encoding.Default.GetBytes(decoded);
                fstream.Write(array, 0, array.Length);
            }

            /*
             * Console.WriteLine("Decoded: " + decoded);
             * Console.ReadLine();
             */
        }
예제 #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            string          s    = textBox1.Text;
            int             n    = s.Length;
            List <CharFreq> list = new List <CharFreq>();

            textBox2.Text = string.Empty;

            for (int i = 0; i < n; i++)
            {
                bool     found = false;
                char     c     = s[i];
                CharFreq cf    = new CharFreq();

                for (int j = 0; !found && j < list.Count; j++)
                {
                    if (c == list[j].ch)
                    {
                        found   = true;
                        cf.ch   = c;
                        cf.freq = 1 + list[j].freq;
                        list.RemoveAt(j);
                        list.Add(cf);
                    }
                }

                if (!found)
                {
                    cf.ch   = c;
                    cf.freq = 1;
                    list.Add(cf);
                }
            }

            HuffmanTree ht = new HuffmanTree();
            BinaryTreeNode <CharFreq> root = ht.Build(list, list.Count);

            InorderTraversal(root);
            textBox2.Text += "\r\nKarakterler = " + n.ToString() + "\r\n";
            textBox2.Text += "Düğümler = " + leafNodes.ToString() + "\r\n";
            textBox2.Text += "Sıkıştırma oranı %" +
                             (100.0 - 100.0 * ((double)leafNodes) / n).ToString("F2") + "\r\n";
        }