/****************************************************************************************** ex10.cpp ******************************************************************************************/ #include <iostream> #include <string> #include <fstream> using namespace std; // class definitions class person { public: string first; string last; string state; }; class state { public: string full; string token; int count; float percent; }; // prototypes void readFile(string filename, person [], const int maximum, int& size); int TallyByState(string token, person [], const int size); void fullTally(state [], const int, person [], const int); void displayResults(state list[], const int count); void loadStates(string filename, state list[], const int maximum, int& size); void calculateOther(state[], int&, const int); // MAIN! void main() { int StateCount = 0; const int StateSize=50+1; state Tally[StateSize]; string filename; cout << "Enter the file to read for STATE CRITERIA: "; cin >> filename; loadStates(filename, Tally, StateSize, StateCount); int count=0; const int maxsize=100; person census[maxsize]; cout << "Enter the file to read for CENSUS DATA: "; cin >> filename; readFile(filename, census, maxsize, count); fullTally(Tally, StateCount, census, count); calculateOther(Tally, StateCount, count); displayResults(Tally, StateCount); } //------- Generate an "OTHER PLACES" entry ------ void calculateOther(state list[], int& count, int total) { list[count].full = "Other States"; list[count].count = total; list[count].percent = 1.0; for (int i = 0; i < count; i++) { list[count].count -= list[i].count; list[count].percent -= list[i].percent; } count++; } //------- fill state critera data from a file ------ void loadStates(string filename, state list[], const int maximum, int& size) { ifstream fin(filename.c_str()); for (int i=0; i < maximum; i++) { if(fin >> list[i].full >> list[i].token) size++; } } //------- show all the results ------ void displayResults(state list[], const int count) { int i; for (i = 0; i < count; i++) cout << list[i].count << " records from "<< list[i].full << endl; cout << endl; for (i = 0; i < count; i++) cout << list[i].percent*100 << "% of records from "<< list[i].full << endl; } //------- do all the calculations ------ void fullTally(state criteria[], const int critCount, person data[], const int dataCount) { for (int i = 0; i < critCount; i++) { criteria[i].count = TallyByState(criteria[i].token, data, dataCount); criteria[i].percent = (float)criteria[i].count/dataCount; } } //------- return the count of records where state matches the token ------ int TallyByState(string token, person list[], const int size) { int count = 0; for (int i = 0; i < size; i++) if (list[i].state == token) count++; return count; } //------- fill each person in the list from a file ------ void readFile(string filename, person people[], const int maximum, int& size) { ifstream fin(filename.c_str()); for (int i=0; i < maximum; i++) { if(fin >> people[i].first >> people[i].last >> people[i].state) size++; } }