//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; }
void Wordrec::save_summary(inT32 elapsed_time) { #ifndef SECURE_NAMES STRING outfilename; FILE *f; int x; int total; outfilename = imagefile + ".sta"; f = open_file (outfilename.string(), "w"); fprintf (f, INT32FORMAT " seconds elapsed\n", elapsed_time); fprintf (f, "\n"); fprintf (f, "%d characters\n", character_count); fprintf (f, "%d words\n", word_count); fprintf (f, "\n"); fprintf (f, "%d permutations performed\n", permutation_count); fprintf (f, "%d characters classified\n", chars_classified); fprintf (f, "%4.0f%% classification overhead\n", (float) chars_classified / character_count * 100.0 - 100.0); fprintf (f, "\n"); fprintf (f, "%d words chopped (pass 1) ", words_chopped1); fprintf (f, " (%0.0f%%)\n", (float) words_chopped1 / word_count * 100); fprintf (f, "%d chops performed\n", chops_performed1); fprintf (f, "%d chops attempted\n", chops_attempted1); fprintf (f, "\n"); fprintf (f, "%d words joined (pass 1)", words_segmented1); fprintf (f, " (%0.0f%%)\n", (float) words_segmented1 / word_count * 100); fprintf (f, "%d segmentation states\n", segmentation_states1); fprintf (f, "%d segmentations timed out\n", states_timed_out1); fprintf (f, "\n"); fprintf (f, "%d words chopped (pass 2) ", words_chopped2); fprintf (f, " (%0.0f%%)\n", (float) words_chopped2 / word_count * 100); fprintf (f, "%d chops performed\n", chops_performed2); fprintf (f, "%d chops attempted\n", chops_attempted2); fprintf (f, "\n"); fprintf (f, "%d words joined (pass 2)", words_segmented2); fprintf (f, " (%0.0f%%)\n", (float) words_segmented2 / word_count * 100); fprintf (f, "%d segmentation states\n", segmentation_states2); fprintf (f, "%d segmentations timed out\n", states_timed_out2); fprintf (f, "\n"); total = 0; iterate_tally (states_before_best, x) total += (tally_entry (states_before_best, x) * x); fprintf (f, "segmentations (before best) = %d\n", total); if (total != 0.0) fprintf (f, "%4.0f%% segmentation overhead\n", (float) (segmentation_states1 + segmentation_states2) / total * 100.0 - 100.0); fprintf (f, "\n"); print_tally (f, "segmentations (before best)", states_before_best); iterate_tally (best_certainties[0], x) cprintf ("best certainty of %8.4f = %4d %4d\n", x * CERTAINTY_BUCKET_SIZE, tally_entry (best_certainties[0], x), tally_entry (best_certainties[1], x)); PrintIntMatcherStats(f); dj_statistics(f); fclose(f); #endif }