コード例 #1
0
        static void Main(string[] args)
        {
            //Encoding enc=Encoding.GetEncoding(1251);
            string      input       = System.IO.File.ReadAllText(@"D:\Epam\goncharov.txt", Encoding.Default);
            HuffmanTree huffmanTree = new HuffmanTree();

            // Build the Huffman tree
            huffmanTree.Build(input);

            // Encode

            Console.Write("Encoded1: ");
            BitArray encoded = huffmanTree.Encode(input);

            Console.Write("Encoded2: ");
            string dec = "";

            Console.WriteLine(encoded.Length);
            int i = 0;

            foreach (bool bit in encoded)
            {
                dec += (bit ? (char)1 : (char)0);
                //  Console.Write(i++);
                //    Console.Write((bit ? 1 : 0) + "");
            }
            System.IO.File.WriteAllText(@"D:\Epam\code.txt", dec);
            Console.WriteLine();
            Console.Write("Encoded3: ");
            // Decode
            string decoded = huffmanTree.Decode(encoded);

            Console.Write("Encoded4: ");
            System.IO.File.WriteAllText(@"D:\Epam\result.txt", decoded, Encoding.Default);
            Console.WriteLine("Decoded: " + decoded);

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Inserir o texto a ser compressado:");
            string      input       = Console.ReadLine();
            HuffmanTree huffmanTree = new HuffmanTree();

            huffmanTree.Build(input);

            BitArray encoded = huffmanTree.Encode(input);

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

            string decoded = huffmanTree.Decode(encoded);

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

            Console.ReadLine();
        }