-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
271 lines (237 loc) · 10.4 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
#include "AthleteAlgos.h"
#include "BnRTree.h"
#include "HashSet.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
#include <chrono>
#include <thread>
using namespace std;
void sleeperText(string s = "", int time = 50);
string sportSelect();
void ASCIIDisplay();
Athlete welcomeMessage();
void algoOperation(HashSet &athletes,BnRTree &tree, Athlete &user, unordered_map<string, vector<string>> &sport_to_id, bool &reRun);
//read in TSV data and stores the athlete objects created in a hashset and red and back tree
void GetDataTSVFile(string fileP, HashSet &athletes, BnRTree &tree,
unordered_map<string, vector<string>> &sport_to_id) {
ifstream file(fileP);
if (file.is_open()) {
string line;
getline(file, line); // get rid of column name line
while (getline(file, line)) {
// create stream from line of data
istringstream stream(line);
string id;
string playerName;
string sex;
int age;
int height; // in centimeters
int weight; // in kilograms
string sport;
string event;
string medalType;
getline(stream, id, '\t');
getline(stream, playerName, '\t');
//cout << id << ": " << playerName << endl;
getline(stream, sex, '\t');
string tempA;
getline(stream, tempA, '\t');
age = stoi(tempA);
string tempH;
getline(stream, tempH, '\t');
height = stoi(tempH);
string tempW;
getline(stream, tempW, '\t');
weight = stoi(tempW);
getline(stream, sport, '\t');
getline(stream, event, '\t');
getline(stream, medalType, '\t');
//create athlete object with info from file
Athlete athlete(id, playerName, sex, age, height, weight, sport, event,
medalType);
Athlete *a = new Athlete(id, playerName, sex, age, height, weight, sport, event, medalType);
//add to data structures
athletes.add(athlete);
tree.insert(a);
sport_to_id[sport].push_back(id);
}
}
}
int main() {
Athlete user = welcomeMessage(); //make athlete object from user information
HashSet athletes;
BnRTree tree;
unordered_map<string, vector<string>> sport_to_id;
/* read in TSV */
GetDataTSVFile("athlete_events_filtered.tsv", athletes, tree, sport_to_id);
/* run hashset version of algo and time it */
bool reRun = false;
algoOperation(athletes,tree, user, sport_to_id, reRun);
while (reRun) { // allows user to rerun with different sports until they choose not to.
ASCIIDisplay();
user.setSport(sportSelect());
algoOperation(athletes,tree, user, sport_to_id, reRun);
}
}
Athlete welcomeMessage() { // welcome message and taking in general user characteristics via command line and returns Athlete object
sleeperText(" ____ _ _ _ ");
sleeperText(" / __ \\| | | | (_) ");
sleeperText(" | | | | |_ _ _ __ ___ _ __ __ _ _ __ ___ __ _| |_ _ ___ ");
sleeperText(" | | | | | | | | '_ ` _ \\| '_ \\ / _` | '_ ` _ \\ / _` | __| |/ __|");
sleeperText(" | |__| | | |_| | | | | | | |_) | (_| | | | | | | (_| | |_| | (__ ");
sleeperText(" \\____/|_|\\__, |_| |_| |_| .__/ \\__,_|_| |_| |_|\\__,_|\\__|_|\\___|");
sleeperText(" __/ | | | ");
sleeperText(" |___/ |_| ");
sleeperText();
ASCIIDisplay();
sleeperText("Please enter your information as prompted and we shall see if you have what it takes");
sleeperText();
sleeperText();
string name;
string gender;
int age;
int height;
int weight;
string sport;
cout << "What is your name?" << endl;
getline(cin, name);
sleeperText("",100);
cout << "What is your gender? (Enter \"M\" for Male. Enter \"F\" for Female)" << endl;
getline(cin, gender);
sleeperText("",100);
cout << "What is your age?" << endl;
string temp;
getline(cin, temp);
age = stoi(temp);
sleeperText("",100);
cout << "What is height? (Enter to the nearest centimeters)" << endl;
getline(cin, temp);
height = stoi(temp);
sleeperText("",100);
cout << "What is weight (Enter to the nearest kilograms)" << endl;
getline(cin, temp);
weight = stoi(temp);
sleeperText("",100);
sport = sportSelect();
return Athlete("", name, gender, age, height, weight, sport, "", "");
}
void sleeperText(string s, int time) { // allows us to add delay to cout commands so we can stagger outputs for UX purposes
cout << s << endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
string sportSelect() {
/* displays all sport options and takes in user's choice, returning it as a string */
cout << "What sport are you interested in? (Type exactly as seen)";
cout << endl;
sleeperText("Alpine Skiing Cycling Luge Speed Skating",100);
sleeperText("Archery Diving Modern Pentathlon Swimming",100);
sleeperText("Art Competitions Equestrianism Nordic Combined Syncronized Swimming",100);
sleeperText("Athletics Fencing Rhythmic Gymnastics Table Tennis",100);
sleeperText("Badminton Figure Skating Rowing Taekwondo",100);
sleeperText("Baseball Football Rugby Tennis",100);
sleeperText("Basketball Freestyle Skiing Rugby Sevens Trampolining",100);
sleeperText("Beach Volleyball Golf Sailing Triathlon",100);
sleeperText("Biathlon Gymnastics Shooting Tug-Of-War",100);
sleeperText("Bobsleigh Handball Short Track Speed Skating Volleyball" ,100);
sleeperText("Boxing Hockey Skeleton Water Polo",100);
sleeperText("Canoeing Ice Hockey Ski Jumping Weightlifting",100);
sleeperText("Cross Country Skiing Judo Snowboarding Wrestling",100);
sleeperText("Curling Lacrosse Softball ",100);
cout << endl;
string sport;
getline(cin, sport);
return sport;
}
void algoOperation(HashSet &athletes,BnRTree &tree, Athlete &user, unordered_map<string, vector<string>> &sport_to_id, bool &reRun)
{
/* The running function for our algorithms, abstracted so we could run the algorithms multiple times without having to relaunch the whole program */
cout << endl << "-----------------------------------------" << endl;
cout << endl << "Hashset Implementation of Algorithm: " << endl;
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
algo(athletes,user,sport_to_id);
chrono::steady_clock::time_point end = chrono::steady_clock::now();
sleeperText(".",300);
sleeperText(".",300);
sleeperText(".",300);
sleeperText("", 300);
cout << "Time for hashset probability calculation: "
<< chrono::duration_cast<chrono::microseconds>(end - begin).count() << "[μs]" << endl;
sleeperText("",1000);
cout << "-----------------------------------------" << endl;
/* run red-black tree version of algo and time it */
cout << endl << "Red-Black Tree Implementation of Algorithm: " << endl;
begin = chrono::steady_clock::now();
algo(tree,user,sport_to_id);
end = chrono::steady_clock::now();
sleeperText(".",300);
sleeperText(".",300);
sleeperText(".",300);
sleeperText("", 300);
cout << "Time for Red-Black Tree probability calculation: "
<< chrono::duration_cast<chrono::microseconds>(end - begin).count() << "[μs]" << endl;
sleeperText("", 300);
sleeperText(".",300);
sleeperText(".",300);
sleeperText(".",300);
sleeperText("", 300);
cout << "-----------------------------------------" << endl << endl;
cout << "Would you like to try a new sport? (Enter \"Y\" for Yes. Enter \"N\" for No)" << endl;
string answer;
getline(cin,answer);
answer == "Y" ? reRun = true : reRun = false;
if (answer == "N") {
cout << endl << "Thank you for using the..." << endl << endl;
sleeperText(" ____ _ _ _ ");
sleeperText(" / __ \\| | | | (_) ");
sleeperText(" | | | | |_ _ _ __ ___ _ __ __ _ _ __ ___ __ _| |_ _ ___ ");
sleeperText(" | | | | | | | | '_ ` _ \\| '_ \\ / _` | '_ ` _ \\ / _` | __| |/ __|");
sleeperText(" | |__| | | |_| | | | | | | |_) | (_| | | | | | | (_| | |_| | (__ ");
sleeperText(" \\____/|_|\\__, |_| |_| |_| .__/ \\__,_|_| |_| |_|\\__,_|\\__|_|\\___|");
sleeperText(" __/ | | | ");
sleeperText(" |___/ |_| ");
sleeperText();
}
}
void ASCIIDisplay() { // randomly displays one of three ASCII arts
int random = rand() % 3;
if (random == 0) {
sleeperText(" \\ /");
sleeperText(" |_O X O\\");
sleeperText(" /`-/ \\-'\\");
sleeperText(" | \\ / |");
sleeperText(" / \\ | \\");
sleeperText("",400);
sleeperText(".",100);
sleeperText(".",100);
sleeperText(".",100);
sleeperText("",100);
}
else if (random == 1) {
sleeperText(" o \\ o / _ o __| \\ / |__ o _ \\ o / o");
sleeperText("/|\\ | /\\ ___\\o \\o | o/ o/__ /\\ | /|\\");
sleeperText("/ \\ / \\ | \\ /) | ( \\ /o\\ / ) | (\\ / | / \\ / \\");
sleeperText("",400);
sleeperText(".",100);
sleeperText(".",100);
sleeperText(".",100);
sleeperText("",100);
}
else {
sleeperText(" ___");
sleeperText(" /` _\\");
sleeperText(" | / 0|--.");
sleeperText(" - / \\_|0`/ /.`'._/)");
sleeperText(" - ~ -^_| /-_~ ^- ~_` - -~ _");
sleeperText(" - ~ -| | - ~ - ~ -");
sleeperText(" \\ \\, ~ - ~");
sleeperText(" \\_|");
sleeperText("",400);
sleeperText(".",100);
sleeperText(".",100);
sleeperText(".",100);
sleeperText("",100);
}
}