Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<iomanip>
- #include<vector>
- #include<string>
- using namespace std;
- char getMenuChoice();
- void verticalSpace(int);
- class countyElectionResults{
- friend ostream &operator<<(ostream &, countyElectionResults);
- friend istream &operator>>(istream &, countyElectionResults &);
- public:
- countyElectionResults();
- countyElectionResults(string);
- bool loadFile(string);
- string getCountyName();
- vector<char> getVotes();
- void showVotes();
- int getNullifierVotes();
- int getFielditeVotes();
- int getOtherVotes();
- int getTotalVotes();
- countyElectionResults * getNextResult();
- void setNextResult(countyElectionResults*);
- private:
- string countyName;
- vector<char> votes;
- int nullifierVotes;
- int fielditeVotes;
- int otherVotes;
- void countVotes();
- countyElectionResults * next;
- };
- ostream &operator<<(ostream &output, countyElectionResults results){
- output << setw(25) << results.countyName
- << setw(15) << results.fielditeVotes
- << setw(15) << results.nullifierVotes
- << setw(15) << results.otherVotes;
- return output;
- }
- istream &operator>>(istream &input, countyElectionResults &results){
- char vote;
- getline(input, results.countyName);
- while (input >> vote){
- results.votes.push_back(vote);
- }
- results.countVotes();
- return input;
- }
- countyElectionResults * countyElectionResults::getNextResult(){
- return next;
- }
- void countyElectionResults::setNextResult(countyElectionResults* nextNode){
- next = nextNode;
- }
- countyElectionResults::countyElectionResults(){
- nullifierVotes = 0;
- fielditeVotes = 0;
- otherVotes = 0;
- next = NULL;
- }
- countyElectionResults::countyElectionResults(string filename){
- nullifierVotes = 0;
- fielditeVotes = 0;
- otherVotes = 0;
- next = NULL;
- loadFile(filename);
- }
- bool countyElectionResults::loadFile(string filename){
- ifstream countyFile;
- char vote;
- countyFile.open(filename.c_str());
- if (!countyFile){
- cout << "File not found: " << filename << endl;
- return false;
- }
- // Read in the first line with the county name
- getline(countyFile, countyName);
- while (countyFile >> vote){
- votes.push_back(vote);
- }
- countVotes();
- return true;
- }
- void countyElectionResults::showVotes(){
- cout << setw(15) << countyName << endl;
- for (int i = 0; i < votes.size(); i++){
- cout << setw(15) << votes[i] << " " <<endl;
- }
- }
- string countyElectionResults::getCountyName(){
- return countyName;
- }
- // countyElectionResults::getNextResult(){
- // return next;
- // }
- vector<char> countyElectionResults::getVotes(){
- return votes;
- }
- int countyElectionResults::getNullifierVotes(){
- return nullifierVotes;
- }
- int countyElectionResults::getFielditeVotes(){
- return fielditeVotes;
- }
- int countyElectionResults::getOtherVotes(){
- return otherVotes;
- }
- int countyElectionResults::getTotalVotes(){
- return votes.size();
- }
- void countyElectionResults::countVotes(){
- nullifierVotes = 0;
- fielditeVotes = 0;
- otherVotes = 0;
- for (int i = 0; i < votes.size(); i ++){
- switch (votes[i]){
- case 'N':
- case 'n':
- nullifierVotes ++;
- break;
- case 'F':
- case 'f':
- fielditeVotes ++;
- break;
- default:
- otherVotes ++;
- }
- }
- }
- class countyElectionList{
- public:
- countyElectionList();
- countyElectionResults at(int);
- void push_back(countyElectionResults *);
- int size();
- bool empty();
- private:
- countyElectionResults * head;
- int elementCount;
- };
- // A constructor that initializes the private data members
- countyElectionList::countyElectionList(){
- }
- // This returns the countyElectionResults result at a particular point
- // in the list.
- // This is analogous to the at method in the vector class.
- countyElectionResults countyElectionList::at(int place){
- }
- // This method adds a countyElectionResults object to the end of the list.
- // It is analogous to the push_back method in the vector class.
- void countyElectionList::push_back(countyElectionResults * newCER){
- if(head == NULL){
- head = newCER;
- else
- {
- countyElectionResults * current = head;
- while(current->getNextResult() != NULL){
- current = current->getNextResult();
- }
- current->setNextResult(newCER);
- }
- }
- // This method returns the number of elements on the list.
- // It is analogous to the size method in the vector class
- int countyElectionList::size(){
- }
- //This method returns true iff the list has no elements
- // It is analogous to the empty method in the vector class.
- bool countyElectionList::empty(){
- }
- class stateElectionResults {
- public:
- void addCountyFile();
- void showAllCountyResults();
- void showCountyResults(string);
- void showCountyVotes(string);
- void listCounties();
- private:
- countyElectionList stateResults;
- };
- void stateElectionResults::addCountyFile(){
- string countyFileName;
- int voteCount = 0;
- cout << "Please enter the county file name to add:";
- getline(cin, countyFileName);
- countyElectionResults * newResults = new (countyElectionResults);
- if (newResults->loadFile(countyFileName)){
- stateResults.push_back(newResults);
- }
- }
- void stateElectionResults::showAllCountyResults(){
- int nullifierVotes = 0;
- int fielditeVotes = 0;
- int otherVotes = 0;
- verticalSpace(5);
- cout << setw(25) << "County Name"
- << setw(15) << "Fieldite Votes"
- << setw(15) << "Nullifier Votes"
- << setw(15) << "Other Votes"
- << endl;
- for (int i = 0; i < stateResults.size(); i++){
- cout << stateResults.at(i) << endl;
- nullifierVotes += stateResults.at(i).getNullifierVotes();
- fielditeVotes += stateResults.at(i).getFielditeVotes();
- otherVotes += stateResults.at(i).getOtherVotes();
- }
- cout << setw(25) << "Total:"
- << setw(15) << fielditeVotes
- << setw(15) << nullifierVotes
- << setw(15) << otherVotes
- << endl;
- verticalSpace(5);
- }
- void stateElectionResults::showCountyResults(string countyName){
- verticalSpace(5);
- for (int i = 0; i < stateResults.size(); i++){
- if (stateResults.at(i).getCountyName() == countyName){
- cout << stateResults.at(i) << endl;
- break;
- }
- }
- verticalSpace(5);
- }
- void stateElectionResults::showCountyVotes(string countyName){
- for (int i = 0; i < stateResults.size(); i++){
- if (stateResults.at(i).getCountyName() == countyName){
- stateResults.at(i).showVotes();
- break;
- }
- }
- }
- void stateElectionResults::listCounties(){
- cout << setw(25) << "COUNTIES" << endl;
- for (int i = 0; i < stateResults.size(); i++){
- cout << setw(25) << stateResults.at(i).getCountyName() << endl;
- }
- }
- int main(){
- stateElectionResults theResults;
- bool done = false;
- char choice;
- string county;
- do {
- choice = getMenuChoice();
- switch (choice){
- case 'Q':
- case 'q':
- done = true;
- break;
- case 'A':
- case 'a':
- theResults.addCountyFile();
- break;
- case 'R':
- case 'r':
- theResults.showAllCountyResults();
- break;
- case 'L':
- case 'l':
- theResults.listCounties();
- break;
- case 'C':
- case 'c':
- cout << "Please enter the county name: ";
- getline(cin,county);
- theResults.showCountyResults(county);
- break;
- case 'V':
- case 'v':
- cout << "Please enter the county name: ";
- getline(cin,county);
- theResults.showCountyVotes(county);
- break;
- default:
- cout << "Unknown choice. Please try again" << endl;
- }
- } while (!done);
- }
- // This main is for simple testing of the the countyElectionResults class
- // int main(){
- // Make three county Election result objects
- // countyElectionResults* lake = new countyElectionResults("Lake-co-gov.txt");
- // countyElectionResults* dover = new countyElectionResults("Dover-co-gov.txt");
- // countyElectionResults* clackamas = new countyElectionResults("Clackamas-co-gov.txt");
- // Link them together into a linked list
- // countyElectionResults* head = lake;
- // lake->setNextResult(dover);
- // dover->setNextResult(clackamas);
- // Print out the elements of the linked list so that we can see if the
- // self-referential elements are working.
- // cout << "Printing county names as: Lake Dover Clackamas" << endl;
- // cout << "If those print but then your program crashes, you" << endl;
- // cout << "probably forgot to set the nextResult pointer to null in the constructor" << endl;
- // cout << "--------------------------" << endl;
- // countyElectionResults* current = head;
- // while (current != NULL){
- // cout << current->getCountyName() << endl;
- // current = current->getNextResult();
- // }
- // cout << "--------------------------" << endl;
- // cout << "If this prints out, then the self-referential part looks good" << endl;
- // }
- void verticalSpace(int space){
- for (int i = 0; i < space; i++){
- cout << endl;
- }
- }
- char getMenuChoice(){
- char choice;
- verticalSpace(5);
- cout << setw(45) << "A: Add a county file" << endl;
- cout << setw(45) << "L: List counties" << endl;
- cout << setw(45) << "R: show all results" << endl;
- cout << setw(45) << "C: show single county result" << endl;
- cout << setw(45) << "V: verify single county votes" << endl;
- cout << setw(45) << "Q: quit" << endl;
- cout << "Your choice: ";
- cin >> choice;
- string trash;
- getline (cin,trash);
- verticalSpace(5);
- return choice;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement