Advertisement
teknique

Untitled

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