Advertisement
aaronvan

VC++

Feb 15th, 2019
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. #include <vector>
  5. #include <string>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. map<string, double> gerrymanderingRatios(const string &file);
  11. void printMap(map<string, double>);
  12.  
  13. int main() {
  14.     cout << "output should be something like: {\"D\":1.3, \"R\":0.742857}" << endl;
  15.     printMap(gerrymanderingRatios("data.txt"));
  16.     cout << endl;
  17.     system("pause");
  18.     return EXIT_SUCCESS;
  19. }
  20.  
  21. void printMap(map<string, double> in) {
  22.     map<string, double>::iterator dit;
  23.     for (pair<string, double> p : in) {
  24.       cout << '{' << p.first << ":" << p.second << "} ";
  25.     }
  26. }
  27.  
  28. map<string, double> gerrymanderingRatios(const string &file) {
  29.     //countMap<string, int> {D:14, R:13}
  30.     map<string, int> countMap;          // party : votes  
  31.  
  32.     //districtWinner <string, string>{1:D, 2:R, 2:R, 4:D, 5:D}
  33.     map<string, string> districtWinner; // district : winning party
  34.  
  35.     vector<string> districtVotes;       // temp storage for each district's votes
  36.    
  37.     map<string, double> gerryMap;       // party : ratio (return)
  38.     ifstream file_in(file, ios::in);
  39.     double totalVotes = 0.0;
  40.     double districtCounter = 0.0;
  41.     if (file_in) {
  42.         string line, districtName, partyVote, majorityVote;
  43.         while (file_in >> districtName, getline(file_in, line)) {
  44.             stringstream ss(line);
  45.             while (ss >> partyVote) {
  46.                 try {
  47.                     ++countMap[partyVote];
  48.                 } catch (const out_of_range& oor) {     // if key not present,
  49.                     countMap[partyVote] = 1;            // adds and begin counting
  50.                 }
  51.                 districtVotes.push_back(partyVote);
  52.                 ++totalVotes;
  53.             }
  54.             // iterate over districtVotes, insert majority party into districtWinner
  55.             for (size_t i = 0; i < districtVotes.size() - 1; ++i) {
  56.                 string winner = districtVotes[i];
  57.                 int counter = 0;
  58.                 int maxCounter = 0;
  59.                 if (districtVotes[i + 1] == districtVotes[i]) {
  60.                     winner = districtVotes[i + 1];
  61.                     ++counter;
  62.                 }
  63.                 else {
  64.                     if (counter > maxCounter)
  65.                         counter = maxCounter;
  66.                     majorityVote = districtVotes[maxCounter];
  67.                     counter = 0;
  68.                 }
  69.                 districtWinner[districtName] = majorityVote;
  70.             }
  71.             districtVotes.clear();
  72.             ++districtCounter;
  73.         }
  74.     }
  75.     file_in.close();
  76.    
  77.     map<string, int>::iterator voteCounter = countMap.begin(); // countMap
  78.     for (; voteCounter != countMap.end(); ++voteCounter) {
  79.         map<string, string>::iterator partyCounter = districtWinner.begin(); // districtWinner
  80.         int districtsWon = 0;
  81.         for (; partyCounter != districtWinner.end(); ++partyCounter) {
  82.             if ((*voteCounter).first == (*partyCounter).second)
  83.                 ++districtsWon;
  84.         }
  85.         double percentOfVotes = (*voteCounter).second / totalVotes; // total votes / # of party votes
  86.         double percentDistrictsWon = districtsWon / districtCounter; // districts won by party / # of districts
  87.         double ratio = percentDistrictsWon / percentOfVotes;
  88.         gerryMap[(*voteCounter).first] = ratio;
  89.     }
  90.     return gerryMap;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement