Write a program to read any text file and print out a table listing the frequency of occurrence of each letter of the alphabet. The program should also print a count of all the whitespace characters, and all the non-alphabetic, non-whitespace characters in the file, and the text itself.
addChar and addRange for CType to do this. Each class should encapsulate a counting mechanism specialized for it.run function) will be uniform across all three classes.
#include <fstream.h>
...
char fn[256];
cin >> fn; // reads the file name from the user
ifstream in(fn); // opens the file for reading
...
char c;
in.get(c)... // reads a single character from the file
...
Here is the code for the class Ctype (this will be mailed to you):
class CType {
private:
class CSubRange { // a private nested class
private:
char low, high; // the upper and lower bounds of the range
public:
CSubRange(int l = 0, int h = 0) : low(l), high(h) {}
bool inRange(char c) { return c >= low && c <= high; }
};
protected:
char *singles; // a pointer to an array of characters
CSubRange *ranges; // a pointer to an array of sub-ranges
int nSingles, nRanges; // the number of characters in
// singles and the
// number of sub-ranges in ranges
public:
CType(int nMaxRanges = 0, int nMaxSingles = 0) :
singles(new char[nMaxSingles]), ranges(new CSubRange[nMaxRanges]),
nSingles(0), nRanges(0) {}
~CType() { delete [] singles; delete [] ranges; }
void addChar(char c) { // add a new character
singles[nSingles++] = c;
}
void addRange(int l, int h) { // add a new sub-range
ranges[nRanges++] = CSubRange(l, h);
}
bool contains(char c) const; // returns true if c is a member of
// the type
};
bool CType::contains(char c) const {
for (int s = 0; s < nSingles; s++) // is it one of the single characters?
if (c == singles[s])
return true;
for (int r = 0; r < nRanges; r++) // is it in one of the ranges?
if (ranges[r].inRange(c))
return true;
return false;
}
Hand in your documents to me (RTH) on November 6th. before 5:00 pm.