Advertisement
teknique

Untitled

Dec 4th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 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. countyElectionList::head = NULL;
  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.  
  240. // This method returns the number of elements on the list.
  241. // It is analogous to the size method in the vector class
  242. int countyElectionList::size(){
  243.  
  244.  
  245. }
  246.  
  247.  
  248. //This method returns true iff the list has no elements
  249. // It is analogous to the empty method in the vector class.
  250. bool countyElectionList::empty(){
  251.  
  252. }
  253.  
  254.  
  255.  
  256. class stateElectionResults {
  257.  
  258. public:
  259. void addCountyFile();
  260. void showAllCountyResults();
  261. void showCountyResults(string);
  262. void showCountyVotes(string);
  263. void listCounties();
  264.  
  265.  
  266. private:
  267. countyElectionList stateResults;
  268.  
  269. };
  270.  
  271.  
  272.  
  273. void stateElectionResults::addCountyFile(){
  274.  
  275. string countyFileName;
  276. int voteCount = 0;
  277.  
  278. cout << "Please enter the county file name to add:";
  279. getline(cin, countyFileName);
  280.  
  281. countyElectionResults * newResults = new (countyElectionResults);
  282. if (newResults->loadFile(countyFileName)){
  283. stateResults.push_back(newResults);
  284. }
  285.  
  286. }
  287.  
  288. void stateElectionResults::showAllCountyResults(){
  289.  
  290. int nullifierVotes = 0;
  291. int fielditeVotes = 0;
  292. int otherVotes = 0;
  293.  
  294. verticalSpace(5);
  295. cout << setw(25) << "County Name"
  296. << setw(15) << "Fieldite Votes"
  297. << setw(15) << "Nullifier Votes"
  298. << setw(15) << "Other Votes"
  299. << endl;
  300.  
  301. for (int i = 0; i < stateResults.size(); i++){
  302.  
  303. cout << stateResults.at(i) << endl;
  304.  
  305. nullifierVotes += stateResults.at(i).getNullifierVotes();
  306. fielditeVotes += stateResults.at(i).getFielditeVotes();
  307. otherVotes += stateResults.at(i).getOtherVotes();
  308. }
  309.  
  310. cout << setw(25) << "Total:"
  311. << setw(15) << fielditeVotes
  312. << setw(15) << nullifierVotes
  313. << setw(15) << otherVotes
  314. << endl;
  315. verticalSpace(5);
  316. }
  317.  
  318. void stateElectionResults::showCountyResults(string countyName){
  319.  
  320. verticalSpace(5);
  321. for (int i = 0; i < stateResults.size(); i++){
  322.  
  323. if (stateResults.at(i).getCountyName() == countyName){
  324. cout << stateResults.at(i) << endl;
  325. break;
  326. }
  327. }
  328. verticalSpace(5);
  329. }
  330.  
  331. void stateElectionResults::showCountyVotes(string countyName){
  332.  
  333. for (int i = 0; i < stateResults.size(); i++){
  334.  
  335. if (stateResults.at(i).getCountyName() == countyName){
  336. stateResults.at(i).showVotes();
  337. break;
  338. }
  339. }
  340. }
  341.  
  342. void stateElectionResults::listCounties(){
  343.  
  344. cout << setw(25) << "COUNTIES" << endl;
  345. for (int i = 0; i < stateResults.size(); i++){
  346. cout << setw(25) << stateResults.at(i).getCountyName() << endl;
  347. }
  348.  
  349. }
  350.  
  351.  
  352. int main(){
  353.  
  354. stateElectionResults theResults;
  355. bool done = false;
  356. char choice;
  357. string county;
  358.  
  359. do {
  360.  
  361. choice = getMenuChoice();
  362. switch (choice){
  363. case 'Q':
  364. case 'q':
  365. done = true;
  366. break;
  367. case 'A':
  368. case 'a':
  369. theResults.addCountyFile();
  370. break;
  371. case 'R':
  372. case 'r':
  373. theResults.showAllCountyResults();
  374. break;
  375. case 'L':
  376. case 'l':
  377. theResults.listCounties();
  378. break;
  379. case 'C':
  380. case 'c':
  381. cout << "Please enter the county name: ";
  382. getline(cin,county);
  383. theResults.showCountyResults(county);
  384. break;
  385. case 'V':
  386. case 'v':
  387. cout << "Please enter the county name: ";
  388. getline(cin,county);
  389. theResults.showCountyVotes(county);
  390. break;
  391. default:
  392. cout << "Unknown choice. Please try again" << endl;
  393. }
  394.  
  395.  
  396. } while (!done);
  397. }
  398.  
  399.  
  400.  
  401. // This main is for simple testing of the the countyElectionResults class
  402. // int main(){
  403.  
  404. // Make three county Election result objects
  405.  
  406. // countyElectionResults* lake = new countyElectionResults("Lake-co-gov.txt");
  407. // countyElectionResults* dover = new countyElectionResults("Dover-co-gov.txt");
  408. // countyElectionResults* clackamas = new countyElectionResults("Clackamas-co-gov.txt");
  409.  
  410.  
  411. // Link them together into a linked list
  412. // countyElectionResults* head = lake;
  413. // lake->setNextResult(dover);
  414. // dover->setNextResult(clackamas);
  415.  
  416. // Print out the elements of the linked list so that we can see if the
  417. // self-referential elements are working.
  418.  
  419. // cout << "Printing county names as: Lake Dover Clackamas" << endl;
  420. // cout << "If those print but then your program crashes, you" << endl;
  421. // cout << "probably forgot to set the nextResult pointer to null in the constructor" << endl;
  422. // cout << "--------------------------" << endl;
  423. // countyElectionResults* current = head;
  424. // while (current != NULL){
  425. // cout << current->getCountyName() << endl;
  426. // current = current->getNextResult();
  427. // }
  428. // cout << "--------------------------" << endl;
  429. // cout << "If this prints out, then the self-referential part looks good" << endl;
  430.  
  431. // }
  432.  
  433.  
  434. void verticalSpace(int space){
  435.  
  436. for (int i = 0; i < space; i++){
  437. cout << endl;
  438. }
  439.  
  440. }
  441.  
  442. char getMenuChoice(){
  443.  
  444. char choice;
  445. verticalSpace(5);
  446. cout << setw(45) << "A: Add a county file" << endl;
  447. cout << setw(45) << "L: List counties" << endl;
  448. cout << setw(45) << "R: show all results" << endl;
  449. cout << setw(45) << "C: show single county result" << endl;
  450. cout << setw(45) << "V: verify single county votes" << endl;
  451. cout << setw(45) << "Q: quit" << endl;
  452.  
  453. cout << "Your choice: ";
  454. cin >> choice;
  455. string trash;
  456. getline (cin,trash);
  457. verticalSpace(5);
  458. return choice;
  459.  
  460. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement