-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdowning-Voting.c++
292 lines (237 loc) · 7.6 KB
/
downing-Voting.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdio.h>
#include <iostream>
#include <string>
#include <cassert> // assert
#include <vector>
#include <algorithm>
using namespace std;
class Ballot{
public:
int top;
vector<int> votes;
Ballot() {top = 0;}
int getVote(){
assert(top >= 0);
if(top >= votes.size()){ return -1; } else { return votes[top]; } }
};
class Candidate{
public:
int voteCount;
string name;
vector<Ballot> ballots;
Candidate() {voteCount = 0; }
void addVote(Ballot& ballot);
};
class BallotBox{
public:
int numberOfBallots, numOfValidCandidates;
vector<Candidate> candidates;
BallotBox() {numberOfBallots = 0; numOfValidCandidates = 0;}
void addCandidate(string& name);
void castBallot(string ballot);
vector<int> ballotStringtoVector(string ballot, int size);
int isMajority(int majority);
vector<Candidate*> getLastPlace();
void sortLosers(vector<Candidate*>& losers);
};
vector<string> voting_findWinner(BallotBox&);
void voting_read (istream&, BallotBox&);
void voting_print (ostream&, vector<string>&, bool);
void voting_holdElection(istream&, ostream&, bool);
void Candidate::addVote(Ballot& ballot){
voteCount++;
if(ballot.top < ballot.votes.size()){
ballot.top += 1;
ballots.push_back(ballot);
}
}
///////////////////////////////////////////////////////////////
void BallotBox::addCandidate(string& name){
Candidate candidate;
candidate.name = name;
candidates.push_back(candidate);
numOfValidCandidates++;
assert(candidates.size() <= 20);
}
void BallotBox::castBallot(string ballotString){
numberOfBallots++;
assert(numberOfBallots <= 1000);
Ballot ballot;
ballot.votes = ballotStringtoVector(ballotString, candidates.size());
int firstChoice = ballot.getVote();
assert(firstChoice-1 >= 0);
candidates[firstChoice-1].addVote(ballot);
}
vector<int> BallotBox::ballotStringtoVector(string ballot, int size){
vector<int> ballotVector;
string temp = "";
string test = "";
for(int i =0; i < ballot.length(); i+=2){
temp = ballot.substr(i+1, 1);
if(temp == " "){
test = ballot.substr(i, 1);
}
else{
test = ballot.substr(i, 2);
i++;
}
ballotVector.push_back(atoi(test.c_str()));
}
return ballotVector;
}
int BallotBox::isMajority(int majority){
int count = 0;
for(vector<Candidate>::iterator it = candidates.begin(); it != candidates.end(); ++it){
if((*it).voteCount >= majority){
return count;
}
count++;
}
return -1;
}
vector<Candidate*> BallotBox::getLastPlace(){
vector<Candidate*> losersReturn;
vector<int> losersIndex;
int smallestVote = numberOfBallots + 1;
Candidate* currentCandidate;
int count = 0;
for(vector<Candidate>::iterator it = candidates.begin(); it != candidates.end(); ++it){
currentCandidate = &(*it);
//want to ignore all invalid candidates
if( currentCandidate->voteCount != -1){
if(currentCandidate->voteCount < smallestVote){
losersIndex.clear();
smallestVote = currentCandidate->voteCount;
losersIndex.push_back(count);
}
else if(currentCandidate->voteCount == smallestVote){
losersIndex.push_back(count);
}
}
count++;
}
//get all the losers and set their votes to -1 making them invalid
for(unsigned int i = 0; i< losersIndex.size(); i++){
assert(losersIndex[i] >= 0);
currentCandidate = &candidates[losersIndex[i]];
//candidates[losersIndex[i]]->voteCount = -1;
currentCandidate->voteCount = -1;
losersReturn.push_back(currentCandidate);
}
return losersReturn;
}
void BallotBox::sortLosers(vector<Candidate*>& losers){
Candidate* loser;
Candidate nextCandidate;
vector<Ballot> ballots;
Ballot ballot;
int nextChoice;
for(vector<Candidate*>::iterator it = losers.begin(); it != losers.end(); ++it){
loser = *it;
ballots = loser->ballots;
for(vector<Ballot>::iterator it = ballots.begin(); it != ballots.end(); ++it){
// for(unsigned int j = 0; j < ballots.size(); j++){
ballot = *it;
do{
nextChoice = ballot.getVote();
nextCandidate = candidates[nextChoice -1];
if(nextCandidate.voteCount != -1){
candidates[nextChoice - 1].addVote(ballot);
}
else{
assert(nextCandidate.voteCount == -1);
ballot.top += 1; //should be able to delete this
}
}while(nextCandidate.voteCount == -1 && ballot.top < ballot.votes.size());
}
numOfValidCandidates--;
}
}
///////////////////////////////////////////////////////////////////////
vector<string> voting_findWinner(BallotBox& box){
bool winnerFound = false;
vector<string> winners;
int majority = (box.numberOfBallots /2) + 1;
assert((box.numberOfBallots - majority) < majority);
while(!winnerFound){
int isMajority = box.isMajority(majority);
if(isMajority >= 0){
winners.push_back(box.candidates[isMajority].name);
winnerFound = true;
}
else{
assert(isMajority == -1);
vector<Candidate*> losers = box.getLastPlace();
if(losers.size() == box.numOfValidCandidates){
for(vector<Candidate*>::iterator it = losers.begin(); it != losers.end(); ++it){
winners.push_back((*it)->name);
}
winnerFound = true;
}
else{
box.sortLosers(losers);
}
}
}
return winners;
}
//TODO on suceed needs to return the ballot object
//TODO on fail needs to return null
void voting_read (istream& reader, BallotBox& box) {
int numberOfCandidates;
reader >> numberOfCandidates;
assert(reader);
string candidateName;
getline (reader, candidateName); // this is to skip to the next line sincde reader >> did not
for(int i =0; i < numberOfCandidates; i++){
getline(reader, candidateName);
box.addCandidate(candidateName);
assert(reader);
}
string ballot = "";
while(true){
getline(reader, ballot);
if(ballot.compare("") != 0){
box.castBallot(ballot);
}
else if(ballot.compare("") == 0){
break;
}
}
//cout << "------voting_read" << endl;
}
// -------------
// voting_print
// -------------
void voting_print (ostream& writer, vector<string>& winnerNames, bool lastOne) {
for(vector<string>::iterator it = winnerNames.begin(); it != winnerNames.end(); ++it){
writer << *it;
//if(it == !lastOne)
writer << endl;
}
if(!lastOne) writer << endl;
}
void voting_holdElection(istream& reader, ostream& writer, bool lastOne){
BallotBox box;
vector<string> winners;
//cout << "------- about to read" << endl;
voting_read(reader, box);
//cout << "------- done with read" << endl;
winners = voting_findWinner(box);
voting_print(writer, winners, lastOne);
}
int main () {
////cout << "------main" << endl;
using namespace std;
int numberOfElections;
string blankLine;
////cout << "------- start" << endl;
cin >> numberOfElections;
getline(cin, blankLine);
getline(cin, blankLine);
for(int i=0; i< numberOfElections; i++){
////cout << "------- election" << endl;
voting_holdElection(cin, cout, !(i < numberOfElections -1));
}
return 0;
}