-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnsoccerpedia.cpp
465 lines (395 loc) · 15.2 KB
/
Ensoccerpedia.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
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
//Ensoccerpedia: this program uses two data structure, a hash table and a bst, to compare their perfomance when doing the same tasks
//It reads from 4 different soccer data sets of 4 different leagues and can search for specific matches or a team's performance in one season
//Authors: Eitan Kogutek and Manuel Vera Miranda
//Special acknowledgement to James Curley who compiled the datasets used here for his R packet "engsoccerdata"
//It can be found in https://github.com/jalapic/engsoccerdata
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include "Hash.h"
#include "redBlackTree.h"
using namespace std;
//function to run the program using the hash table
void mainHash() {
cout << "Welcome to the Ensoccerpedia!" << endl;
cout << "An accessible soccer database with over 290,000 historical results from Europe's top 4 leagues" << endl;
cout << "To begin, select which league you would like to explore: " << endl;
cout << "1. England (available 1888-1889 to 2019-2020) \n2. Spain (available 1928-1929 to 2019-2020)" << endl;
cout << "3. Italy (available 1934-1935 to 2019-2020) \n4. Germany (available 1963-1964 to 2019-2020)" << endl;
short selection;
cin >> selection;
string fileName;
int hashSize = 0, tableSize = 0;
//specifies the data file to load
switch (selection) {
case 1:
fileName = "england.csv";
hashSize = 199884;
tableSize = 131;
cout << "You have chosen the English league.";
break;
case 2:
fileName = "spain.csv";
hashSize = 25435;
tableSize = 91;
cout << "You have chosen the Spanish league.";
break;
case 3:
fileName = "italy.csv";
hashSize = 26924;
tableSize = 85;
cout << "You have chosen the Italian league.";
break;
case 4:
fileName = "germany.csv";
hashSize = 35858;
tableSize = 56;
cout << "You have chosen the German league.";
break;
default:
cout << "Invalid input! Please try running the program again" << endl;
exit(0);
}
cout << "There are " << hashSize << " games in the database." << endl;
//initializes the hash table
Hash hash = Hash(tableSize);
string line;
//depending on the league chosen, loads the dataset
if (fileName == "england.csv") {
ifstream england(fileName);
getline(england, line);
while (getline(england, line, ','))
{
string date = line;
getline(england, line, ',');
int season = stoi(line);
getline(england, line, ',');
string homeTeam = line;
getline(england, line, ',');
string awayTeam = line;
getline(england, line, ',');
getline(england, line, ',');
int hGoals = stoi(line);
getline(england, line, ',');
int aGoals = stoi(line);
getline(england, line, ',');
getline(england, line, ',');
getline(england, line, ',');
getline(england, line, ',');
getline(england, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
hash.Insert(season, game);
}
}
else if (fileName == "spain.csv") {
ifstream spain(fileName);
string line;
getline(spain, line);
while (getline(spain, line, ','))
{
string date = line;
getline(spain, line, ',');
int season = stoi(line);
getline(spain, line, ',');
string homeTeam = line;
getline(spain, line, ',');
string awayTeam = line;
getline(spain, line, ',');
getline(spain, line, ',');
getline(spain, line, ',');
int hGoals = stoi(line);
getline(spain, line, ',');
int aGoals = stoi(line);
getline(spain, line, ',');
getline(spain, line, ',');
getline(spain, line, ',');
getline(spain, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
hash.Insert(season, game);
}
}
else if (fileName == "germany.csv") {
ifstream germany(fileName);
getline(germany, line);
while (getline(germany, line, ','))
{
string date = line;
getline(germany, line, ',');
int season = stoi(line);
getline(germany, line, ',');
string homeTeam = line;
getline(germany, line, ',');
string awayTeam = line;
getline(germany, line, ',');
getline(germany, line, ',');
int hGoals = stoi(line);
getline(germany, line, ',');
int aGoals = stoi(line);
getline(germany, line, ',');
getline(germany, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
hash.Insert(season, game);
}
}
else if (fileName == "italy.csv") {
ifstream italy(fileName);
getline(italy, line);
while (getline(italy, line, ','))
{
string date = line;
getline(italy, line, ',');
int season = stoi(line);
getline(italy, line, ',');
string homeTeam = line;
getline(italy, line, ',');
string awayTeam = line;
getline(italy, line, ',');
getline(italy, line, ',');
int hGoals = stoi(line);
getline(italy, line, ',');
int aGoals = stoi(line);
getline(italy, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
hash.Insert(season, game);
}
}
cout << "Which action would you like to perform?" << endl;
cout << "1. Specific match (requires season year and home and away teams)" << endl;
cout << "2. Season records (requires season year and team)" << endl;
cin >> selection;
int season = 0;
string home = "";
string away = "";
Game found;
switch (selection) {
//prints out a specific match
case 1:
cout << "Please enter the season year (i.e. for 2018-19 season enter 2018)" << endl;
cin >> season;
cout << "Please enter the home team" << endl;
cin.ignore();
getline(cin, home);
home = "\"" + home + "\"";
cout << "Please enter the away team" << endl;
getline(cin, away);
away = "\"" + away + "\"";
cout << "\nSearching..." << endl << endl;
found = hash.Search(season, home, away);
cout << "Game found!" << endl;
cout << "Date: " << found.date << endl;
cout << "Teams: " << found.homeTeam.substr(1, found.homeTeam.size() - 2) << " - " << found.awayTeam.substr(1, found.awayTeam.size() - 2) << endl;
cout << "Result: " << found.hGoals << "-" << found.aGoals << endl << endl;
break;
//prints a team's season record
case 2:
cout << "Please enter the season year (i.e. for 2018-19 season enter 2018)" << endl;
cin >> season;
cout << "Please enter the team you would like to search the record for: " << endl;
cin.ignore();
getline(cin, home);
hash.Search(season, home);
break;
//exits the program if an invalid input was chosen
default:
cout << "Invalid input! Please try running the program again" << endl;
exit(0);
}
}
//function to run the program using the binary search tree
void mainTree() {
cout << "Welcome to the Ensoccerpedia!" << endl;
cout << "An accessible soccer database with over 290,000 historical results from Europe's top 4 leagues" << endl;
cout << "To begin, select which league you would like to explore: " << endl;
cout << "1. England (available 1888-1889 to 2019-2020) \n2. Spain (available 1928-1929 to 2019-2020)" << endl;
cout << "3. Italy (available 1934-1935 to 2019-2020) \n4. Germany (available 1963-1964 to 2019-2020)" << endl;
short selection;
cin >> selection;
string fileName;
int hashSize = 0;
//specifies the data file to load
switch (selection) {
case 1:
fileName = "england.csv";
hashSize = 199884;
cout << "You have chosen the English league.";
break;
case 2:
fileName = "spain.csv";
hashSize = 25435;
cout << "You have chosen the Spanish league.";
break;
case 3:
fileName = "italy.csv";
hashSize = 26924;
cout << "You have chosen the Italian league.";
break;
case 4:
fileName = "germany.csv";
hashSize = 35858;
cout << "You have chosen the German league.";
break;
default:
cout << "Invalid input! Please try running the program again" << endl;
exit(0);
}
cout << "There are " << hashSize << " games in the database." << endl;
//initializes the red-black tree
redBlackTree tree;
string line;
//depending on the league chosen, loads the dataset
if (fileName == "england.csv") {
ifstream england(fileName);
getline(england, line);
while (getline(england, line, ','))
{
string date = line;
getline(england, line, ',');
int season = stoi(line);
getline(england, line, ',');
string homeTeam = line;
getline(england, line, ',');
string awayTeam = line;
getline(england, line, ',');
getline(england, line, ',');
int hGoals = stoi(line);
getline(england, line, ',');
int aGoals = stoi(line);
getline(england, line, ',');
getline(england, line, ',');
getline(england, line, ',');
getline(england, line, ',');
getline(england, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
tree.insert(season, game);
}
}
else if (fileName == "spain.csv") {
ifstream spain(fileName);
string line;
getline(spain, line);
while (getline(spain, line, ','))
{
string date = line;
getline(spain, line, ',');
int season = stoi(line);
getline(spain, line, ',');
string homeTeam = line;
getline(spain, line, ',');
string awayTeam = line;
getline(spain, line, ',');
getline(spain, line, ',');
getline(spain, line, ',');
int hGoals = stoi(line);
getline(spain, line, ',');
int aGoals = stoi(line);
getline(spain, line, ',');
getline(spain, line, ',');
getline(spain, line, ',');
getline(spain, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
tree.insert(season, game);
}
}
else if (fileName == "germany.csv") {
ifstream germany(fileName);
getline(germany, line);
while (getline(germany, line, ','))
{
string date = line;
getline(germany, line, ',');
int season = stoi(line);
getline(germany, line, ',');
string homeTeam = line;
getline(germany, line, ',');
string awayTeam = line;
getline(germany, line, ',');
getline(germany, line, ',');
int hGoals = stoi(line);
getline(germany, line, ',');
int aGoals = stoi(line);
getline(germany, line, ',');
getline(germany, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
tree.insert(season, game);
}
}
else if (fileName == "italy.csv") {
ifstream italy(fileName);
getline(italy, line);
while (getline(italy, line, ','))
{
string date = line;
getline(italy, line, ',');
int season = stoi(line);
getline(italy, line, ',');
string homeTeam = line;
getline(italy, line, ',');
string awayTeam = line;
getline(italy, line, ',');
getline(italy, line, ',');
int hGoals = stoi(line);
getline(italy, line, ',');
int aGoals = stoi(line);
getline(italy, line, '\n');
Game game(date, season, homeTeam, awayTeam, hGoals, aGoals);
tree.insert(season, game);
}
}
cout << "Which action would you like to perform?" << endl;
cout << "1. Specific match (requires season year and home and away teams)" << endl;
cout << "2. Season records (requires season year and team)" << endl;
cin >> selection;
int season = 0;
string home = "";
string away = "";
Game found;
switch (selection) {
//prints out a specific match
case 1:
cout << "Please enter the season year (i.e. for 2018-19 season enter 2018)" << endl;
cin >> season;
cout << "Please enter the home team" << endl;
cin.ignore();
getline(cin, home);
home = "\"" + home + "\"";
cout << "Please enter the away team" << endl;
getline(cin, away);
away = "\"" + away + "\"";
cout << "\nSearching..." << endl << endl;
found = tree.search(season, home, away);
cout << "Game found!" << endl;
cout << "Date: " << found.date << endl;
cout << "Teams: " << found.homeTeam.substr(1, found.homeTeam.size() - 2) << " - " << found.awayTeam.substr(1, found.awayTeam.size() - 2) << endl;
cout << "Result: " << found.hGoals << "-" << found.aGoals << endl << endl;
break;
//prints a team's season record
case 2:
cout << "Please enter the season year (i.e. for 2018-19 season enter 2018)" << endl;
cin >> season;
cout << "Please enter the team you would like to search the record for: " << endl;
cin.ignore();
getline(cin, home);
tree.search(season, home);
break;
//exits the program if an invalid input was chosen
default:
cout << "Invalid input! Please try running the program again" << endl;
exit(0);
}
}
int main() {
cout << "Select which data structure to run the program with " << endl;
cout << "1. Hash Table (uses a hash table of maps)" << endl;
cout << "2. Binary Search Tree (uses a red-black tree of maps)" << endl;
int selection;
cin >> selection;
//runs the program depending on which data structure was chosen
if (selection == 1) {
mainHash();
}
else if (selection == 2) {
mainTree();
}
return 0;
}