Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.Write("Enter file name / file path > ");
            string fileName  = Console.ReadLine();
            var    chars     = new List <char>();
            var    occurance = new List <int>();

            FileCharsOcc(fileName, occurance, chars);
            var counts = new Dictionary <char, int>();

            Console.WriteLine("CHAR\tOCCURANCE");
            for (int i = 0; i < chars.Count; i++)
            {
                counts.Add(chars[i], occurance[i]);
                Console.WriteLine(chars[i] + "\t" + occurance[i]);
            }
            HuffmanTree tree = new HuffmanTree(counts);
            IDictionary <char, string> encodings = tree.CreateEncodings();

            var chars_new      = new List <char>();
            var chars_new_size = new List <int>();

            Console.WriteLine("CHAR\tCODE");
            foreach (KeyValuePair <char, string> k in encodings)
            {
                Console.WriteLine((k.Key == '\n' ? "EOF" : k.Key.ToString()) + ":\t" + k.Value);
                chars_new.Add(k.Key);
                chars_new_size.Add(k.Value.Length);
            }
            int formattedFileSize = 0;

            for (int i = 0; i < chars.Count; i++)
            {
                for (int j = 0; j < chars_new.Count; j++)
                {
                    if (chars[i] == chars_new[j])
                    {
                        formattedFileSize += occurance[i] * chars_new_size[j];
                        break;
                    }
                }
            }
            double fileSize = FileSize(fileName);

            fileSize *= Math.Sqrt(counts.Count);
            Console.WriteLine("File " + fileName + " without compression : " + fileSize + " Bits");
            Console.WriteLine("File " + fileName + " with compression    : " + formattedFileSize + " Bits");
        }