Advertisement
Guest User

Untitled

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