-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
284 lines (246 loc) · 12.7 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
284
#include <iostream>
#include <iterator>
#include <chrono>
#include "HeapSort.h"
#include "quickSort.h"
#include "ReadFile.h"
//code for using chrono execution time from geeksforgeeks.org/measure-execution-time-function-cpp/
//Print Vector Function
void printVector(vector<pair<string, int>> cities, string country) {
cout << "---------------------------------------------" << endl;
cout << "Future Cities Populations in " << country << endl;
for (unsigned int i = 0; i < cities.size(); i++) {
cout <<"City: "<< cities[i].first << ", Population: " << cities[i].second << endl;
}
cout << "---------------------------------------------" << endl;
}
int main() {
string file = "geonames.csv";
unordered_map<string, vector<pair<string, int>>> countries;
ReadFile(file, countries);
bool endProgram = false;
//User input with programs features: QS or HS
while (endProgram != true) {
int input;
cout << "Which feature would you like to use:" << endl;
cout << "1. Find largest city by population in a country and see sorted list of city populations by country" << endl;
cout << "2. Compare population of two cities to find the largest and see sorted lists of city populations by country" << endl;
cout << "0. Exit" << endl;
cin >> input;
cin.ignore();
switch (input) {
//Largest Population while sorting the population of all cities in country
case 1: {
//user input
string city;
string country;
cout << "Which city do you want to find the population of?" << endl;
getline(cin, city);
cout << "What country is " << city << " in?" << endl;
getline(cin, country);
//Make sub vector with country and sort by cities in country
vector<pair<string, int>> sortVec = countries[country];
//user input
cout << "Which method do you want to use to find the population of "<< city <<", " <<country << "?" << endl;
cout << "Method 1. Heap Sort" << endl;
cout << "Method 2. Quick Sort" << endl;
cin >> input;
switch (input) {
case 1:
{
//measure execution time
cout << "Calling heap sort" << endl;
auto start = std::chrono::high_resolution_clock::now();
//calling function
HeapSort(sortVec);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std :: chrono :: duration_cast<std::chrono::microseconds>(stop - start);
//PRINT SORTED VECTOR
printVector(sortVec, country);
cout << "Time taken to sort by Heap Sort: " << duration.count() << " microseconds" << endl;
//Print CITY WITH LARGEST POPULATION
cout << "City with largest population in " << country << " is " << sortVec[sortVec.size() - 1].first << " with a population of " << sortVec[sortVec.size() - 1].second << endl;
cout << endl;
break;
};
case 2:
{
//measure execution time
cout << "Calling quick sorts" << endl;
int endRoot = sortVec.size();
auto start = std::chrono::high_resolution_clock::now();
//calling function
quickSort(sortVec, 0, endRoot - 1);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std :: chrono :: duration_cast<std::chrono::microseconds>(stop - start);
//PRINT SORTED VECTOR
printVector(sortVec, country);
cout << "Time taken to sort by Quick Sort: " << duration.count() << " microseconds" << endl;
//Print CITY WITH LARGEST POPULATION
cout << "City with largest population in " << country << " is " << sortVec[sortVec.size() - 1].first << " with a population of " << sortVec[sortVec.size() - 1].second << endl;
cout << endl;
break;
};
default: {
break;
};
}
endProgram = false;
break;
};
//Compare populations
case 2: {
//user input
string city1, country1;
cout << "What is the first city you want to compare?" << endl;
getline(cin, city1);
cout << "What country is " << city1 << " in?" << endl;
getline(cin, country1);
//SUB VECTOR WITH ALL CITIES IN COUNTRY
vector<pair<string, int>> sortVec1 = countries[country1];
//user input
cout << "What is the second city you want to compare to " << city1 << ", " << country1 << "?" << endl;
string country2, city2;
getline(cin, city2);
cout << "What country is " << city2 << " in?" << endl;
getline(cin, country2);
//SUB VECTOR WITH ALL CITIES IN COUNTRY
vector<pair<string, int>> sortVec2 = countries[country2];
//user input
cout << "Which method do you want to use to compare "<< city1 <<","<<country1 << " and "<< city2<<", "<<country2 <<"?" << endl;
cout << "Method 1. Heap Sort" << endl;
cout << "Method 2. Quick Sort" << endl;
cin >> input;
switch (input) {
case 1: {
//get execution time of 1st sort
cout << "Calling heap sorts" << endl;
auto start = std::chrono::high_resolution_clock::now();
HeapSort(sortVec1);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std :: chrono :: duration_cast<std::chrono::microseconds>(stop - start);
//get execution time of 2nd sort
auto start2 = std::chrono::high_resolution_clock::now();
HeapSort(sortVec2);
auto stop2 = std::chrono::high_resolution_clock::now();
auto duration2 = std :: chrono :: duration_cast<std::chrono::microseconds>(stop2 - start2);
//PRINT BOTH SORTED VECTORS
printVector(sortVec1, country1);
cout << "----------------------------------------------------" << endl;
cout << "----------------------------------------------------" << endl;
cout << "----------------------------------------------------" << endl;
printVector(sortVec2, country2);
//COMPARE CITIES POPULATION AND PRINT BIGGER POPULATION
string bigCity;
string smallCity;
int population1;
int population2;
vector<pair<string, int>> :: iterator iter;
vector<pair<string, int>> :: iterator iter2;
//go through vector 1
for(iter = sortVec1.begin(); iter != sortVec1.end(); iter++){
if(iter->first == city1){
population1 = iter->second;
break;
}
}
//go through vector 2
for(iter2 = sortVec2.begin(); iter2 != sortVec2.end(); iter2++){
if(iter2->first == city2){
population2 = iter->second;
break;
}
}
//compare populations
if(population1 > population2){
bigCity = city1;
smallCity = city2;
}
else{
bigCity = city2;
smallCity = city1;
}
//print output
cout << "First Heap Sort takes " << duration.count() << " microseconds" << endl;
cout << "Second Heap Sort takes " << duration2.count() << " microseconds" << endl;
cout << bigCity << " has a bigger population than " << smallCity << endl;
cout << endl;
break;
};
case 2: {
//get execution time of 1st sort
cout << "Calling quick sort" << endl;
int endRoot1 = sortVec1.size();
auto start = std::chrono::high_resolution_clock::now();
quickSort(sortVec1, 0, endRoot1 - 1);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std :: chrono :: duration_cast<std::chrono::microseconds>(stop - start);
printVector(sortVec1, country1);
cout << "----------------------------------------------------" << endl;
cout << "----------------------------------------------------" << endl;
cout << "----------------------------------------------------" << endl;
//get execution time of 2nd sort
int endRoot2 = sortVec2.size();
auto start2 = std::chrono::high_resolution_clock::now();
quickSort(sortVec2, 0, endRoot2 - 1);
auto stop2 = std::chrono::high_resolution_clock::now();
auto duration2 = std :: chrono :: duration_cast<std::chrono::microseconds>(stop2 - start2);
printVector(sortVec2, country2);
//COMPARE CITIES POPULATION AND PRINT BIGGER POPULATION
string bigCity;
string smallCity;
int population1;
int population2;
vector<pair<string, int>> :: iterator iter;
vector<pair<string, int>> :: iterator iter2;
//go through vector 1
for(iter = sortVec1.begin(); iter != sortVec1.end(); iter++){
if(iter->first == city1){
population1 = iter->second;
break;
}
}
//go through vector 2
for(iter2 = sortVec2.begin(); iter2 != sortVec2.end(); iter2++){
if(iter2->first == city2){
population2 = iter->second;
break;
}
}
//compare populations
if(population1 > population2){
bigCity = city1;
smallCity = city2;
}
else{
bigCity = city2;
smallCity = city1;
}
//print output
cout << "First Quick Sort takes " << duration.count() << " microseconds" << endl;
cout << "Second Quick Sort takes " << duration2.count() << " microseconds" << endl;
cout << bigCity << " has a bigger population than " << smallCity << endl;
cout << endl;
break;
};
default: {
break;
};
}
endProgram = false;
break;
};
case 0: {
//Breaks while loop and ends code
endProgram = true;
break;
};
default: {
cout << "Input a valid integer." << endl;
endProgram = false;
break;
};
}
}
return 0;
}