Пример #1
0
CandidateSet CandidateSet::getCandidateTrees(double score) {
    CandidateSet res;
    for (CandidateSet::iterator it = begin(); it != end(); it++) {
        if (abs(it->first - score) < 0.1) {
            res.insert(*it);
        }
    }
    return res;
}
Пример #2
0
CandidateSet CandidateSet::getBestCandidateTrees(int numTrees) {
    CandidateSet res;
    if (numTrees >= size() || numTrees == 0)
        numTrees = (int) size();

    for (reverse_iterator rit = rbegin(); rit != rend() && numTrees > 0; rit++, numTrees--) {
        res.insert(*rit);
    }
    return res;
}
Пример #3
0
//Print the message followed by all the entries in the CandidateTally, in
//  the order specified by has_higher_priority: i is printed before j, if
//  has_higher_priority(i,j) returns true: sometimes alphabetically by candidate,
//  other times by decreasing votes for the candidate.
//Use a "->" to separate the candidate name from the number of votes they
//  received.
void print_tally(std::string message, const CandidateTally& tally, bool (*has_higher_priority)(const TallyEntry& i,const TallyEntry& j)) {
	CandidateSet candidateSet;
	for (const auto& kv : tally) {
		candidateSet.insert(kv.first);
	}
	TallyEntryPQ tallyPQ(*has_higher_priority);
	tallyPQ.enqueue_all(tally);
	std::cout << message << ": still in election = " << candidateSet << std::endl;

	for (const TallyEntry& tallyEntry : tallyPQ) {
		std::cout << "  " << tallyEntry.first << " -> " << tallyEntry.second << std::endl;
	}
	std::cout << std::endl;
}
Пример #4
0
//Prompt the user for a file, create a voter preference Map, and print it.
//Determine the Set of all the candidates in the election, from this Map.
//Repeatedly evaluate the ballot based on the candidates (still) in the
//  election, printing the vote count (tally) two ways: with the candidates
//  (a) shown alphabetically increasing and (b) shown with the vote count
//  decreasing (candidates with equal vote counts are shown alphabetically
//  increasing); from this tally, compute which candidates remain in the
//  election: all candidates receiving more than the minimum number of votes;
//  continue this process until there are less than 2 candidates.
//Print the final result: there may 1 candidate left (the winner) or 0 left
//   (no winner).
int main() {
  try {
	  std::ifstream text_file;
	  ics::safe_open(text_file,"Enter the name of a file with voter preferences","votepref1.txt");
	  std::cout << std::endl;

	  const Preferences prefs = read_voter_preferences(text_file);
	  print_voter_preferences(prefs);
	  std::cout << std::endl;

	  CandidateSet candidateSet;
	  for (const auto& kv : prefs) {
		  for (const std::string& candidate : kv.second) {
			  candidateSet.insert(candidate);
		  }
	  }

	  CandidateTally tally = evaluate_ballot(prefs, candidateSet);
	  int ballot = 1;

	  while (candidateSet.size() > 1) {
		  std::stringstream ss;
		  ss << ballot++;
		  print_tally("Vote count on ballot #" + ss.str() +
				  " with candidates in alphabetical order", tally, alphabetic_gt);
		  print_tally("Vote count on ballot #" + ss.str() +
				  " with candidates in numerical order", tally, numeric_gt);

		  candidateSet = remaining_candidates(tally);
		  tally = evaluate_ballot(prefs, candidateSet);
	  }

	  if (candidateSet.size() == 0) {
		  std::cout << "No winner" << std::endl;
	  } else {
		  std::string winner;
		  for (const std::string& candidate : candidateSet) {
			  winner += candidate + " and ";
		  }
		  std::cout << "Winner is " << winner.substr(0, winner.length()-5) << std::endl;
	  }

  } catch (ics::IcsError& e) {
    std::cout << e.what() << std::endl;
  }
  return 0;
}
Пример #5
0
//Return the Set of candidates who are still in the election, based on the
//  tally of votes: compute the minimum number of votes and return a Set of
//  all candidates receiving more than that minimum; if all candidates
//  receive the same number of votes (that would be the minimum), the empty
//  Set is returned.
CandidateSet remaining_candidates(const CandidateTally& tally) {
	CandidateSet result;
	if (!tally.empty()) {
		int min = -1;

		for (const TallyEntry& entry : tally) {
				if (min == -1 || entry.second < min) {
					min = entry.second;
				}
		}
		for (const TallyEntry& entry : tally) {
			if (entry.second > min) {
				result.insert(entry.first);
			}
		}
	}
	return result;
}