-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
222 lines (187 loc) · 6.27 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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <climits>
#include <algorithm>
#include <chrono>
#include "Quakinator.h"
using namespace std;
#define Q Quakinator
void init();
void printLogo();
void printMenu();
void printCurrentSettings();
void printSortAlgoSubMenu();
void printSortCriteriaSubMenu();
void printDisplayCountSubMenu();
void printDisplayListSubMenu();
void sortList();
string getEQString(string key);
unordered_map<string, vector<double>> Q::data;
vector<string> Q::keys;
int Q::criteriaIndex = 0;
vector<string> sortAlgos, sortCriteria;
unsigned int mainOption = UINT_MAX;
unsigned int algoOption = 3;
unsigned int criteriaOption = 1;
int displayCount = 10;
float duration = -1;
int main() {
srand(time(nullptr)); // For random shuffle by time
init();
Q::init();
do {
// Print menu
printMenu();
switch(mainOption) {
case 1: // Set Sorting Algo.
printSortAlgoSubMenu();
break;
case 2: // Set Sorting Criteria
printSortCriteriaSubMenu();
break;
case 3: // Set Display Count
printDisplayCountSubMenu();
break;
case 4: // Shuffle List
random_shuffle(Q::keys.begin(), Q::keys.end());
cout << "\nList has been shuffled!\n\n";
break;
case 5: // Display List
{
// Start sort clock
auto start = chrono::high_resolution_clock::now();
// Sort list based on criteria
switch (algoOption) {
case 1:
std::sort(Q::keys.begin(), Q::keys.end(), Q::compare);
// cout << "\nSorted by STL sort.\n";
break;
case 2:
Q::quickSort(0, Q::keys.size()-1);
// cout << "\nSorted by quicksort.\n";
break;
case 3:
cout << "\nList was not sorted.\n";
break;
}
// End sort clock
auto end = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::milliseconds>(end - start).count();
// Display list
printDisplayListSubMenu();
break;
}
default:
if (mainOption > 6) {
cout << "\nPlease enter a valid option.\n\n";
cout << "Enter Option: ";
cin >> mainOption;
}
break;
}
} while(mainOption != 6);
return 0;
}
void init() {
// Initialize Sorting Algorithms
sortAlgos.push_back("STL Sort");
sortAlgos.push_back("Quicksort");
sortAlgos.push_back("No Sort");
// Initialize Sorting Criteria
sortCriteria.push_back("Time");
sortCriteria.push_back("Magnitude");
sortCriteria.push_back("Longitude");
sortCriteria.push_back("Latitude");
// Print logo on program start
printLogo();
}
void printLogo() {
cout << " __" << endl;
cout << "___( o)>" << endl;
cout << "\\ <_. )" << endl;
cout << " `---'" << endl;
cout << "Quakinator by the Qua(c)kers\n";
}
void printMenu() {
cout << "---------------------------\n";
cout << "[Main Menu]\n";
cout << "1. Set Sorting Algorithm >>\n";
cout << "2. Sort By >>\n";
cout << "3. Set Display Count\n";
cout << "4. Shuffle List\n";
cout << "5. Display List\n";
cout << "6. Exit\n\n";
cout << "[Current Settings]\n";
cout << "Algorithm: " << sortAlgos[algoOption-1] << "\n";
cout << "Criteria: " << sortCriteria[criteriaOption-1] << "\n";
cout << "Display Count: " << displayCount << "\n\n";
cout << "Enter Option: ";
cin >> mainOption;
}
void printSortAlgoSubMenu() {
cout << "\n---------------------------\n";
cout << "[Sorting Algorithm]\n\n";
for (int i = 0; i < sortAlgos.size(); i++)
cout << i+1 << ". " << sortAlgos[i] << "\n";
cout << sortAlgos.size()+1 << ". Return to Main Menu\n\n";
unsigned int temp;
cout << "Enter Option: ";
cin >> temp;
while(temp > sortAlgos.size()+1) {
cout << "\nPlease enter a valid option.\n\n";
cout << "Enter Option: ";
cin >> temp;
}
algoOption = (temp == sortAlgos.size()+1) ? algoOption : temp;
}
void printSortCriteriaSubMenu() {
cout << "\n---------------------------\n";
cout << "[Sort By]\n\n";
for (int i = 0; i < sortCriteria.size(); i++)
cout << i+1 << ". " << sortCriteria[i] << "\n";
cout << sortCriteria.size()+1 << ". Return to Main Menu\n\n";
unsigned int temp;
cout << "Enter Option: ";
cin >> temp;
while(temp > sortCriteria.size()+1) {
cout << "\nPlease enter a valid option.\n\n";
cout << "Enter Option: ";
cin >> temp;
}
criteriaOption = (temp == sortCriteria.size()+1) ? criteriaOption : temp;
Q::criteriaIndex = criteriaOption-2;
}
void printDisplayCountSubMenu() {
cout << "\n---------------------------\n";
cout << "[Display Count]\n\n";
string temp;
while (true) {
cout << "(Enter -1 to display all earthquakes)\n";
cout << "Enter Display Count: ";
cin >> temp;
try {
displayCount = stoi(temp);
if (displayCount < -1) throw exception();
break;
} catch (exception e) {
cout << "\nError: Please enter a valid number.\n\n";
}
}
}
void printDisplayListSubMenu() {
cout << "\n---------------------------\n";
cout << "[Display List]\n\n";
if (displayCount == -1)
for (int i = 0; i < Q::keys.size(); i++)
cout << i+1 << ". " << getEQString(Q::keys[i]);
else
for (int i = 0; i < displayCount; i++)
cout << i+1 << ". " << getEQString(Q::keys[i]);
cout << "\n\nSort Duration: " << duration << " milliseconds\n\n";
}
string getEQString(string key) {
vector<double> v = Q::data[key];
return "Time: " + key + ", Mag: " + to_string(v[0]) + ", Lon: " + to_string(v[1]) + ", Lat: " + to_string(v[2]) + "\n";
}