Description: Design and write a program that reads a text file and counts the nu
ID: 3886865 • Letter: D
Question
Description: Design and write a program that reads a text file and counts the number of times each NON-WHITESPACE character occurs, then computes a single integer (long) value based on the character code and occurrence frequency of each such character. Calculation: hash value = sum of products (char_code x char_frequency) KEY: The program must behave the same way as the Unix-executable program HashFile.run, which is stored in the course public (OP) repository. OPcopy HashFile.run ./HashFile.run EXAMPLE: The file simple.dat contains: Fat cat ran slow. But the butterball turkey got stuck on the Thanksgiving table. Program output: (note the FORMATTING--up to 10 per line, spacing) SYMBOL FREQUENCIES: 2. 1B 1F 1T 6a 3b 2c 5e 3g 3h 2i 3k 41 4n 3o 3r 3s 11t 4u 1v 1w 1y simple.dat HASH VALUE = 7037Explanation / Answer
#
include < iostream > #include < fstream > #include < cctype > #include < string > #include < iomanip >
using namespace std;
void nonblank(int & , ifstream & );
void numwords(int & , ifstream & );
void numvowels(int & , ifstream & );
void numupper(int & , ifstream & );
//void numpattern();
int avglength(double avg);
//int or void for reprinting the filestream
int main() {
ifstream infile; //the input filestream to be read
string pattern; //the pattern to be matched in the file
string fname; //variable used to hold actual name of file
int n; //the number of non-whitespace characters
int w; //the number of words in the file
int v; //the number of vowels in the file
int u; //the number of uppercase letters in the file
int pat; //the number of times the pattern occurs in file
double avg; //the average characters per word
cout << "Please enter the file name to be read." << endl;
cin >> fname;
infile.open(fname.c_str());
cout << "Please enter the pattern to be matched." << endl;
cin >> pattern;
cout << " " << endl;
cout << "Pattern: " << pattern << endl;
cout << "Input file: " << fname << endl;
cout << " " << endl;
cout << left;
while (!cin.eof()) {
nonblank(n, infile); //call the function that counts non-whitespace characters
cout << "# non-blank characters: " << n << endl;
/* numwords(w, infile); //call function that counts number of words
cout << "# words: " << w << endl;
numvowels(v,infile); //call function that counts upper and lower case vowels
cout << "# vowels: " << v << endl;
numupper(u, infile); //call function that counts # uppercase letters in file
cout << "# uppercase letters: " << u << endl;
avglength(avg); //call function that computes average length of words
cout << "average characters/word: " << avg << endl;
*/
/*
numpattern();
cout << "# times " << pattern << " found: " << /*THE NUMBER*/
<< endl; * /
}
infile.close(); //command to close the input filestream
return 0;
}
void nonblank(int & n, ifstream & infile) {
char character;
n = 0;
cin.get(character);
if (character != ' ' || character != ' ') {
n = n + 1;
} else {
n = n + 0;
}
}