#include #include #include "apvector.h" #include "apstring.h" const int NUM_GRADES = 4; // # different grades class GradeInfo { public: GradeInfo(); // constructor void Read(istream & input); // read info from input void PrintBarGraph(); // print bar graph of grades private: int CharToIndex(char ch); // convert char to index apvector myGrades; // # occurrences of each grade int myCount; // total # of grades }; GradeInfo::GradeInfo() : myGrades(NUM_GRADES,0), myCount(0) { // myGrades initialized to NUM_GRADES cells, all equal to 0 } int GradeInfo::CharToIndex(char ch) // precondition: 'A' <= ch <= 'D' // postcondition: returns int index corresponding to ch { return ch - 'A'; } void GradeInfo::Read(istream & input) // precondition: input is open for reading // postcondition: myGrades contains the frequency count of // each grade in input, // myCount contains the total number of grades // in input { int k,index; char ch; // initialize all counters to 0 for(k=0; k < NUM_GRADES; k++) { myGrades[k] = 0; } // read file while (input >> ch) { index = CharToIndex(ch); // map char to an index myGrades[index]++; // bump # occurrences myCount++; // bump total count } } void GradeInfo::PrintBarGraph() { int k,index; char ch; for(ch = 'A'; ch <= 'D'; ch++) { index = CharToIndex(ch); for(k=0; k < myGrades[index]; k++) { cout << ch; } cout << endl; } cout << "Total Grades = " << myCount << endl; }