-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject3.cpp
327 lines (261 loc) · 8.73 KB
/
project3.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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <chrono>
using namespace std;
using namespace chrono;
struct Games {
int id;
string game_names;
double rating;
string genres;
string platforms;
string pubs_devs;
};
struct PriorityQueue
{
//calculates the index of the index's parent.
int Parent(int i)
{
return (i - 1) / 2;
}
//calculates the index of the index's left child.
int LC(int i)
{
return (2 * i + 1);
}
//calculates the index of the index's left child.
int RC(int i)
{
return (2 * i + 2);
}
//Heapify's up from pos x
// Average and worst case is O(log(N))
void heapifyU(vector<Games>& MaxHeap, int x)
{
// Checks if swap is neccessary
if (MaxHeap[Parent(x)].rating < MaxHeap[x].rating)
{
swap(MaxHeap[x], MaxHeap[Parent(x)]);
//calls Heapifyu again
heapifyU(MaxHeap,Parent(x));
}
}
//Heapifies down from pos x
// Average and worst case is O(log(N))
void heapifyD(vector<Games>& MaxHeap, int x)
{
unsigned int left = LC(x);
unsigned int right = RC(x);
int lpos = x;
//determines if and which child to switch with
if (left < MaxHeap.size() && MaxHeap[left].rating > MaxHeap[x].rating)
{
lpos = left;
}
if (right < MaxHeap.size() && MaxHeap[right].rating > MaxHeap[lpos].rating)
{
lpos = right;
}
//swaps and calls HeapifyD again
if (lpos != x)
{
swap(MaxHeap[x], MaxHeap[lpos]);
heapifyD(MaxHeap,lpos);
}
}
//checks if maxheap is empty
bool empty(vector<Games>& MaxHeap)
{
if (MaxHeap.size() == 0)
return true;
else
return false;
}
// Average and worst case is O(log(N))
//inserts values then calls heapifyU
void push(vector<Games>& MaxHeap, Games x)
{
MaxHeap.push_back(x);
int index = MaxHeap.size() - 1;
heapifyU(MaxHeap,index);
}
//Pops the head value and then calls heapifyD
// Average and worst case is O(log(N))
Games pop(vector<Games>& MaxHeap)
{
Games temp = MaxHeap[0];
MaxHeap[0] = MaxHeap.back();
MaxHeap.pop_back();
heapifyD(MaxHeap,0);
return temp;
}
//Pops values and prints out the first 25 values that match the user inputed criteria
void printPQ(vector<Games>& MaxHeap, string user_input)
{
int count = 0;
user_input = "\"" + user_input + "\"";
while (count != 25)
{
Games printer = pop(MaxHeap);
if (printer.genres == user_input || printer.genres.find(user_input) != string::npos) {
std::cout << count + 1 << "." << printer.game_names << " | " << printer.rating << "% | " << printer.genres << " | " << printer.platforms << " | " << printer.pubs_devs << endl;
count++;
}
}
}
};
class TreeNode {
public:
double key; //rating
Games gamedata = {};
TreeNode* left = nullptr;
TreeNode* right = nullptr;
};
class IGDB {
public:
TreeNode* root;
vector<TreeNode*> games;
string strOutput = ""; //use for cout ouput
IGDB()
{
root = nullptr;
}
//Average case is O(log(N))
//Worst case is 0(N)
//recursivly inserts nodes
TreeNode* insertNode(TreeNode* root, TreeNode* node) {
if (root == nullptr) {
root = node;
return root;
}
if (node->key < root->key) {
root->left = insertNode(root->left, node);
}
else if (node->key > root->key) {
root->right = insertNode(root->right, node);
}
else {
return root;
}
return root;
}
//using reverse inorder traversal, searches for the genre inputted by the user and if node contains the genre, pushes node into a vector of nodes
// Average case is O(log(N))
//Worst case is 0(N)
vector <TreeNode*> inorderSearchGenre(string genre, TreeNode* root, vector<TreeNode*> &games) {
if (root == nullptr){
return {};
}
inorderSearchGenre(genre, root->right, games);
if(root->gamedata.genres.find(genre) != string::npos && root->gamedata.rating)
games.push_back(root);
inorderSearchGenre(genre, root->left, games);
return games;
}
void printBST(vector <TreeNode*> &games, string user_input) {
for (int i = 0; i < 25; i++) {
cout << i + 1 << "." << games[i]->gamedata.game_names << " | " << games[i]->gamedata.rating << "% | " << games[i]->gamedata.genres << " | " << games[i]->gamedata.platforms << " | " << games[i]->gamedata.pubs_devs << endl;
}
}
};
string capitalize_first_letter(string input) {
for (int i = 0; i < input.length(); i++) {
if (i == 0) {
input[i] = toupper(input[i]);
}
}
return input;
}
int main() {
Games gameObj;
PriorityQueue PQ = PriorityQueue();
vector<Games> gamesVec;
string filePath = "games.csv";
IGDB igdb;
fstream inFile;
string output;
char del = ',';
inFile.open(filePath, ios::in);
int cnt = 0;
int id = 0;
if (inFile.is_open()) {
string tp;
std::cout << "\nInitializing data to BST and Priority Queue..." << endl << endl;
auto start = high_resolution_clock::now();
while (!inFile.eof()) {
TreeNode* newnode = new TreeNode();
if (cnt == 0) {
getline(inFile, output);
cnt++;
continue;
}
getline(inFile, output, ',');
if (output == "")
continue;
else
gameObj.id = stoi(output);
getline(inFile, output, '\"');
getline(inFile, output, '\"');
gameObj.game_names = "\"" + output + "\"";
getline(inFile, output, ',');
getline(inFile, output, ',');
//double d = stod(output);
if (output == "")
gameObj.rating = 0;
else
gameObj.rating = stod(output);
getline(inFile, output, ',');
gameObj.genres = output;
getline(inFile, output, ',');
gameObj.platforms = output;
getline(inFile, output);
gameObj.pubs_devs = output;
PQ.push(gamesVec,gameObj);
newnode->key = gameObj.rating;
newnode->gamedata = gameObj;
igdb.root = igdb.insertNode(igdb.root, newnode);
cnt++;
}
inFile.close();
}
cout << "Welcome to our Game Parser!" << endl << endl;
cout << "Genre Options: " << endl << endl;
cout << "Fighting" << endl;
cout << "Shooter" << endl;
cout << "Music" << endl;
cout << "Platform" << endl;
cout << "Puzzle" << endl;
cout << "Racing" << endl;
cout << "Simulator" << endl;
cout << "Sport"<< endl;
cout << "Strategy" << endl;
cout << "Tactical" << endl;
cout << "Adventure" << endl;
cout << "Arcade" << endl;
cout << "Indie" << endl << endl;
string user_input;
cout << "Please enter your favorite genre from the list above: ";
cin >> user_input;
user_input = capitalize_first_letter(user_input);
cout << "Getting top 25 games..." << endl << endl;
//user_input = "\"" + user_input + "\"";
auto start = high_resolution_clock::now();
igdb.inorderSearchGenre(user_input, igdb.root, igdb.games);
cout << "Top 25 Games from Priority Queue:" << endl;
auto start2 = high_resolution_clock::now();
PQ.printPQ(gamesVec, user_input);
auto stop2 = high_resolution_clock::now();
auto PQtime = duration_cast<microseconds>(stop2 - start2);
cout << "\nPriority Queue Search by Genre: " << PQtime.count() <<" microseconds" << endl << endl << endl;
cout << "Top 25 Games from BST: " << endl;
igdb.printBST(igdb.games, user_input);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "\nBST Inorder Search by Genre: " << duration.count() << " microseconds" << endl;
cout << "\nThanks for Using Game Parser! :)\n";
delete igdb.root;
delete &gamesVec;
}