Last active
March 18, 2019 09:25
-
-
Save tommyip/e33a736b3a688b6192d9d689710f102e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Lab 6: Voting - An election system | |
* File: lab6_skeleton.cpp | |
*/ | |
#include<iostream> | |
using namespace std; | |
const int MAX_SIZE = 50; | |
const int MAX_STRING_LENGTH = 20; | |
// structure definition of Participant | |
struct Participant | |
{ | |
int id; | |
char name[MAX_STRING_LENGTH]; | |
bool hasVoted; // true if voter has voted, false otherwise | |
int numVotes; // number of votes received (if the participant is a candidate | |
}; | |
// structure definition of Election | |
struct Election | |
{ | |
Participant participants[MAX_SIZE]; | |
int numCandidates; | |
int numVoters; | |
// the candidates will occupy the first numCandidates elements in the participants array, | |
// while the other voters will occupy the next numVoters elements in the participants array | |
}; | |
/* Print the list of candidates | |
* Parameter: | |
* - election: the Election struct object | |
*/ | |
void printCandidates(const Election &election) | |
{ | |
cout << "The list of candidates: " << endl; | |
for (int i=0; i<election.numCandidates; i++) | |
cout << "Candidate id: " << election.participants[i].id << "\tName:" << election.participants[i].name << endl; | |
} | |
/* Setup the election | |
* - initialize all the member variables of the Election object | |
* Parameters: | |
* - election: a Election struct object | |
* Note: | |
* - id for voter/candidate has to be unique | |
* - initialize all the member variables of the Participant elements | |
*/ | |
void setup(Election &election) | |
{ | |
cout << "Enter the details of the candidates: " << endl; | |
for (int i = 0; i < election.numCandidates; ++i) { | |
Participant p; | |
cout << "Candidate No. " << i + 1 << endl; | |
cout << "\tEnter his/her id: "; | |
cin >> p.id; | |
cout << "\tEnter Name: "; | |
cin >> p.name; | |
p.hasVoted = false; | |
p.numVotes = 0; | |
election.participants[i] = p; | |
} | |
cout << "Enter the details of the voters: " << endl; | |
for (int i = 0; i < election.numVoters; ++i) { | |
Participant p; | |
cout << "Voter No. " << (i + 1) + election.numCandidates << endl; | |
cout << "\tEnter his/her id: "; | |
cin >> p.id; | |
cout << "\tEnter Name: "; | |
cin >> p.name; | |
p.hasVoted = false; | |
p.numVotes = 0; | |
election.participants[i + election.numCandidates] = p; | |
} | |
} | |
/* Casting votes to some candidates by a specific voter given the voter id | |
* Parameters: | |
* - election: the Election struct object | |
* - voterId: the voter's id | |
* - numVotes: the number of votes the voter wants to cast | |
* Note: | |
* validates the following | |
* - voter id has to be existed | |
* - candidate id has to be existed | |
*/ | |
void voting(Election& election, int voterId, int numVotes) | |
{ | |
bool foundVoter = false; | |
for (Participant& p : election.participants) { | |
if (p.id == voterId) { | |
p.hasVoted = foundVoter = true; | |
break; | |
} | |
} | |
if (!foundVoter) { | |
cout << "Voter does not exist" << endl; | |
return; | |
} | |
for (int i = 1; i <= numVotes; ++i) { | |
cout << "Enter the candidate id you want to vote for: "; | |
int targetId; | |
cin >> targetId; | |
bool foundTarget = false; | |
for (int i = 0; i < election.numCandidates; ++i) { | |
if (election.participants[i].id == targetId) { | |
++election.participants[i].numVotes; | |
foundTarget = true; | |
break; | |
} | |
} | |
if (!foundTarget) { | |
cout << "Candidate not found" << endl; | |
return; | |
} | |
cout << "Vote " << i << " done." << endl; | |
} | |
} | |
/* Output the number of votes for each candidate | |
* and announce the winner by finding who has the maximum number of votes | |
* Parameters: | |
* - election: the Election struct object | |
* Note: if there is more than 1 candidate has the same number of maximum votes, | |
* announce it as a tie election. | |
*/ | |
void resultTallying(const Election &election) | |
{ | |
unsigned int totalVotes = 0; | |
int maxVotes = 0; | |
Participant winner = election.participants[0]; | |
for (int i = 0; i < election.numCandidates; ++i) { | |
const Participant& p = election.participants[i]; | |
cout << "The total number of votes for candidate " << p.id; | |
cout << ": " << p.numVotes << endl; | |
totalVotes += p.numVotes; | |
if (p.numVotes > maxVotes) { | |
maxVotes = p.numVotes; | |
winner = p; | |
} | |
} | |
cout << "The total number of votes for the whole election: " << totalVotes << endl; | |
cout << "The winner is: " << winner.id << endl; | |
} | |
// Main function for the election system | |
int main() | |
{ | |
// Create an election | |
Election election; | |
cout << "===============================" << endl; | |
cout << " Welcome to the voting system! " << endl; | |
cout << "===============================" << endl; | |
cout << "Preparing ... " << endl; | |
do { | |
cout << "Enter how many candidates will run for the election: \n"; | |
cin >> election.numCandidates; | |
cout << "Enter how many more voters: \n"; | |
cin >> election.numVoters; | |
} while (((election.numCandidates + election.numVoters) > MAX_SIZE) || (election.numCandidates <= 0)); | |
cout << "\n"; | |
// the setup phase | |
setup(election); | |
int currentId; | |
int numVotes = 0; | |
// the voting phase | |
cout << "Voting starts ..." << endl; | |
printCandidates(election); | |
char cmd; | |
//for (int i = 0; i < (election.numCandidates + election.numVoters); i++) | |
do { | |
cout << "Enter the voter id to start voting:\n"; | |
cin >> currentId; | |
do { | |
cout << "Enter the number of votes to be cast (<" << election.numCandidates+1 << ") :\n"; | |
cin >> numVotes; | |
} while (numVotes > election.numCandidates); | |
voting(election, currentId, numVotes); | |
cout << "Continue with the next voter? (y/n) "; | |
cin >> cmd; | |
} while ((cmd == 'y') || (cmd == 'Y')); | |
cout << "Tallying votes ..." << endl; | |
//the result-tallying phase | |
resultTallying(election); | |
cout << "End of the election!" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment