Design a program that reads an ASCII text file (provided) one byte at a time and
ID: 3756935 • Letter: D
Question
Design a program that reads an ASCII text file (provided) one byte at a time and produces an output file that contains the frequency count of how many times each character appears in the input file. Do not sort the output. A character frequency class instance must be used to represent each unique character in the file (see below). For this exercise, the character frequency objects must be processed and stored using a Vector. Reading the entire file into memory is prohibited.
For example, given the sample input file: Hello. Would yield the following output file. (10) 1 (13) 1 .(46) 1 H(72) 1 e(101) 1 l(108) 2 o(111) 1 The output file must follow the format presented above. Character(ASCII Value) (tab) Frequency. Note that formatting anomalies are expected due to the nature of the characters themselves (e.g., the tab character, carriage return, etc.) and will not cause a point deduction. The program should be able to be executed from the command line using the following syntax: programname.exe . 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 ) You may use C++, VB, C#, or Java to implement this program as long as the following requirements are met. A C++, VB, or C# project must be created in Visual Studio 2017. Java programs must be created in the Eclipse project format or provided in source code form with instructions on how to compile and run the program. Regardless of the language used, the entire project must be submitted as a single ZIP file that contains the project folder, source code, and documentation.
Explanation / Answer
ANSWER :
Please find the code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ASCII
{
class CharacterFrequency
{
private char ch;
private int frequency;
public char Ch
{
get { return ch; }
set { ch = value; }
}
public int Frequency
{
get { return frequency; }
set { frequency = value; }
}
}
class Program
{
public string InputFileName="";
public string OutputFileName="Output.txt";
public string FilePath="";
public static SortedDictionary<char, ulong> Count(string stringToCount)
{
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
{
if (!characterCount.ContainsKey(character)) // added character to dictionary if only character is absent in charactercount
{
characterCount.Add(character, 1);
}
else
{
characterCount[character]++; // increemetned count
}
}
return characterCount;
}
static void Main(string[] args)
{
Program p = new Program();
CharacterFrequency obj = new CharacterFrequency() ;
StreamWriter streamWriter = new StreamWriter(p.OutputFileName);
try
{
p.InputFileName = args[0]; // get input
p.OutputFileName = args[1]; // get output
p.FilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\" + p.InputFileName; // get file path
if (File.Exists(p.FilePath)) // checked if file exist
{
string data = File.ReadAllText(p.FilePath); // read the data in string
var count = Program.Count(data); // passthe string to count method
foreach (var character in count)
{
streamWriter.WriteLine(character.Key+"("+(int)character.Key+")"+" "+character.Value); // write data to output file
}
streamWriter.Close();
Console.ReadLine();
}
else
{
Console.WriteLine("Please provide input File"); // if input file absent sent message to user to place input file
}
}
catch (Exception ex)
{
Console.WriteLine("Exception occured " + ex.Message.ToString());
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.