public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            CharacterFrequency cf = obj as CharacterFrequency;

            if (obj != null)
            {
                int result = 0;
                if (this.m_count > cf.m_count)
                {
                    result = -1;
                }
                else if (this.m_count == cf.m_count)
                {
                    result = 0;
                }
                else
                {
                    result = 1;
                }

                return(result);
            }
            else
            {
                throw new ArgumentException("object is not a Character Frequency");
            }
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj == this)
            {
                return(true);
            }

            if (!(obj.GetType() == GetType()))
            {
                return(false);
            }

            bool equal            = false;
            CharacterFrequency cf = new CharacterFrequency('\0', 0);

            cf = (CharacterFrequency)obj;

            if (this.character == cf.character)
            {
                equal = true;
            }

            return(equal);
        }
Beispiel #3
0
        static LinkedList <CharacterFrequency> Charactercounting(String Filepath, LinkedList <CharacterFrequency> cflist)
        {
            StreamReader sr   = new StreamReader(Filepath);
            int          text = 0;

            text = sr.Read();
            if (text == -1)
            {
                sr.Close();
                return(cflist);
            }
            else
            {
                //Reading the File
                while (text != -1)
                {
                    CharacterFrequency cf = new CharacterFrequency((char)text, 1);
                    LinkedListNode <CharacterFrequency> node = cflist.Find(cf);

                    if (node != null)
                    {
                        node.Value.increment();
                    }
                    else
                    {
                        cflist.AddFirst(cf);
                    }

                    text = sr.Read();
                }
                sr.Close();
                //Ordering the Linked List
                CharacterFrequency[] cfa = cflist.ToArray();
                Array.Sort(cfa);

                cflist.Clear();

                for (int i = 0; i < cfa.Length; i++)
                {
                    cflist.AddFirst(cfa[i]);
                }
                //Debugging
                //LinkedListNode<CharacterFrequency> snode = cflist.First;
                //while (snode != null)
                //{
                //    Console.WriteLine(snode.Value.ToString());
                //    snode = snode.Next;
                //}
                //End of debugging
                return(cflist);
            }
        }
Beispiel #4
0
        static LinkedList <BinaryTree <CharacterFrequency> > buildTree(LinkedList <CharacterFrequency> cflist, LinkedList <BinaryTree <CharacterFrequency> > tree)
        {
            LinkedListNode <CharacterFrequency> node = cflist.First;

            //Turn all the CharacterFrequency's into BinaryTree roots and add them to a LinkedList of Binarytrees of Characterfrequencys
            while (node != null)
            {
                BinaryTree <CharacterFrequency> bt = new BinaryTree <CharacterFrequency>();
                bt.Insert(node.Value, BinaryTree <CharacterFrequency> .Relative.root);
                tree.AddLast(bt);
                node = node.Next;
            }
            BinaryTree <CharacterFrequency> bt1 = new BinaryTree <CharacterFrequency>();
            BinaryTree <CharacterFrequency> bt2 = new BinaryTree <CharacterFrequency>();

            //Creating the Binary Tree
            while (tree.Count > 1)
            {
                //Remove first two elements
                bt1 = tree.First();
                tree.RemoveFirst();
                bt2 = tree.First();
                tree.RemoveFirst();
                //Sums the two elements and creates a new node
                int sum = bt1.Current.Data.count + bt2.Current.Data.count;
                CharacterFrequency cf = new CharacterFrequency('\0', sum);
                BinaryTree <CharacterFrequency> ptrBT_new = new BinaryTree <CharacterFrequency>();

                ptrBT_new.Insert(cf, BinaryTree <CharacterFrequency> .Relative.root);
                ptrBT_new.Insert(bt1.Root, BinaryTree <CharacterFrequency> .Relative.leftChild);
                ptrBT_new.Insert(bt2.Root, BinaryTree <CharacterFrequency> .Relative.rightChild);
                //Adds the Root into the list ordered by count from smallest to largest
                if (tree.Count == 0)
                {
                    tree.AddFirst(ptrBT_new);
                }
                else if (tree.First.Value.Root.Data.count >= ptrBT_new.Root.Data.count)
                {
                    tree.AddFirst(ptrBT_new);
                }
                else if (tree.Last.Value.Root.Data.count <= ptrBT_new.Root.Data.count)
                {
                    tree.AddLast(ptrBT_new);
                }
                else
                {
                    LinkedListNode <BinaryTree <CharacterFrequency> > treenode = tree.First;
                    while (treenode.Value.Root.Data.count < ptrBT_new.Root.Data.count && treenode != null)
                    {
                        treenode = treenode.Next;
                    }
                    tree.AddBefore(treenode, ptrBT_new);
                }
                //clean up
                cf        = null;
                ptrBT_new = null;
                bt1       = null;
                bt2       = null;
            }
            return(tree);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("To compress a file enter Compress, to decompress a file enter Decompress\tTo exit type Exit");
            String enter    = Console.ReadLine();
            String filePath = null;

            while (enter.ToLower() != "exit")
            {
                //****************Compression******************//

                if (enter.ToLower() == "compress")
                {
                    Console.WriteLine("Please enter in the absolute path for the file:");
                    filePath = Console.ReadLine();
                    try
                    {
                        LinkedList <CharacterFrequency> cflist = new LinkedList <CharacterFrequency>();
                        Console.WriteLine("Counting characters please wait!");
                        Charactercounting(filePath, cflist);
                        Console.WriteLine("Characters Counted!");
                        //Check to see if the file is empty
                        if (cflist.Count == 0)
                        {
                            Console.WriteLine("Cannot compress a file that is empty. Please re enter another command");
                            enter = Console.ReadLine();
                        }
                        else
                        {
                            //Variables created only if we're comrpessing and the file isnt empty.
                            String newFile = null;
                            String addName = "_Encode.txt";
                            LinkedList <encoding> encode = new LinkedList <encoding>();
                            LinkedList <BinaryTree <CharacterFrequency> > tree = new LinkedList <BinaryTree <CharacterFrequency> >();
                            //Write the cflist onto the encoding file here
                            newFile = getNewFileName(filePath, addName);
                            if (File.Exists(newFile))
                            {
                                File.Delete(newFile);
                            }

                            File.Create(newFile).Close();

                            StreamWriter sw = new StreamWriter(newFile, false);

                            LinkedListNode <CharacterFrequency> node = cflist.First;
                            sw.WriteLine(cflist.Count + "\n");
                            while (node != null)
                            {
                                sw.WriteLine(node.Value.Print());
                                node = node.Next;
                            }
                            sw.Close();
                            String newfileText = getNewFileName(filePath, "_EncodeText.txt");
                            //Building tree
                            Console.WriteLine("Building tree, please wait");
                            buildTree(cflist, tree);
                            Console.WriteLine("Tree built!");
                            encodeTable(tree, encode);
                            //compression
                            Console.WriteLine("Compressing File please do NOT exit the program");
                            StreamReader sr = new StreamReader(filePath);
                            compress(encode, newfileText, sr);
                            sr.Close();
                            Console.WriteLine("File encoded!");
                            Console.WriteLine("Please enter another command");
                            enter = Console.ReadLine();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine("The file could not be opened, make sure you gave the right path to the file.");
                        Console.WriteLine("Please re enter another command");
                        enter = Console.ReadLine();
                    }
                }

                //************Decompression***************//

                else if (enter.ToLower() == "decompress")
                {
                    Console.WriteLine("Please enter the absolute path of the file that you want to decompress");
                    filePath = Console.ReadLine();
                    try
                    {
                        String encodeFile;
                        encodeFile = getNewFileName(filePath, "_Encode.txt");
                        String encodeText     = getNewFileName(filePath, "_EncodeText.bin");
                        String decompressPath = getNewFileName(filePath, "_Decode.txt");
                        if (File.Exists(encodeFile) && File.Exists(encodeText))
                        {
                            //Variables
                            StreamReader sr = new StreamReader(encodeFile);

                            LinkedList <CharacterFrequency> cflist = new LinkedList <CharacterFrequency>();
                            //get the total number of unique characters in the file so it knows when to stop;
                            int listcount = int.Parse(sr.ReadLine());
                            int counter   = 0;
                            sr.ReadLine();
                            //gets the total number of characters in the file
                            //Creating the Tree
                            String   line  = sr.ReadLine();
                            Char[]   split = new Char[] { ',' };
                            String[] results;
                            //Recreating the Linked List of CharacterFrequencys
                            while (counter < listcount)
                            {
                                results = line.Split(split, StringSplitOptions.None);
                                CharacterFrequency cf = new CharacterFrequency(int.Parse(results[0]), int.Parse(results[1]));
                                cflist.AddLast(cf);
                                sr.ReadLine();
                                line = sr.ReadLine();
                                counter++;
                            }
                            sr.Close();
                            //gets the total
                            int totalchar = total(cflist);

                            LinkedList <BinaryTree <CharacterFrequency> > tree = new LinkedList <BinaryTree <CharacterFrequency> >();
                            Console.WriteLine("Building tree, please wait");
                            buildTree(cflist, tree);
                            Console.WriteLine("Tree built");
                            //Decompressing
                            Console.WriteLine("Decompressing File please do NOT exit the program");

                            decompress(tree, encodeText, decompressPath, totalchar);

                            Console.WriteLine("File Decompressed!");
                            Console.WriteLine("Please enter another command");
                            enter = Console.ReadLine();
                        }
                        else
                        {
                            Console.WriteLine("Cannot decompress a file when the encoding table could not be found");
                            Console.WriteLine("Either the encoding table was lost or the file you selected was not compressed");
                            Console.WriteLine("Please enter another command");
                            enter = Console.ReadLine();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine("The file could not be opened, make sure that you gave the right path to the file.");
                        Console.WriteLine("Please re enter another command");
                        enter = Console.ReadLine();
                    }
                }

                else
                {
                    Console.WriteLine("Please enter in a valid command");
                    enter = Console.ReadLine();
                }
            }
            Environment.Exit(0);
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            Console.WriteLine(" To decompress a file enter a method of input 'FILE/MANUAL'");
            String enter    = Console.ReadLine();
            String filePath = null;

            if (enter.ToLower() == "file")
            {
                //Reading from file
                Console.WriteLine("Please enter the absolute path of the file that you want to decompress");
                filePath = Console.ReadLine();
                try
                {
                    String encodeFile;
                    encodeFile = getNewFileName(filePath, "_Encode.txt");
                    String encodeText     = getNewFileName(filePath, "_EncodeText.bin");
                    String decompressPath = getNewFileName(filePath, "_Decode.txt");
                    if (File.Exists(encodeFile) && File.Exists(encodeText))
                    {
                        //Variables
                        StreamReader sr = new StreamReader(encodeFile);

                        LinkedList <CharacterFrequency> cflist = new LinkedList <CharacterFrequency>();
                        //get the total number of unique characters in the file so it knows when to stop;
                        int listcount = int.Parse(sr.ReadLine());
                        int counter   = 0;
                        sr.ReadLine();
                        //gets the total number of characters in the file
                        //Creating the Tree
                        String   line  = sr.ReadLine();
                        Char[]   split = new Char[] { ',' };
                        String[] results;
                        //Recreating the Linked List of CharacterFrequencys
                        while (counter < listcount)
                        {
                            results = line.Split(split, StringSplitOptions.None);
                            CharacterFrequency cf = new CharacterFrequency(int.Parse(results[0]), int.Parse(results[1]));
                            cflist.AddLast(cf);
                            sr.ReadLine();
                            line = sr.ReadLine();
                            counter++;
                        }
                        sr.Close();
                        //gets the total
                        int totalchar = total(cflist);

                        LinkedList <BinaryTree <CharacterFrequency> > tree = new LinkedList <BinaryTree <CharacterFrequency> >();
                        Console.WriteLine("Building tree, please wait");
                        buildTree(cflist, tree);
                        Console.WriteLine("Tree built");
                        //Decompressing
                        Console.WriteLine("Decompressing File please do NOT exit the program");

                        decompressFile(tree, encodeText, decompressPath, totalchar);

                        Console.WriteLine("File Decompressed!");
                        Console.WriteLine("Please enter another command");
                        enter = Console.ReadLine();
                    }
                    else
                    {
                        Console.WriteLine("Cannot decompress a file when the encoding table could not be found");
                        Console.WriteLine("Either the encoding table was lost or the file you selected was not compressed");
                        Console.WriteLine("Please enter another command");
                        enter = Console.ReadLine();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("The file could not be opened, make sure that you gave the right path to the file.");
                    Console.WriteLine("Please re enter another command");
                    enter = Console.ReadLine();
                }
            }
            else if (enter.ToLower() == "manual")
            {
                String decompressPath = getNewFileName(filePath, "_Decode.txt");
                Console.WriteLine(" Input characters, to exit write 'exit'");
                Console.WriteLine(" Input must be in a form of 'X,3'");
                LinkedList <CharacterFrequency> cflist = new LinkedList <CharacterFrequency>();
                while (true)
                {
                    String input = Console.ReadLine();
                    if (input.ToLower() != "exit")
                    {
                        break;
                    }
                    Char[]   split = new Char[] { ',' };
                    String[] results;

                    results = input.Split(split, StringSplitOptions.None);
                    CharacterFrequency cf = new CharacterFrequency(int.Parse(results[0]), int.Parse(results[1]));
                    cflist.AddLast(cf);
                }


                //Recreating the Linked List of CharacterFrequen

                int totalchar = total(cflist);

                LinkedList <BinaryTree <CharacterFrequency> > tree = new LinkedList <BinaryTree <CharacterFrequency> >();
                Console.WriteLine("Building tree, please wait");
                buildTree(cflist, tree);
                Console.WriteLine("Tree built");
                //Decompressing
                Console.WriteLine("Decompressing File please do NOT exit the program");

                decompressManual(tree, decompressPath, totalchar);

                Console.WriteLine("File Decompressed!");
                Console.WriteLine("Please enter another command");
                enter = Console.ReadLine();
            }
        }