static void Main(string[] args)
        {
            string filePath   = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string folderPath = System.IO.Path.GetDirectoryName(filePath);

            Console.Out.WriteLine("Please enter file address to be read: ");

            string fileName = Console.In.ReadLine();

            Console.Out.WriteLine("Please enter desired file name: ");

            string desiredFileName = folderPath + "\\" + Console.In.ReadLine();
            string text;

            try
            {
                text = System.IO.File.ReadAllText(fileName);

                LinkedList <CharacterFrequency> chars = new LinkedList <CharacterFrequency>();



                foreach (byte c in text)
                {
                    CharacterFrequency m_char = new CharacterFrequency(c);
                    bool repeat = false;
                    foreach (CharacterFrequency m_chars in chars)
                    {
                        if (m_char.Equals(m_chars))
                        {
                            m_chars.increment();
                            repeat = true;
                            break;
                        }
                    }

                    if (repeat == false)
                    {
                        chars.AddLast(m_char);
                    }
                }

                System.IO.StreamWriter file = new System.IO.StreamWriter(desiredFileName + ".txt");
                using (file)
                {
                    foreach (CharacterFrequency freqs in chars)
                    {
                        if (freqs != null)
                        {
                            file.WriteLine(freqs.toString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Unable to read file, please try again.");
            }
        }
예제 #2
0
 // Add bool
 public bool Equals(CharacterFrequency m_char)
 {
     if (m_ch == m_char.getCharacter())
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        static void Main(string[] args)
        {
            string filePath   = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string folderPath = System.IO.Path.GetDirectoryName(filePath);

            Console.Out.WriteLine("Please enter file address to be read: ");

            string fileName = Console.In.ReadLine();

            Console.Out.WriteLine("Please enter desired file name: ");

            string desiredFileName = folderPath + "\\" + Console.In.ReadLine();
            string text;

            try
            {
                text = System.IO.File.ReadAllText(fileName);

                CharacterFrequency[] chars = new CharacterFrequency[255];
                int counter = 0;
                foreach (byte c in text)
                {
                    if (chars[Convert.ToInt32(c.ToString())] == null)
                    {
                        chars[Convert.ToInt32(c.ToString())] = new CharacterFrequency();
                        chars[Convert.ToInt32(c.ToString())].setCharacter(c);
                        chars[Convert.ToInt32(c.ToString())].setFrequency(1);
                        counter++;
                    }
                    else
                    {
                        chars[Convert.ToInt32(c.ToString())].increment();
                    }
                }

                System.IO.StreamWriter file = new System.IO.StreamWriter(desiredFileName + ".txt");
                using (file)
                {
                    foreach (CharacterFrequency freqs in chars)
                    {
                        if (freqs != null)
                        {
                            file.WriteLine(freqs.toString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Unable to read file, please try again.");
            }
        }
        static void ProcessWithArray(string fileName, string desiredFileName)
        {
            try
            {
                string text = System.IO.File.ReadAllText(fileName);

                CharacterFrequency[] chars = new CharacterFrequency[255];
                int counter = 0;
                foreach (byte c in text)
                {
                    if (chars[Convert.ToInt32(c.ToString())] == null)
                    {
                        chars[Convert.ToInt32(c.ToString())] = new CharacterFrequency();
                        chars[Convert.ToInt32(c.ToString())].setCharacter(c);
                        chars[Convert.ToInt32(c.ToString())].setFrequency(1);
                        counter++;
                    }
                    else
                    {
                        chars[Convert.ToInt32(c.ToString())].increment();
                    }
                }

                System.IO.StreamWriter file = new System.IO.StreamWriter(desiredFileName + ".txt");
                using (file)
                {
                    foreach (CharacterFrequency freqs in chars)
                    {
                        if (freqs != null)
                        {
                            file.WriteLine(freqs.toString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Unable to read file, please try again.");
            }
        }