Advertisement
teknique

Untitled

Dec 16th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <vector>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. class countyElectionResults{
  11.  
  12.   friend std::ostream &operator<<(ostream &, countyElectionResults);
  13.  
  14. public:
  15.   countyElectionResults();
  16.   countyElectionResults(string filename);
  17.   bool loadFile(string filename);
  18.   string getCountyName();
  19.   vector<char> getVotes();
  20.   void showVotes();
  21.   int getNullifierVotes();
  22.   int getFielditeVotes();
  23.   int getOtherVotes();
  24.   int getTotalVotes();
  25.  
  26.  
  27.  
  28. private:
  29.   string countyName;
  30.   vector<char> votes;
  31.   int nullifierVotes;
  32.   int fielditeVotes;
  33.   int otherVotes;
  34.   int totalVotes;
  35.   // A private function that counts all the votes stored in the
  36.   // votes vector
  37.   void countVotes();
  38.  
  39. };
  40.  
  41. class stateElectionResults {
  42.  
  43. public:
  44. // Asks the user for a file name, then loads that file into
  45.   // a countyElectionObject from that file and places it into a vector
  46.   void addCountyFile();
  47.   // Prints the results from all countyElection results
  48.   void showAllCountyResults();
  49.   // Prints the results from one county whose name is passed as a string
  50.   void showCountyResults(string);
  51.   // Prints votes from one county whose name is passed as a string
  52.   void showCountyVotes(string);
  53.   // Prints the names of the counties that are stored in the vector.
  54.   void listCounties();
  55.    
  56.  
  57. private:
  58.   vector<countyElectionResults> stateResults;;
  59.  
  60. };
  61.  
  62. //overloading the operator
  63. ostream &operator<<(ostream &output, countyElectionResults county_results)
  64. {
  65.     output << county_results.countyName;
  66.     output << county_results.nullifierVotes;
  67.     output << county_results.fielditeVotes;
  68.     output << county_results.otherVotes;
  69.     output << county_results.totalVotes;
  70. }
  71.  
  72. // Constructor - just initializes internal variables
  73. countyElectionResults::countyElectionResults()
  74. {
  75.     nullifierVotes = 0;
  76.     fielditeVotes = 0;
  77.     otherVotes = 0;
  78.     totalVotes = 0;
  79. }
  80.  
  81. // Constructor - initializes internal variables and loads data from file
  82. // string parameter is filename
  83. countyElectionResults::countyElectionResults(string filename)
  84. {
  85.     filename = " ";
  86. }
  87.  
  88. // Loads data from a file, string parameter is the filename
  89. // returns true if it worked, and false if not
  90. bool countyElectionResults::loadFile(string filename)
  91. {
  92.     char vote;
  93.     std::ifstream input((filename).c_str());
  94.     if(!input.good())
  95.     {
  96.         return false;
  97.     }
  98.     getline(input, countyName);
  99.     //counts each vote
  100.     while(input >> vote)
  101.     {
  102.         //adds it into a vector
  103.         votes.push_back(vote);
  104.     }
  105.     countVotes();
  106.     return true;
  107. }
  108.  
  109. //count each vote in a file
  110. void countyElectionResults::countVotes()
  111. {
  112.     for(int i=0; i < votes.size(); i++)
  113.     {
  114.         switch(votes[i])
  115.         {
  116.             case 'f':
  117.             case 'F':
  118.             {
  119.                 fielditeVotes++;
  120.                 break;
  121.             }
  122.             case 'n':
  123.             case 'N':
  124.             {
  125.                 nullifierVotes++;
  126.                 break;
  127.             }
  128.             default:
  129.             {
  130.                 otherVotes++;
  131.                 break;
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. // Returns the county name
  138. string countyElectionResults::getCountyName()
  139. {
  140.     return countyName;
  141. }
  142.  
  143. // Returns the vector that contains all the votes that were loaded
  144. vector<char> countyElectionResults::getVotes()
  145. {
  146.     return votes;
  147. }
  148.  
  149. // Prints the votes that were loaded
  150. void countyElectionResults::showVotes()
  151. {
  152.     for(int i=0; i < votes.size(); i++)
  153.     {
  154.         cout << votes[i] << endl;
  155.     }
  156. }
  157.  // Returns the total fieldite votes
  158. int countyElectionResults::getFielditeVotes()
  159. {
  160.     return fielditeVotes;
  161. }
  162. // Returns the total third party votes
  163. int countyElectionResults::getOtherVotes()
  164. {
  165.     return otherVotes;
  166. }
  167.  
  168. // Returns the total nullifier votes
  169. int countyElectionResults::getNullifierVotes()
  170. {
  171.     return nullifierVotes;
  172. }
  173. // Returns the total number of votes
  174. int countyElectionResults::getTotalVotes()
  175. {
  176.     int totalVotes = 0;
  177.     totalVotes = nullifierVotes + fielditeVotes + otherVotes;
  178.     return totalVotes;
  179. }
  180.  
  181. //shows the votes of each county listed in the program
  182. void stateElectionResults::showAllCountyResults()
  183. {
  184.     int FTotal = 0;
  185.     int NTotal = 0;
  186.     int OTotal = 0;
  187.     cout << "County Name" << setw(20) << "Fieldite Votes" << setw(20) << "Nullifier Votes" << setw(20) << "Other Votes" << endl;
  188.     //for loop to go through each county in the vector to return the votes
  189.     for(int i=0; i < stateResults.size();i++)
  190.     {
  191.         cout << stateResults[i].getCountyName() << setw(20) << stateResults[i].getFielditeVotes() << setw(20) << stateResults[i].getNullifierVotes() << setw(20) << stateResults[i].getOtherVotes() << endl;
  192.     }
  193.     //for loop to go through each county in the vector to return the totals
  194.     for(int i=0; i < stateResults.size(); i++)
  195.     {
  196.     FTotal = FTotal + stateResults[i].getFielditeVotes();
  197.     NTotal = NTotal + stateResults[i].getNullifierVotes();
  198.     OTotal = OTotal + stateResults[i].getOtherVotes();
  199.     }
  200.     cout << "Total: " << setw(20) << FTotal << setw(20) << NTotal << setw(20) << OTotal << endl;
  201. }
  202.  
  203. //searches a county a shows the results for that county
  204. void stateElectionResults::showCountyResults(string filename)
  205. {
  206.     string selection;
  207.     cout << "Please enter the county name: ";
  208.     cin >> selection;
  209.     for(int i=0; i < stateResults.size();i++)
  210.     {
  211.         //if there is an equal county name, then it shows the results
  212.         if(stateResults[i].getCountyName() == selection)
  213.         {
  214.             cout << endl << stateResults[i].getCountyName() << setw(20) << stateResults[i].getFielditeVotes() << setw(20) << stateResults[i].getNullifierVotes() << setw(20) << stateResults[i].getOtherVotes() << endl;
  215.             break;
  216.         }
  217.     }
  218. }
  219.  
  220. //lists all of the counties in the program on the screen       
  221. void stateElectionResults::listCounties()
  222. {
  223.     cout << endl << endl;
  224.     cout << "COUNTIES" << endl;
  225.     for(int i=0; i < stateResults.size();i++)
  226.     {
  227.         cout << stateResults[i].getCountyName() << endl;
  228.     }
  229. }
  230.  
  231. //shows each actual vote for a specificed county
  232. void stateElectionResults::showCountyVotes(string userinput)
  233. {
  234.     string selection;
  235.     cout << "Please enter the county name: ";
  236.     cin >> selection;
  237.     for(int i=0; i < stateResults.size();i++)
  238.     {
  239.         if(stateResults[i].getCountyName() == selection)
  240.         {
  241.         //makes a temp vector to store the votes of the found county's votes
  242.             vector<char> temp;
  243.             temp = stateResults[i].getVotes();
  244.             //prints those votes to the screen
  245.             for(int i=0; i < temp.size();i++)
  246.             {
  247.                 cout << temp[i] << endl;
  248.             }
  249.             return;
  250.         }
  251.     }
  252. }
  253.  
  254. // (1) Asks the user for a file name, then (2) loads that file into
  255. // a countyElectionObject from that file and (3) places it into a vector
  256. void stateElectionResults::addCountyFile(){
  257.    
  258.     string filename;
  259.     countyElectionResults county_results;
  260.  
  261.     cout << "Enter the county file to process: "; // 1.
  262.     //allows for a space in the file name
  263.     getline(cin, filename);
  264.  
  265.     if(!county_results.loadFile(filename)){ // 2.
  266.         // loading file didn't work
  267.         cout << "File not found: " << filename << endl << endl;
  268.         return;
  269.     }
  270.  
  271.     // loading file worked, so push the object onto the vector
  272.     stateResults.push_back(county_results); // 3.
  273.    
  274.     return;
  275.  
  276. }
  277. //function to get the menu choice of the user
  278. char get_menu_choice()
  279. {
  280.     cout << endl << endl << endl;
  281.     cout << "A: Add a county file" << endl;
  282.     cout << "L: List counties" << endl;
  283.     cout << "R: show all results" << endl;
  284.     cout << "C: show single county result" << endl;
  285.     cout << "V: verify single county votes" << endl;
  286.     cout << "Q: quit" << endl << endl << endl << endl;
  287.  
  288.     cout << "Your choice: ";
  289.     char selection;
  290.     cin >> selection;
  291.     //fixes the error of adding extra characters to the menu choice
  292.     cin.ignore(500,'\n');
  293.     return selection;
  294. }
  295.  
  296. int main(){
  297.  
  298. stateElectionResults S1;
  299. char selection;
  300. string filename;
  301. do
  302.     {
  303.         //this sets the user choice to a variable, which is used for our if statement
  304.         selection = get_menu_choice();
  305.        
  306.     if(selection == 'a' || selection == 'A')
  307.     {
  308.         S1.addCountyFile();
  309.     }
  310.        
  311.     if(selection == 'R' || selection == 'r')
  312.     {
  313.         S1.showAllCountyResults();
  314.     }      
  315.     if(selection == 'C' || selection == 'c')
  316.     {
  317.         S1.showCountyResults(filename);
  318.     }
  319.     if(selection == 'L' || selection == 'l')
  320.     {
  321.         S1.listCounties();
  322.     }
  323.     if(selection == 'V' || selection == 'v')
  324.     {
  325.         S1.showCountyVotes(filename);
  326.     }
  327.        
  328.     }while((selection != 'Q' && selection != 'q'));
  329.     return 0;
  330.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement