forked from EDAII/Lista4-AndreLucas-LeonardoMedeiros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
283 lines (259 loc) · 7.45 KB
/
main.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
/*
Leonardo Medeiros de Araujo
Andre Lucas de Sousa Pinto
*/
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class SoldGame{
public:
SoldGame(int id,string scorePhrase,string title,
string url,string platform,double score,
vector<string> genre,bool editorsChoice,
short int releaseYear,short int releaseMonth,
short int releaseDay){
this->id = id;
this->scorePhrase = scorePhrase;
this->title = title;
this->url = url;
this->platform = platform;
this->score = score;
this->genre = genre;
this->editorsChoice = editorsChoice;
this->releaseYear = releaseYear;
this->releaseMonth = releaseMonth;
this->releaseDay = releaseDay;
}
void print(){
cout << id << endl;
cout << scorePhrase << endl;
cout << title << endl;
cout << url << endl;
cout << platform << endl;
cout << score << endl;
cout << genre[0];
for(unsigned int i = 1; i < genre.size(); ++i){
cout << ' ' << genre[i];
}
cout << endl;
cout << editorsChoice << endl;
cout << releaseYear << endl;
cout << releaseMonth << endl;
cout << releaseDay << endl;
}
int print(string arg){
if (arg == "id")
cout << id << endl;
else if (arg == "scorePhrase")
cout << scorePhrase << endl;
else if (arg == "title")
cout << title << endl;
else if (arg == "url")
cout << url << endl;
else if (arg == "platform")
cout << platform << endl;
else if (arg == "score")
cout << score << endl;
else if (arg == "genre"){
cout << genre[0];
for(unsigned int i = 1; i < genre.size(); ++i){
cout << ' ' << genre[i];
}
cout << endl;
}
else if (arg == "editorsChoice")
cout << editorsChoice << endl;
else if (arg == "releaseYear")
cout << releaseYear << endl;
else if (arg == "releaseMonth")
cout << releaseMonth << endl;
else if (arg == "releaseDay")
cout << releaseDay << endl;
else{
return -1;
}
return 0;
}
int id;
string scorePhrase;
string title;
string url;
string platform;
double score;
vector<string> genre;
bool editorsChoice;
short int releaseYear;
short int releaseMonth;
short int releaseDay;
bool operator<(const SoldGame & b)const{
if(platform == b.platform){
if(score == b.score){
return (365*(int)releaseYear+30*(int)releaseMonth+(int)releaseDay
<
365*(int)b.releaseYear+30*(int)b.releaseMonth+(int)b.releaseDay);
}
return (score > b.score);
}
return (platform < b.platform);
}
bool operator>(const SoldGame & b)const{
if(platform == b.platform){
if(score == b.score){
return (365*(int)releaseYear+30*(int)releaseMonth+(int)releaseDay
>
365*(int)b.releaseYear+30*(int)b.releaseMonth+(int)b.releaseDay);
}
return (score < b.score);
}
return (platform > b.platform);
}
};
void shiftDown(vector<SoldGame> & games, int root, int bottom){
int maxChild;
bool done;
done = false;
while((root*2 <= bottom) && (!done)){
if(root*2 == bottom)
maxChild = root * 2;
else if(games[root*2] > games[root*2+1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;
if(games[root] < games[maxChild]){
swap(games[root], games[maxChild]);
root = maxChild;
}
else{
done = true;
}
}
}
void heapSort(vector<SoldGame> & games){
int i;
for(i = (games.size()/2)-1; i >= 0; i--){
shiftDown(games, i, games.size());
}
for(i = games.size()-1; i>=1; i--){
swap(games[0], games[i]);
shiftDown(games, 0, i-1);
}
}
int main(){
ifstream in;
in.open("ign.csv");
string auxS;
getline(in, auxS); // Ignoring first line, headers of table, appears in class atribute order
vector<SoldGame> games;
while(!in.eof()){
int id;
string scorePhrase;
string title;
string url;
string platform;
double score;
vector<string> genre;
bool editorsChoice;
short int releaseYear;
short int releaseMonth;
short int releaseDay;
in >> id;
in.get(); // ignoring ','
getline(in, scorePhrase, ',');
int auxI;
auxI = in.peek();
if(auxI == '\"'){ // title has '"' in name
in.get(); // ignoring '"'
getline(in, auxS, '\"');
title = auxS;
while(true){
auxI = in.peek();
if(auxI == ','){
in.get(); // ignoring ','
break;
}
else{
title += '\"'; // ajusting title, adding '"' that was wrongly removed
getline(in, auxS, '\"');
title += auxS;
}
}
}
else{
getline(in, title, ',');
}
getline(in, url, ',');
getline(in, platform, ',');
in >> score;
in.get(); // ignoring ','
auxI = in.peek();
if(auxI == '\"'){ // more than 1 genre
in.get(); // ignoring '"'
while(true){
getline(in, auxS, ',');
if(auxS[auxS.size() - 1] == '\"'){ // no more genre
auxS.pop_back();
genre.push_back(auxS);
break;
}
else{
genre.push_back(auxS);
in.get(); // ignoring ' '
}
}
in.get(); // ignoring '"'
}
else{
getline(in, auxS, ',');
genre.push_back(auxS);
}
char auxC;
in.get(auxC);
if(auxC == 'Y'){
editorsChoice = true;
}
else{
editorsChoice = false;
}
in.get(); // ignoring ','
in >> releaseYear;
in.get(); // ignoring ','
in >> releaseMonth;
in.get(); // ignoring ','
in >> releaseDay;
SoldGame game(id, scorePhrase, title, url, platform,
score, genre, editorsChoice, releaseYear,
releaseMonth, releaseDay);
games.push_back(game);
}
heapSort(games);
int rankPos = 0; // count 3 best elements
string tempPlatform;
cout << "TOP 3 Best Games of each Platform" << endl;
for(unsigned int i=0; i<games.size(); i++){
if(!rankPos){
cout << "Platform: " << games[i].platform << endl;
tempPlatform=games[i].platform;
}
else{
if(games[i].platform != tempPlatform){
rankPos=0;
cout << "Platform: " << games[i].platform << endl;
tempPlatform=games[i].platform;
}
}
if(rankPos<3 and games[i].platform == tempPlatform){
cout << rankPos+1 << " > " << games[i].title << " (with: " << games[i].score << " score points).";
cout << endl;
rankPos++;
}else{
if(games[i].score==10){
cout << rankPos+1 << " > " << games[i].title << " (with: " << games[i].score << " score points).";
cout << endl;
rankPos++;
}
}
}
return 0;
}