This repository was archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.cpp
More file actions
468 lines (443 loc) · 17.3 KB
/
Copy pathfrontend.cpp
File metadata and controls
468 lines (443 loc) · 17.3 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* frontend.cpp
* VMF
*
* Created by bdm on 6/24/10.
* Copyright 2010 Rochester Institute of Technology. All rights reserved.
*
*/
#include "frontend.h"
using namespace std;
// main
int main( int argc, char** argv ) {
srand( time(NULL) );
set<string> candidates;
cout << "How many candidates are in the election? ";
int count;
cin >> count;
if( count <= 1 || !cin ) {
cerr << "Error, enter a number >= 1.\n";
exit(1);
}
cout << "Enter a candidate's name, hit enter. Do this " << count << " time(s).\n";
string in;
while( count-- > 0 ) {
cin >> in;
if( !cin.good() ) {
++count;
cin.clear();
cin.sync();
//ERROR CONDITION
} else {
int initial = candidates.size();
candidates.insert( in );
int final = candidates.size();
if( final != initial + 1 ) {
//the user attempted to create two candidates of the same name...
++count;
}
}
}
if( !cin.good() ) {
count++;
//Error condition
cin.clear();
cin.sync();
}
if( candidates.size() < 2 ) {
cerr << "ERROR: At least two candidates needed for an election.\n";
exit(1);
}
//cout << "Rational systems: \n-Condorcet \n-Position Score Systems: \n\t1. Plurality\n\t2. Veto\n\t3. Borda\n";
//cout << "Irrational systems: \n-Copeland\n";
//PositionalScoreElection*
RationalElection* e = new Borda( candidates );
char c = 0;
while( c != 'q' && c != 'Q' ) {
cout << "\nWhat would you like to do?\n" <<
"\t1. Add Vote\n" <<
"\t\tV. Add Random Votes\n" <<
"\t2. Remove Vote\n" <<
"\t3. List Votes\n" <<
"\t4. Add Candidate\n" <<
"\t5. Remove Candidate\n" <<
"\tR. Run Election\n" <<
"\tC. Change Election\n";
if( dynamic_cast<PositionalScoreElection*> (e) ) {
cout << "\tM. Manipulate with Greedy Algorithm\n";
}
cout << "\tX. Clear the votes in the election.\n" <<
"\tQ. Quit\n" <<
"Enter desired option: ";
cout.flush();
#ifndef _WIN32
set_conio_terminal_mode();
#endif
while( !kbhit() ) {
usleep(100000);
}
//while( kbhit() ) {
//User entered multiple keys at once. We want to consume all the keys
//before moving on.
c = getch();
//}
#ifndef _WIN32
reset_terminal_mode();
#endif
cout << "\n";
switch (c) {
case '1':
{
RationalVote vote = create_vote(candidates);
e->add_vote( vote );
vector<string>::iterator j;
cout << "The following vote was added to the election: " << vote << "\n";
}
break;
case 'V':
case 'v':
{
int n;
cout << "How many random votes do you want to add: ";
cin >> n;
if ( n >= 1 ) {
vector<RationalVote> votes;
for( int i = 0; i < n; i++ ) {
vector<string> remaining( candidates.begin(), candidates.end() );
vector<string> vote;
while( remaining.size() ) {
int index = rand() % remaining.size();
vote.push_back( *(remaining.begin() + index) );
remaining.erase( remaining.begin() + index );
}
votes.push_back( vote );
}
cout << "The following votes were added to the election:\n";
print_votes( votes );
vector<RationalVote>::iterator i = votes.begin();
while ( i != votes.end() ) {
e->add_vote( *i );
++i;
}
}
}
break;
case '2':
{
cout << "Votes currently in the election:\n";
multiset<RationalVote> votes = e->get_votes();
print_votes( votes );
if( e->remove_vote( create_vote(candidates) ) ) {
cout << "Vote successfully removed.\n";
}
else {
cerr << "ERROR: Vote could not be removed.\n";
}
}
break;
case '3':
{
cout << "Votes currently in the election:\n";
print_votes( e->get_votes() );
}
break;
case '4':
cout << "Enter the name of a candidate previously removed from the election: ";
cout.flush();
cin >> in;
e->add_candidate( in );
break;
case '5':
cout << "Enter the name of a candidate in the election to remove them or anything else to go back: ";
cout.flush();
cin >> in;
e->remove_candidate( in );
break;
case 'R':
case 'r':
{
if( !e->votes_counted() ) {
e->count_votes();
}
PositionalScoreElection* p;
if( p = dynamic_cast<PositionalScoreElection*> (e) ) {
map<string, int> r = p->get_vote_count();
map<string,int>::iterator k = r.begin();
while( k != r.end() ) {
cout << k->first << ": " << k->second << " votes.\n";
++k;
}
} else {
CondorcetElection* c = dynamic_cast<CondorcetElection*> (e);
if( c ) {
map<pair<string,string>,int> m = c->get_vote_count();
map<pair<string,string>,int>::iterator i = m.begin();
while( i != m.end() ) {
cout << "(" << i->first.first << "," << i->first.second << "): " << i->second << endl;
++i;
}
}
}
set<string> winners = e->get_winners();
set<string>::iterator l = winners.begin();
cout<< "\nWinners:";
while( l != winners.end() ) {
cout << " " << *l;
l++;
}
cout << endl;
}
break;
case 'C':
case 'c':
{
cout << "Which election type would you like to change the election to?\n";
cout << "\t1. Plurality\n\t2. Veto\n\t3. Borda\n\t4. (n,n-1,...,1,0,...,0)\n\t5. (a1,a2,...,an,0,...,0)\n";
cout << "Enter the number corresponding to the desired election.";
//TODO: Error check input
#ifndef _WIN32
set_conio_terminal_mode();
#endif
while( !kbhit() ) {
usleep( 100000 );
}
char k = getch();
#ifndef _WIN32
reset_terminal_mode();
#endif
multiset<RationalVote> v = e->get_votes();
switch( k ) {
case '1':
delete e;
e = new Plurality( candidates, v );
break;
case '2':
delete e;
e = new Veto( candidates, v );
break;
case '3':
delete e;
e = new Borda( candidates, v );
break;
case '4':
{
int n;
delete e;
e = new Countdown( candidates, v );
cout << "From what number do you want to count down from: ";
cin >> n;
// TODO: Error trap input.
if( n > 0 ) {
dynamic_cast<Countdown*>(e)->change_countdown( n );
}
break;
}
case '5':
{
vector<int> scores;
int n;
delete e;
cout << "Enter numbers to put into the scoring vector and -1 to finish: ";
while ( cin >> n && n > 0 ) {
scores.push_back( n );
}
e = new ScoringVector( candidates, v );
dynamic_cast<ScoringVector*>(e)->set_scoring_vector( scores );
scores = dynamic_cast<ScoringVector*>(e)->get_scoring_vector();
vector<int>::iterator i = scores.begin();
while( i != scores.end() ) {
cout << *i << " ";
++i;
}
cout << "\n";
break;
}
case '6':
{
delete e;
e = new CondorcetElection( candidates, v );
}
default:
cout << "Invalid choice, election system unchanged.";
break;
}
}
break;
case 'M':
case 'm':
if ( dynamic_cast<PositionalScoreElection*> (e) )
{
PositionalScoreElection* p = dynamic_cast<PositionalScoreElection*> (e);
cout << "Unique winner problem [Y/n]: ";
cout.flush();
// unique winner problem
#ifndef _WIN32
set_conio_terminal_mode();
#endif
while( !kbhit ) {
usleep( 100000 );
}
char k = getch();
cout << "\n";
int unique = ( k != 'n' || k != 'N' );
cout << "Would you like to limit the number of manipulators [y/N]: ";
cout.flush();
while( !kbhit ) {
usleep( 100000 );
}
k = getch();
cout << "\n";
#ifndef _WIN32
reset_terminal_mode();
#endif
int limit = 0;
if( k == 'y' || k == 'Y' ) {
while( limit < 1 ) {
cout << "Enter the number of manipulators: ";
cout.flush();
if( !( cin >> limit ) || limit < 1 ) {
cerr << "ERROR: A positive integer is needed.\n";
if( !cin ) {
cin.clear();
cin.sync();
}
}
}
}
cout << "Enter the name of the preferred candidate: ";
cout.flush();
cin >> in;
// TODO: Make this not crap out when the candidate is removed from the election.
vector<RationalVote> manipulators;
if( candidates.find( in ) != candidates.end() ) {
map<string, int> scores = p->get_vote_count();
set<string> winners = p->get_winners();
// This next line is kind of messy.
// Basically it loops while the candidate is not in the winner set (or not the unique winner if the unique flag is set) AND
// if the number of manipulators is less than the limit of the manipulators if such a limit was imposed.
while( (winners.find( in ) == winners.end() || (unique && winners.size() > 1)) && (!limit || manipulators.size() < limit ) ) {
set<string> remaining( candidates );
remaining.erase( in );
vector<string> vote;
vote.push_back( in );
int position = 1;
map<string,int>::iterator sc = scores.begin();
while( p->position_score( position++ ) > 0 ) {
map<string,int>::iterator i = scores.begin();
int min = -1;
string target_min_candidate;
string target_max_candidate = "";
while( i != scores.end() ) {
if( remaining.find( i->first ) != remaining.end() ) {
if( i->second < min || min == -1 ) {
min = i->second;
target_min_candidate = i->first;
}
}
++i;
}
vote.push_back(target_min_candidate);
remaining.erase( target_min_candidate );
}
vote.insert( vote.end(), remaining.begin(), remaining.end() );
e->add_vote(vote);
manipulators.push_back( vote );
scores = p->get_vote_count();
winners = e->get_winners();
}
cout << "Manipulation used " << manipulators.size() << " manipulators.\n";
cout << "Added votes:\n";
print_votes( manipulators );
}
}
break;
case 'x':
case 'X':
cout << "Erasing all votes in the election.\n";
e->erase_votes();
break;
default:
break;
}
}
return 0;
}
// print_votes
void print_votes( const vector<RationalVote>& votes ) {
vector<RationalVote>::const_iterator i = votes.begin();
map<RationalVote,int>::iterator j;
map<RationalVote,int> vote_map;
while( i != votes.end () ) {
vote_map[*i]++;
++i;
}
j = vote_map.begin();
while( j != vote_map.end() ) {
cout << j->first << ": " << j->second << "\n";
++j;
}
}
// print_votes
void print_votes( const multiset<RationalVote>& votes ) {
vector<RationalVote> vect( votes.begin(), votes.end() );
print_votes( vect );
}
// create_vote
RationalVote create_vote( set<string> candidates ) {
#ifndef _WIN32
reset_terminal_mode();
#endif
string in;
set<string> remaining( candidates );
vector<string> vote;
while( remaining.size() ) {
cout << "Remaining candidates to place in vote: ";
set<string>::iterator i = remaining.begin();
while( i != remaining.end() ) {
cout << *i;
++i;
if( i != remaining.end() ) {
cout << ", ";
}
else {
cout << "\n";
}
}
vector<string>::iterator j;
if( vote.size() > 0 ) {
j = vote.begin();
while( j != vote.end() ) {
cout << *j;
++j;
if( j != vote.end() ) {
cout << " > ";
}
else {
cout << "\n";
}
}
}
cout << "Select a candidate to add to the vote or a candidate in the vote to remove it: ";
cout.flush();
cin >> in;
i = remaining.find( in );
if( i != remaining.end() ) {
remaining.erase( i );
vote.push_back( in );
}
else {
j = vote.begin();
while( j != vote.end() && *j != in ) {
++j;
}
if( j != vote.end() ) {
vote.erase( j );
remaining.insert( in );
}
}
}
#ifndef _WIN32
set_conio_terminal_mode();
#endif
return vote;
}