CS177/457 C++ Programming
Spring 1998

A Text Analyzer

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.

Procedure

  1. Study the attached class declaration of CType and and derive three new classes from it: one for alphabetic characters, one for whitespace characters and one for all others. You will need to use the constructor and member functions addChar and addRange for CType to do this. Each class should encapsulate a counting mechanism specialized for it.
  2. Design an application class to prompt for a file name, read the text file and analyze it using the classes you have derived from CType.
  3. Write and compile the program.
  4. Test your progam using suitable text files.
Hints Reading from a text file is just as easy as using standard input. e.g. #include <iostream.h> // standard input and output
#include <fstream.h> // file handling capabilities
...
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
...
if (in.eof())... // tests for end of file condition
 
Deliverables
  1. A printout of your program source code.
  2. Printouts of your program running on a test files.
  3. Printouts of the test files you used.
Due Date

Hand in your documents to me (RTH) on Monday, May 11th. before 5:00 pm. Mail your source code to the grader: login hhuang.

Here is the code for the class Ctype (download it):

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 {
  // is it one of the single characters?
  for (int s = 0; s < nSingles; s++)
    if (c == singles[s])
      return true;
  // is it in one of the ranges?
  for (int r = 0; r < nRanges; r++)
    if (ranges[r].inRange(c))
      return true;
  return false;
}