Question

c# csci312: character counter - vector design a program that reads in an ascii text file...

c# csci312: character counter - vector design a program that reads in an ascii text file (provided) ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provide... C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII character and the number of time the character appears in the file. Do not sort the output. Each unique character in the file must be represented by a character frequency class instance. For this exercise, the character frequency objects must be stored in a vector. For example, given the sample input file: Hello. The output file would look like this:

H(72) 1 e(101) 1 l(108) 2 o(111) 1 .(46) 1

The program should be able to be executed from the command line using the following syntax: programname.exe (optional for this assignment). For example, counter.exe myInput.txt Count.txt Where counter.exe is the program name, myInput.txt is the input ASCII file, and the Count.txt is the output file. The character frequency class must implement the essence of the object described in the UML diagram below. Note that methods described in the UML diagram below are not absolute and may be changed as desired as long as the basic functionality is preserved.

CharacterFrequency -ch : char -frequency : int +getCharacter() : char +setCharacter(in character : char) +getFrequency() : int +setFrequency(in frequency : int) +increment() +Equals() : bool +ToString() : string AY 2017-2018 DM The solutions will be graded on three components: design, development (code), and testing.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CharacterFrequency.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSCI312_A1_Schmidt
{
    class CharacterFrequency
    {
        byte m_ch;
        int m_frequency;
        bool m_equals;

        public CharacterFrequency(byte c)
        {
            m_ch = c;
            m_frequency = 1;
        }

        public CharacterFrequency()
        {

        }

        public byte getCharacter()
        {

            return m_ch;
        }

        public void setCharacter(byte ch)
        {
            m_ch = (byte)ch;

        }

        public int getFrequency()
        {
            return m_frequency;
        }

        public void setFrequency(int frequency)
        {
            m_frequency = frequency;
        }

        public void increment()
        {
            m_frequency++;
        }

        // Add bool
        public bool Equals(CharacterFrequency m_char)
        {
            if(m_ch == m_char.getCharacter())
            {
                return true;
              
            }
            else
            {
                return false;
            }
          
        }

      

        //Add ToString that makes sense
      
        public string toString()
        {
          

            return (char)m_ch + "(" + m_ch.ToString() + ") " + m_frequency;

        }

      

    }
}


CSCI312_A1_Schmidt.cs


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSCI312_A1_Schmidt
{
    class CSCI312_A2_Schmidt
    {
      

        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.");
                  
                }


          


        }

        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.");

            }

        }
    }
    }

Add a comment
Know the answer?
Add Answer to:
c# csci312: character counter - vector design a program that reads in an ascii text file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a C++ program that reads in a text file and reads each character or lexemes...

    write a C++ program that reads in a text file and reads each character or lexemes and states what token it is and writes to another file for example: while (fahr < upper) a = 23.00 whileend Output: token lexeme -------------------------------- keyword while separator ( identifier fahr operator < identifier upper separator ) identifier a operator = real 23.00 keyword whileend the program aslo reads in comments but does not add to the list. characters that are commented out or...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Write a C program that reads characters from a text file, and recognizes the identifiers in...

    Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text. b. Change the C program in exercise 2, so that all...

  • java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text...

    java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text file (a novel or a report, or just a sequence of letters or symbols), by reading the file into a byte array, convert to a string, and then scan the string letter by letter to count the frequencies of all unique characters in the text, after that, the letters with frequencies greater than 0 and the frequencies are reported side by side. All members...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • Write a C++ program that reads in a text file and writes the histogram of character counts sorted in alphabetical order to an output file. Eg, if the input file is “to be or not to be”, then the output should be: b **2 e **2 n *1 o ****4

    Write a C++ program that reads in a text file andwrites the histogram of character counts sorted inalphabetical order to an output file. Eg, if theinput file is “to be or not to be”, then the outputshould be:b   **2e   **2n   *1o   ****4r    *1t    ***3

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list...

    C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Ex: If the input is: 4 hello zoo sleep drizzle z then the output is: 200 drizzle To...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT