This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eurovision.cpp
371 lines (334 loc) · 10.4 KB
/
eurovision.cpp
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
#include <iostream>
#include "eurovision.h"
#include <stdexcept>
Participant::Participant(const string state, const string song,
const int timeLength, const string singer):
state_name(state), song_name(song), song_length(timeLength),
singer_name(singer), is_registered(false){}
const string Participant::state() const{
return state_name;
}
string Participant::song() const {
return song_name;
}
int Participant::timeLength() const {
return song_length;
}
string Participant::singer() const {
return singer_name;
}
bool Participant::isRegistered() const {
return is_registered;
}
void Participant::update(const string song, const int timeLength, const string singer) {
if (!isRegistered()) {
if (!song.empty()) {
song_name = song;
}
if (timeLength) {
song_length = timeLength;
}
if (!singer.empty()) {
singer_name = singer;
}
}
}
void Participant::updateRegistered(bool flag){
is_registered=flag;
}
ostream &operator<<(ostream &os, const Participant &p){
//[Australia/Song_Australia/180/Singer_Australia]
os << "[";
os << p.state() << "/";
os << p.song() << "/";
os << p.timeLength() << "/";
os << p.singer();
os << "]";
return os;
}
Voter::Voter(const string state, const VoterType type):
state_name(state),
type(type),
times_voted(0){}
Voter& Voter::operator++(){
++times_voted;
return *this;
}
const string Voter::state() const{
return state_name;
}
VoterType Voter::voterType() const{
return type;
}
int Voter::timesOfVotes() const{
return times_voted;
}
ostream &operator<<(ostream &os, const Voter &vr){
//<Israel/Judge>
string voter_type[3]= {"All", "Regular", "Judge"};
os << "<";
os << vr.state() << "/";
os << voter_type[vr.voterType()];
os << ">";
return os;
}
Vote::Vote(Voter& voter, const string state0,
const string state1, const string state2,
const string state3, const string state4,
const string state5 , const string state6,
const string state7 , const string state8,
const string state9 ): voter(voter),
states(new string[10]{state0, state1, state2, state3, state4,
state5,state6, state7, state8, state9})
{}
Vote::Vote(const Vote& vote):
voter(vote.voter),
states(new string[10])
{
for(int i=0; i<10; i++){
states[i]= vote.states[i];
}
}
Vote::~Vote() {
delete[] states;
}
MainControl::ParticipantWithVotes::ParticipantWithVotes(Participant* participant):
participant(participant),
regular_votes(0),
judge_votes(0)
{}
void MainControl::ParticipantWithVotes::swap(MainControl::ParticipantWithVotes &a,
MainControl::ParticipantWithVotes &b)
{
MainControl::ParticipantWithVotes temp(a);
a=b;
b=temp;
}
MainControl::MainControl(const int max_time_length,
const int max_number_of_participants,
const int max_times_voter) :
max_time_length(max_time_length),
max_number_of_participants(max_number_of_participants),
max_times_voter(max_times_voter),
phase(Registration),
participant_array(new ParticipantWithVotes[max_number_of_participants]),
participant_counter(0)
{}
MainControl::~MainControl(){
delete[] participant_array;
}
void MainControl::setPhase(Phase new_phase){
if(phase==Registration && new_phase==Contest)
phase=Contest;
if(phase==Contest && new_phase==Voting)
phase=Voting;
}
bool MainControl::participate(const string participant_name){
return getByState(participant_name) != nullptr;
}
MainControl& MainControl::operator+=(Participant& p){
if(phase != Registration)
return *this;
if(participate(p.state()) || !legalParticipant(p))
return *this;
if(max_number_of_participants==participant_counter)
return *this;
int p_index=get_state_index(p.state());
shift(p_index, Right);
participant_array[p_index]=ParticipantWithVotes(&p);
p.updateRegistered(true);
participant_counter++;
return *this;
}
MainControl& MainControl::operator-=(Participant &p){
if(phase != Registration)
return *this;
if(!p.isRegistered())
return *this;
int p_index=get_state_index(p.state());
participant_array[p_index]=ParticipantWithVotes(nullptr);
shift(p_index, Left);
p.updateRegistered(false);
participant_counter--;
return *this;
}
ostream &operator<<(ostream &os, const MainControl &mc){
/*
{
Voting
Australia : Regular(0) Judge(8)
Cyprus : Regular(7) Judge(12)
Israel : Regular(1) Judge(0)
UK : Regular(1) Judge(10)
}
*/
string phases[3]= {"Registration", "Contest", "Voting"};
os << "{" << endl << phases[mc.phase] << endl;
for(int i=0; i<mc.participant_counter; i++) {
MainControl::ParticipantWithVotes cur_pwv=mc.participant_array[i];
if(mc.phase==Registration)
os << *(cur_pwv.participant) << endl;
else if(mc.phase==Voting) {
int regular = cur_pwv.regular_votes;
int judge = cur_pwv.judge_votes;
os << cur_pwv.participant->state() << " : Regular(" << regular << ") Judge("
<< judge << ")" << endl;
}
}
os << "}" << endl;
return os;
}
bool MainControl::legalParticipant(Participant& p){
if(p.state().empty() || p.song().empty() || p.singer().empty())
return false;
return (p.timeLength() <= max_time_length && p.timeLength() > 0);
}
MainControl::ParticipantWithVotes* MainControl::getByState(string state) const{
for(int i=0; i < participant_counter; i++){
Participant* cur_p = participant_array[i].participant;
if (cur_p->state() == state)
return &participant_array[i];
}
return nullptr;
}
MainControl& MainControl::operator+=(Vote vote){
string voter_state = vote.voter.state();
if (!getByState(voter_state))
return *this;
if(vote.voter.voterType() == Regular) {
if (vote.voter.timesOfVotes() >= max_times_voter)
return *this;
string vote_to = vote.states[0];
if (getByState(vote_to) && voter_state != vote_to) {
getByState(vote_to)->regular_votes++;
++(vote.voter);
}
}
else if(vote.voter.voterType() == Judge){
if(vote.voter.timesOfVotes())
return *this;
int counter=0;
for(int i=0; i < 10; i++){
string vote_to = vote.states[i];
if(vote_to.empty())
break;
if(!getByState(vote_to) || voter_state == vote_to)
continue;
counter++;
if(i ==0)
getByState(vote_to)->judge_votes += 12;
else if(i == 1)
getByState(vote_to)->judge_votes += 10;
else if(i == 2)
getByState(vote_to)->judge_votes += 8;
else
getByState(vote_to)->judge_votes += 10-i;
}
if(counter)
++(vote.voter);
}
return *this;
}
int MainControl::get_state_index(const string& state) const {
int i=0;
for(;i<participant_counter;i++) {
string cur_string = participant_array[i].participant->state();
if (cur_string.compare(state) >= 0)
return i;
}
return i;
}
void MainControl::shift(int i, Direction d) {
if(d==Right){
for(int idx=max_number_of_participants-1; idx>i; idx--){
ParticipantWithVotes::swap(participant_array[idx], participant_array[idx-1]);
}
}
if(d==Left){
for(int idx=i; idx<max_number_of_participants-1; idx++){
ParticipantWithVotes::swap(participant_array[idx], participant_array[idx+1]);
}
}
}
MainControl::Iterator::Iterator(int init, MainControl::ParticipantWithVotes *pwd):
i(init), participant_arr(pwd)
{}
MainControl::Iterator &MainControl::Iterator::operator++() {
i++;
return *this;
}
Participant& MainControl::Iterator::operator*() const {
return *(participant_arr[i].participant);
}
bool MainControl::Iterator::operator==(
const MainControl::Iterator &iterator) const {
return (this->i == iterator.i &&
this->participant_arr == iterator.participant_arr);
}
bool MainControl::Iterator::operator<(
const MainControl::Iterator &iterator) const {
return (this->i < iterator.i);
}
MainControl::Iterator MainControl::begin() const {
return MainControl::Iterator(0,participant_array);
}
MainControl::Iterator MainControl::end() const {
return MainControl::Iterator(participant_counter,participant_array);
}
class Pair : public std::pair<string, int>{
public:
Pair(const string& state, int votes) : pair(state, votes) {}
bool operator<(Pair &pair) const {
if(this->second==pair.second)
return this->first.compare(pair.first) < 0;
return this->second<pair.second;
}
};
template <class Iterator>
Iterator get(int i, Iterator begin, Iterator end) {
if(i <= 0 )
return end;
Iterator iter=begin;
int size=0;
while(iter!=end){
++iter;
++size;
}
if(i>size)
return end;
Iterator min = begin;
for (Iterator j = begin; j < end; j++) {
if (*j < *min)
min = j;
}
Iterator max = min, global_max = min;
for(int times = 0; times < i; times++) {
max = min;
for (Iterator j = begin; j < end; j++) {
if (*max < *j && (times==0 || *j < *global_max))
max = j;
}
global_max = max;
}
return max;
}
string MainControl::operator()(int location, VoterType type) {
vector<Pair> votes;
for(Iterator i = begin();i<end();++i){
Participant& p= *i;
MainControl::ParticipantWithVotes* pwd = getByState(p.state());
int result=0;
if(type == Regular)
result= pwd->regular_votes;
else if(type == Judge)
result= pwd->judge_votes;
else if (type == All)
result = pwd->regular_votes + pwd->judge_votes;
votes.push_back(Pair(p.state(), result));
}
auto desired_pair = get<vector<Pair>::iterator>(location,
votes.begin(),votes.end());
if(desired_pair!=votes.end())
return desired_pair->first;
return "";
}