Advertisement
teknique

Untitled

Dec 6th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.48 KB | None | 0 0
  1. //
  2. // Project 5
  3. // Name: Jay Mellick
  4. // COSC 051
  5. //
  6. // In accordance with the class policies and Georgetown's Honor Code,
  7. // I certify that I have neither given nor received any assistance
  8. // on this project with the exceptions of the lecture notes and those
  9. // lis noted below.
  10. //
  11. // Description: Project 5 that uses methods and classes and functions and linked lists.
  12. //
  13.  
  14. #include<iostream>
  15. #include<fstream>
  16. #include<iomanip>
  17. #include<vector>
  18. #include<string>
  19. using namespace std;
  20.  
  21. char getMenuChoice();
  22. void verticalSpace(int);
  23.  
  24.  
  25. class countyElectionResults{
  26.  
  27.   friend ostream &operator<<(ostream &, countyElectionResults);
  28.   friend istream &operator>>(istream &, countyElectionResults &);
  29.  
  30. public:
  31.  
  32.   countyElectionResults();
  33.   countyElectionResults(string);
  34.   bool loadFile(string);
  35.   string getCountyName();
  36.   vector<char> getVotes();
  37.   void showVotes();
  38.   int getNullifierVotes();
  39.   int getFielditeVotes();
  40.   int getOtherVotes();
  41.   int getTotalVotes();
  42.   countyElectionResults * getNextResult();
  43.   void setNextResult(countyElectionResults*);
  44.  
  45. private:
  46.   string countyName;
  47.   vector<char> votes;
  48.   int nullifierVotes;
  49.   int fielditeVotes;
  50.   int otherVotes;
  51.   void countVotes();
  52.   countyElectionResults * next;
  53.  
  54. };
  55.  
  56. ostream &operator<<(ostream &output, countyElectionResults results){
  57.  
  58.  
  59.   output << setw(25) << results.countyName
  60.      << setw(15) << results.fielditeVotes
  61.      << setw(15) << results.nullifierVotes
  62.      << setw(15) << results.otherVotes;
  63.  
  64.  
  65.   return output;
  66. }
  67.  
  68. istream &operator>>(istream &input, countyElectionResults &results){
  69.  
  70.  
  71.   char vote;
  72.   getline(input, results.countyName);
  73.    
  74.   while (input >> vote){
  75.     results.votes.push_back(vote);
  76.   }
  77.   results.countVotes();  
  78.  
  79.   return input;
  80. }
  81.  
  82. //tells us what the next variable is
  83. countyElectionResults * countyElectionResults::getNextResult(){
  84.     return next;
  85. }
  86.  
  87. //jumps to the next node
  88. void countyElectionResults::setNextResult(countyElectionResults* nextNode){
  89.     next = nextNode;
  90. }
  91.  
  92. countyElectionResults::countyElectionResults(){
  93.  
  94.   nullifierVotes = 0;
  95.   fielditeVotes = 0;
  96.   otherVotes = 0;
  97.   next = NULL;
  98.  
  99. }
  100.  
  101. countyElectionResults::countyElectionResults(string filename){
  102.  
  103.   nullifierVotes = 0;
  104.   fielditeVotes = 0;
  105.   otherVotes = 0;
  106.   next = NULL;
  107.   loadFile(filename);
  108. }
  109.  
  110.  
  111. bool countyElectionResults::loadFile(string filename){
  112.  
  113.  
  114.   ifstream countyFile;
  115.   char vote;
  116.  
  117.   countyFile.open(filename.c_str());
  118.   if (!countyFile){
  119.     cout << "File not found: " << filename << endl;
  120.     return false;
  121.   }
  122.  
  123.   // Read in the first line with the county name
  124.  
  125.   getline(countyFile, countyName);
  126.    
  127.   while (countyFile >> vote){
  128.     votes.push_back(vote);
  129.   }
  130.   countVotes();
  131.  
  132.   return true;
  133.  
  134. }
  135.  
  136. void countyElectionResults::showVotes(){
  137.  
  138.   cout << setw(15) << countyName << endl;
  139.   for (int i = 0; i < votes.size(); i++){
  140.     cout << setw(15) << votes[i] << " " <<endl;
  141.   }
  142. }
  143.  
  144. string countyElectionResults::getCountyName(){
  145.  
  146.   return countyName;
  147.  
  148. }
  149.  
  150. vector<char> countyElectionResults::getVotes(){
  151.  
  152.   return votes;
  153.  
  154. }
  155.  
  156. int countyElectionResults::getNullifierVotes(){
  157.  
  158.   return nullifierVotes;
  159.  
  160. }
  161.  
  162. int countyElectionResults::getFielditeVotes(){
  163.   return fielditeVotes;
  164.  
  165. }
  166.  
  167. int countyElectionResults::getOtherVotes(){
  168.  
  169.   return otherVotes;
  170.  
  171. }
  172.  
  173.  
  174. int countyElectionResults::getTotalVotes(){
  175.  
  176.   return votes.size();
  177.  
  178. }
  179.  
  180. void countyElectionResults::countVotes(){
  181.  
  182.   nullifierVotes = 0;
  183.   fielditeVotes = 0;
  184.   otherVotes = 0;
  185.  
  186.   for (int i = 0; i < votes.size(); i ++){
  187.     switch (votes[i]){
  188.     case 'N':
  189.     case 'n':
  190.       nullifierVotes ++;
  191.       break;
  192.     case 'F':
  193.     case 'f':
  194.       fielditeVotes ++;
  195.       break;
  196.     default:
  197.       otherVotes ++;      
  198.     }
  199.  
  200.   }
  201.  
  202. }
  203.  
  204. class countyElectionList{
  205.  
  206. public:
  207.   countyElectionList();
  208.   countyElectionResults at(int);
  209.   void push_back(countyElectionResults *);
  210.   int size();
  211.   bool empty();
  212.  
  213. private:
  214.   countyElectionResults * head;
  215.   int elementCount;
  216.  
  217. };
  218.  
  219. // A constructor that initializes the private data members
  220. countyElectionList::countyElectionList(){
  221.     countyElectionList::head = NULL;
  222.     elementCount = 0;
  223. }
  224.  
  225. // This returns the countyElectionResults result at a particular point
  226. // in the list.
  227. // This is analogous to the at method in the vector class.
  228. countyElectionResults countyElectionList::at(int place){
  229.     if(head == NULL){
  230.         countyElectionResults * name = NULL;
  231.         return * name;
  232.     }
  233.     else{
  234.         countyElectionResults * current = head;
  235.         int pos = 0;
  236.         while(pos != place && current->getNextResult() != NULL){
  237.             current = current->getNextResult();
  238.         }
  239.         pos++;
  240.         return * current;
  241.     }
  242.     cout << "Not Found" << endl;
  243. }
  244.  
  245.  
  246. // This method adds a countyElectionResults object to the end of the list.
  247. // It is analogous to the push_back method in the vector class.
  248. void countyElectionList::push_back(countyElectionResults * newCER){
  249.     if(head == NULL){
  250.         head = newCER;
  251.     }
  252.     else{
  253.         countyElectionResults * current = head;
  254.         while(current->getNextResult() != NULL){
  255.             current = current->getNextResult();
  256.         }
  257.     current->setNextResult(newCER);
  258.     }
  259.     elementCount = elementCount + 1;
  260. }
  261.  
  262.  
  263. // This method returns the number of elements on the list.
  264. // It is analogous to the size method in the vector class
  265. int countyElectionList::size(){
  266.     return elementCount;
  267. }
  268.  
  269.  
  270. //This method returns true iff the list has no elements
  271. // It is analogous to the empty method in the vector class.
  272. bool countyElectionList::empty(){
  273.     return(head == NULL);
  274. }
  275.  
  276.  
  277.  
  278. class stateElectionResults {
  279.  
  280. public:
  281.   void addCountyFile();
  282.   void showAllCountyResults();
  283.   void showCountyResults(string);
  284.   void showCountyVotes(string);
  285.   void listCounties();
  286.    
  287.  
  288. private:
  289.  countyElectionList stateResults;
  290.  
  291. };
  292.  
  293.  
  294.  
  295. void stateElectionResults::addCountyFile(){
  296.  
  297.   string countyFileName;
  298.   int voteCount = 0;
  299.  
  300.   cout << "Please enter the county file name to add:";
  301.   getline(cin, countyFileName);
  302.  
  303.   countyElectionResults *  newResults = new (countyElectionResults);
  304.   if (newResults->loadFile(countyFileName)){
  305.     stateResults.push_back(newResults);
  306.   }
  307.  
  308. }
  309.  
  310. void stateElectionResults::showAllCountyResults(){
  311.  
  312.   int nullifierVotes = 0;
  313.   int fielditeVotes = 0;
  314.   int otherVotes = 0;
  315.  
  316.   verticalSpace(5);
  317.   cout << setw(25) << "County Name"
  318.        << setw(15) << "Fieldite Votes"
  319.        << setw(15) << "Nullifier Votes"
  320.        << setw(15) << "Other Votes"
  321.        << endl;
  322.  
  323.   for (int i = 0; i < stateResults.size(); i++){
  324.  
  325.     cout << stateResults.at(i) << endl;
  326.    
  327.     nullifierVotes +=  stateResults.at(i).getNullifierVotes();
  328.     fielditeVotes +=  stateResults.at(i).getFielditeVotes();
  329.     otherVotes +=  stateResults.at(i).getOtherVotes();
  330.   }
  331.  
  332.   cout << setw(25) << "Total:"
  333.        << setw(15) << fielditeVotes
  334.        << setw(15) << nullifierVotes
  335.        << setw(15) << otherVotes
  336.        << endl;    
  337.   verticalSpace(5);
  338. }
  339.  
  340. void stateElectionResults::showCountyResults(string countyName){
  341.  
  342.   verticalSpace(5);
  343.   for (int i = 0; i < stateResults.size(); i++){
  344.  
  345.     if (stateResults.at(i).getCountyName() == countyName){
  346.       cout << stateResults.at(i) << endl;
  347.       break;
  348.     }
  349.   }
  350.   verticalSpace(5);
  351. }
  352.  
  353. void stateElectionResults::showCountyVotes(string countyName){
  354.  
  355.     for (int i = 0; i < stateResults.size(); i++){
  356.    
  357.       if (stateResults.at(i).getCountyName() == countyName){
  358.     stateResults.at(i).showVotes();
  359.     break;
  360.       }
  361.     }
  362. }
  363.  
  364. void stateElectionResults::listCounties(){
  365.  
  366.   cout << setw(25) << "COUNTIES" << endl;    
  367.   for (int i = 0; i < stateResults.size(); i++){
  368.     cout << setw(25) << stateResults.at(i).getCountyName() << endl;    
  369.   }
  370.  
  371. }
  372.  
  373.  
  374. int main(){
  375.  
  376.   stateElectionResults theResults;
  377.   bool done = false;
  378.   char choice;
  379.   string county;
  380.  
  381.   do {
  382.  
  383.     choice = getMenuChoice();
  384.     switch (choice){
  385.     case 'Q':
  386.     case 'q':
  387.       done = true;
  388.       break;
  389.     case 'A':
  390.     case 'a':
  391.       theResults.addCountyFile();
  392.       break;
  393.     case 'R':
  394.     case 'r':
  395.       theResults.showAllCountyResults();
  396.       break;
  397.     case 'L':
  398.     case 'l':
  399.       theResults.listCounties();
  400.       break;
  401.     case 'C':
  402.     case 'c':
  403.       cout << "Please enter the county name: ";
  404.       getline(cin,county);
  405.       theResults.showCountyResults(county);
  406.       break;
  407.     case 'V':
  408.     case 'v':
  409.       cout << "Please enter the county name: ";
  410.       getline(cin,county);
  411.       theResults.showCountyVotes(county);
  412.       break;
  413.     default:
  414.       cout << "Unknown choice. Please try again" << endl;
  415.     }
  416.    
  417.  
  418.   } while (!done);
  419. }
  420.  
  421.  
  422.  
  423. // This main is for simple testing of the the countyElectionResults class
  424. // int main(){
  425.  
  426.   // Make three county Election result objects
  427.  
  428.   // countyElectionResults* lake = new countyElectionResults("Lake-co-gov.txt");
  429.   // countyElectionResults* dover = new countyElectionResults("Dover-co-gov.txt");
  430.   // countyElectionResults* clackamas = new countyElectionResults("Clackamas-co-gov.txt");
  431.  
  432.  
  433.   // Link them together into a linked list
  434.   // countyElectionResults* head = lake;
  435.   // lake->setNextResult(dover);
  436.   // dover->setNextResult(clackamas);
  437.  
  438.   // Print out the elements of the linked list so that we can see if the
  439.   // self-referential elements are working.
  440.  
  441.   // cout << "Printing county names as: Lake Dover Clackamas" << endl;
  442.   // cout << "If those print but then your program crashes, you" << endl;
  443.   // cout << "probably forgot to set the nextResult pointer to null in the constructor" << endl;
  444.   // cout << "--------------------------" << endl;
  445.   // countyElectionResults* current = head;
  446.   // while (current != NULL){
  447.     // cout << current->getCountyName() << endl;
  448.     // current = current->getNextResult();    
  449.   // }
  450.   // cout << "--------------------------" << endl;
  451.   // cout << "If this prints out, then the self-referential part looks good" << endl;
  452.  
  453. // }
  454.  
  455.  
  456. void verticalSpace(int space){
  457.  
  458.   for (int i = 0; i < space; i++){
  459.     cout << endl;
  460.   }
  461.  
  462. }
  463.  
  464. char getMenuChoice(){
  465.  
  466.   char choice;
  467.   verticalSpace(5);
  468.   cout << setw(45) << "A: Add a county file" << endl;
  469.   cout << setw(45) << "L: List counties" << endl;
  470.   cout << setw(45) << "R: show all results" << endl;
  471.   cout << setw(45) << "C: show single county result" << endl;
  472.   cout << setw(45) << "V: verify single county votes" << endl;
  473.   cout << setw(45) << "Q: quit" << endl;
  474.  
  475.   cout << "Your choice: ";
  476.   cin >> choice;
  477.   string trash;
  478.   getline (cin,trash);
  479.   verticalSpace(5);
  480.   return choice;
  481.  
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement