-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssn2.cpp
325 lines (301 loc) · 9.69 KB
/
Assn2.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
#include <iostream>
#include <string>
#include <algorithm>
#include "ShapeTwoD.h"
#include "Square.h"
#include "Rectangle.h"
#include "Circle.h"
#include "Cross.h"
#include "algo.h"
#include "Path.h"
using namespace std;
string choice;
bool input_read = false;
bool user_exit = false;
const string student_id = "7311217";
const string student_name = "Nguyen Gia Khanh";
static int shape_id = 0;
vector <ShapeTwoD*> my_shape;
void Menu();
void HandleInput(string choice);
bool CheckInput(string input);
void ShapeInput();
void ShapeCompute();
void ShapeOut();
void ShapeSort();
bool SortAreaASC(ShapeTwoD* a, ShapeTwoD* b);
bool SortAreaDSC(ShapeTwoD* a, ShapeTwoD* b);
bool SortSpecialTypeArea(ShapeTwoD* a, ShapeTwoD* b);
int current_shape = 0;
int main(){
Menu();
return 0;
}
// Main menu
void Menu() {
while(true){
ClearScreen();
cout << "------------------------------------" << endl;
cout << "Student ID : " << student_id << endl;
cout << "Student Name : " << student_name << endl;
cout << "------------------------------------" << endl;
cout << "Welcome to Assn2 program!" << endl;
cout << endl;
cout << endl;
cout << "1) Input sensor data" << endl;
if(input_read) {
cout << "2) Compute area(for all records)" << endl;
cout << "3) Print shapes report" << endl;
cout << "4) Sort shapes report" << endl;
cout << "5) 2D-StarFleet's path finding" << endl;
}
cout << "6) Exit program" << endl;
cout << endl;
cout << "Please enter your choice: ";
cin >> choice;
HandleInput(TrimSpace(choice));
if(user_exit){
cout << "[ Exit ]" << endl;
ClearScreen();
break;
}
}
}
//Handle user input
void HandleInput(string choice) {
if(choice == "1") {
ClearScreen();
ShapeInput();
} else if((choice == "2") && (input_read)) {
ClearScreen();
ShapeCompute();
} else if((choice == "3") && (input_read)) {
ClearScreen();
cout << "Total no. of records available = " << my_shape.size() << endl;
cout << endl;
ShapeOut();
} else if((choice == "4") && (input_read)) {
ShapeSort();
} else if((choice == "5")) {
Path(my_shape);
} else if((choice == "6")) {
user_exit = true;
if(input_read) {
for(int i=0; i< my_shape.size(); i++) {
my_shape[i]->~ShapeTwoD();
}
}
} else {
cout << "Invalid option!" << endl;
MySleep(1);
return;
}
}
//Check if input is valid
bool CheckInput(string input) {
int input_converted;
try {
input_converted = ConvertStringToInteger(input);
} catch(...) {
return false;
}
if(input_converted < 0) {
return false;
}
return true;
}
//Handle shape input
void ShapeInput() {
string shape_name;
string special_type;
int shape_size = 0;
string input_x;
string input_y;
int shape_x;
int shape_y;
vector<Point*> point_list;
vector<int> x_coor;
vector<int> y_coor;
bool cws = false;
point_list.clear();
cout << "[Input sensor data]" << endl;
cout << "Please enter name of shape: ";
cin >> shape_name;
cout << "Please enter special type: ";
cin >> special_type;
if(special_type == "WS") {
cws = true;
} else if(special_type == "NS") {
cws = false;
} else {
cout << "Invalid type!" << endl;
MySleep(1);
return;
}
cout << endl;
if(shape_name == "Square" || shape_name == "Rectangle") {
shape_size = 4;
for(int i=0; i<shape_size; i++){
cout << "Please enter x-ordinate of pt. " << i+1 << ": ";
cin >> input_x;
cout << "Please enter y-ordinate of pt. " << i+1 << ": ";
cin >> input_y;
cout << endl;
if(!(CheckInput(input_x)) || !(CheckInput(input_y))) {
cout << "Invalid input" << endl;
MySleep(1);
return;
}
shape_x = ConvertStringToInteger(input_x);
shape_y = ConvertStringToInteger(input_y);
point_list.push_back(new Point(shape_x, shape_y));
x_coor.push_back(shape_x);
y_coor.push_back(shape_y);
}
int max_x = *max_element(x_coor.begin(), x_coor.end());
int max_y = *max_element(y_coor.begin(), y_coor.end());
int min_x = *min_element(x_coor.begin(), x_coor.end());
int min_y = *min_element(y_coor.begin(), y_coor.end());
if(shape_name == "Square") {
my_shape.push_back(new Square("Square", cws, shape_id, point_list, Point(min_x, min_y), Point(max_x, max_y)));
} else {
my_shape.push_back(new Rectangle("Rectangle", cws, shape_id, point_list, Point(min_x, min_y), Point(max_x, max_y)));
}
shape_id++;
point_list.clear();
x_coor.clear();
y_coor.clear();
} else if(shape_name == "Circle") {
shape_size = 1;
string cir_x;
string cir_y;
int c_x;
int c_y;
string input_radius;
int radius;
cout << "Please enter x-ordinate of center : ";
cin >> cir_x;
cout << "Please enter y-ordinate of center : ";
cin >> cir_y;
if(!(CheckInput(cir_x)) || !(CheckInput(cir_y))) {
cout << "Invalid input" << endl;
MySleep(1);
return;
}
c_x = ConvertStringToInteger(cir_x);
c_y = ConvertStringToInteger(cir_y);
cout << endl;
cout << "Please enter radius of circle : ";
cin >> input_radius;
if(!(CheckInput(input_radius))) {
cout << "Invalid input" << endl;
MySleep(1);
return;
}
radius = ConvertStringToInteger(input_radius);
point_list.push_back(new Point(c_x, c_y));
my_shape.push_back(new Circle("Circle", cws, shape_id, point_list, radius));
shape_id++;
} else if(shape_name == "Cross") {
shape_size = 12;
for(int i=0; i<shape_size; i++){
cout << "Please enter x-ordinate of pt. " << i+1 << ": ";
cin >> input_x;
cout << "Please enter y-ordinate of pt. " << i+1 << ": ";
cin >> input_y;
cout << endl;
if(!(CheckInput(input_x)) || !(CheckInput(input_y))) {
cout << "Invalid input" << endl;
MySleep(1);
return;
}
shape_x = ConvertStringToInteger(input_x);
shape_y = ConvertStringToInteger(input_y);
point_list.push_back(new Point(shape_x, shape_y));
x_coor.push_back(shape_x);
y_coor.push_back(shape_y);
}
my_shape.push_back(new Cross("Cross", cws, shape_id, point_list));
shape_id++;
} else {
cout << "Invalid shape!" << endl;
MySleep(1);
return;
}
input_read = true;
shape_size = 0;
cout << "Record sucessfully stored. Going back to main menu ..." << endl;
MySleep(1);
}
//Compute shape area
void ShapeCompute() {
for(int i = 0;i<my_shape.size();i++){
my_shape[i]->setArea(my_shape[i]->ComputeArea());
}
cout << "Computation complete! ( " << my_shape.size() - current_shape << " records were updated )" << endl;
current_shape = my_shape.size();
MySleep(1);
}
//Print shape report
void ShapeOut() {
for(int i = 0; i<my_shape.size(); i++){
cout<<my_shape[i]->toString() << endl;
}
Freeze();
}
//Handle shape sorting
void ShapeSort() {
string sort_option = "";
bool sort_valid = false;
cout << endl;
ClearScreen();
while(!sort_valid){
cout << " a) Sort by area (ascending)" << endl;
cout << " b) Sort by area (descending)" << endl;
cout << " c) Sort by special type and area" << endl;
cout << "Please select sort option ('q' to go to main menu): ";
cin >> sort_option;
sort_option = TrimSpace(sort_option);
if(sort_option == "a") {
sort_valid = true;
sort(my_shape.begin(), my_shape.end(), SortAreaASC);
ClearScreen();
ShapeOut();
} else if(sort_option == "b") {
sort_valid = true;
sort(my_shape.begin(), my_shape.end(), SortAreaDSC);
ClearScreen();
ShapeOut();
} else if(sort_option == "c") {
sort_valid = true;
sort(my_shape.begin(), my_shape.end(), SortSpecialTypeArea);
ClearScreen();
ShapeOut();
} else if(sort_option == "q") {
return;
} else {
cout << "Invalid option!" << endl;
MySleep(1);
ClearScreen();
}
}
}
//Sort by area ascending
bool SortAreaASC(ShapeTwoD* a, ShapeTwoD* b) {
return a->GetArea() < b->GetArea();
}
//Sort by area descending
bool SortAreaDSC(ShapeTwoD* a, ShapeTwoD* b) {
return a->GetArea() > b->GetArea();
}
//Sort by special type then area
bool SortSpecialTypeArea(ShapeTwoD* a, ShapeTwoD* b) {
if(a->GetContainsWarpSpace() == b->GetContainsWarpSpace()) {
return a->GetArea() > b->GetArea();
} else {
if(a->GetContainsWarpSpace()) {
return true;
}
}
return false;
}