Down here is the question and below it its answer( in case it might help ) . I w
ID: 3840250 • Letter: D
Question
Down here is the question and below it its answer( in case it might help ) . I want OBJECT ORIENTED PROGRAMMING for this question, percentage of leading numbers.
Exercise 1-Character Counter The goal of this laboratory session is to develop a small Console application that counts the number of occurrences of each character (byte value) in a given input file. The solution requires approx. 80 lines of code. As a simplification, we write just a main function to implement the character counting process. (Decomposing the problem into several smaller functions would normally be better, but we do not know enough about parameter passing in C++ at this stage, which is a prerequisite for function decomposition.) The charcounter main function must define five distinct parts Check command line arguments and open an input file Declare two variables: an integer variable to count the total number of characters processed and a array of integers with 256 elements to count the character frequencies. The two variables must be properly initialized. Count the characters in the input file. A simple while-loop can do the trick. We use binary I/O. So, we need to use the get method to read a character from a file in binary mode. We can use the result as an index to increment the corresponding element in the character frequency array Produce a console output by traversing the character frequency array. Skip entries with value 0. All printable entries (is graph (c) returns true if c s the value of the current index) must be printed as a normal character, whereas entries with no graphical representation must be printed as two-character hexadecimal numbers. This step requires a for-loop. You can convert an int value i to a char value c by char c i; (there is another way to achieve this in C/C++, but this approach works for now) Close the input file and terminate the main function Before you can start, you need to get a brief overview of the cctype character handling functions, the iostream standard output stream, the ifstream input file stream, and iomanip stream manipulators. htt www.cplusplus.com/reference In order to build the program you need to understand the use of the c ctype function isgraph, the iomanip manipulators o dec print a number in decimal format, o hex print a number in hexadecimal format, o setw set the width for formatted output, and o setfill set the fill character for formatted output. I/O can always go wrong for any reasons. You must define provisions in your program to test,Explanation / Answer
#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;
class CharaterCounter
{
private:
int lNumberOfCharacters;
int lCharacterCounts[256];
bool isReady;
ifstream lInput; // declare an input file variable (object)
public:
CharaterCounter(){ // cunstructor
lNumberOfCharacters = 0;
isReady = false;
// init array
for ( int i = 0; i < 256; i++ )
lCharacterCounts[i] = 0;
}
//This function is reading file of characters
bool readFile(char * fileame){
lInput.open( fileame, ifstream::binary ); // open an input file (binary)
if ( !lInput.good() )
{
// operation failed
cerr << "Cannot open input file " << fileame << endl;
return false; // program failed (input)
}
while ( lInput.good() )
{
int lChar = lInput.get();
if ( lInput.good() )
{
lCharacterCounts[lChar]++;
lNumberOfCharacters++;
}
}
lInput.close();
isReady = true;
return true;
}
//This function is printing Result
void printData(){
if(!isReady){
cout << "Fist call functin readFile(filename) " << endl;
return;
}
cout << "Total number of characters read: " << lNumberOfCharacters << endl;
for ( int i = 0; i < 256; i++ )
{
if ( lCharacterCounts[i] != 0 )
{
char c = i;
if ( isgraph( c ) ){
cout << " " << c;
}
else{
cout << hex << setw( 2 ) << setfill( '0' ) << i;
}
cout << ": " << dec << lCharacterCounts[i] << endl;
}
}
}
};
int main( int argc, char* argv[] )
{
if ( argc < 2 )
{
cerr << "Arguments missing" << endl;
cerr << "Usage: CharCounter infile" << endl;
return 1; // program failed
}
CharaterCounter charCounter;
charCounter.readFile(argv[1]);
charCounter.printData();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.