-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
451 lines (446 loc) · 15.3 KB
/
Menu.cpp
File metadata and controls
451 lines (446 loc) · 15.3 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include"Menu.h"
KDtree pizzeria;
HashMain mainPizzeria;
HashValley Valley;
int NumCommand;
ProgramState programstate;
void AddN(string NameOfValley) {
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mAdd neighborhood\033[0m\n";
//string NameOfValley;
point p[4];
/*cout << "\t\t\t\033[38;5;202mName of the valley:\033[0m";
cout << "\033[38;5;220m";
std::cin.ignore(1000, '\n');
getline(cin, NameOfValley);
cout << "\033[0m";*/
for (int i = 0; i < 4; i++) {
cin >> p[i];
}
valley* newValley = new valley(NameOfValley, p);
Valley.insert(newValley);
cout << "\t\t\t\033[38;5;28mThe neighborhood was added:)\033[0m";
NumCommand++;
Command* command = new Command("Add-N", *newValley, NumCommand);
programstate.insert(command);
Sleep(3000);
}
void AddP(string NameOfPizzeria) {
//string NameOfPizzeria;
float x, y;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mAdd main branch\033[0m\n";
//cout << "\t\t\t\033[38;5;202mName of the pizzeria:\033[0m";
//cout << "\033[38;5;220m";
//std::cin.ignore(1000, '\n');
//getline(cin, NameOfPizzeria);
//cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mX:\033[0m";
cout << "\033[38;5;220m";
cin >> x;
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mY:\033[0m";
cout << "\033[38;5;220m";
cin >> y;
cout << "\033[0m";
Branch* NewBranch = new Branch(NameOfPizzeria, x, y, "", 1);
MainBranchClass* newMainBranch = new MainBranchClass(NameOfPizzeria, x, y);
if (pizzeria.search(x, y)) {
cout << "\t\t\t\033[38;5;52mThere is another pizzeria in this place:(\033[0m";
Sleep(3000);
}
else {
pizzeria.insert(*NewBranch);
cout << "\t\t\t\033[38;5;28mPizzeria added:)\033[0m";
NumCommand++;
Command* command = new Command("Add-P", *NewBranch, NumCommand);
programstate.insert(command);
Sleep(3000);
mainPizzeria.insert(newMainBranch);
}
}
void AddBr(string NameOfPizzeria) {
//string NameOfPizzeria;
string NameMainBranch;
float x, y;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mAdd pizzeria\033[0m\n";
/*cout << "\t\t\t\033[38;5;202mName of the pizzeria:\033[0m";
cout << "\033[38;5;220m";
std::cin.ignore(1000, '\n');
getline(cin, NameOfPizzeria);
cout << "\033[0m";*/
cout << "\t\t\t\033[38;5;202mName of the main branch:\033[0m";
cout << "\033[38;5;220m";
std::cin.ignore(1000, '\n');
getline(cin, NameMainBranch);
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mX:\033[0m";
cout << "\033[38;5;220m";
cin >> x;
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mY:\033[0m";
cout << "\033[38;5;220m";
cin >> y;
cout << "\033[0m";
Branch* NewBranch = new Branch(NameOfPizzeria, x, y, NameMainBranch, 0);
if (pizzeria.search(x, y)) {
cout << "\t\t\t\033[38;5;52mThere is another pizzeria in this place:(\033[0m";
Sleep(3000);
}
else {
MainBranchClass* Main = mainPizzeria.search(NameMainBranch);
if (Main != nullptr) {
pizzeria.insert(*NewBranch);
Main->setBranch(*NewBranch);
Main->AddNumberOfBranch();
cout << "\t\t\t\033[38;5;28mPizzeria added:)\033[0m";
NumCommand++;
Command* command = new Command("Add-Br", *NewBranch, NumCommand);
programstate.insert(command);
Sleep(3000);
}
else {
cout << "\t\t\t\033[38;5;52mMain branch not found:(\033[0m";
Sleep(3000);
}
}
}
void DelBr() {
float x, y;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mRemove a pizzeria\033[0m\n";
cout << "\t\t\t\033[38;5;202mX:\033[0m";
cout << "\033[38;5;220m";
cin >> x;
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mY:\033[0m";
cout << "\033[38;5;220m";
cin >> y;
cout << "\033[0m";
point* deleteNode = new point(x, y);
if (pizzeria.search(x, y)) {
if (pizzeria.searchBranch(x, y).getMainFlag()) {
cout << "\t\t\t\033[38;5;52mThe main branch cannot be deleted:(\033[0m";
Sleep(3000);
}
else {
Branch deleted = pizzeria.searchBranch(x, y);
MainBranchClass* Main = mainPizzeria.search(pizzeria.searchBranch(x, y).getNameMainBranch());
Main->removeBranch(*deleteNode);
Main->ReduceNumberOfBeranch();
pizzeria.DeleteP(*deleteNode);
cout << "\t\t\t\033[38;5;28mThe pizzeria was removed:)\033[0m";
NumCommand++;
Command* command = new Command("Del-Br", deleted, NumCommand);
programstate.insert(command);
Sleep(3000);
}
}
else {
cout << "\t\t\t\033[38;5;52mPizzeria not found:(\033[0m";
Sleep(3000);
}
}
void NearP() {
float x, y;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mDisplay nearest pizzeria\033[0m\n";
cout << "\t\t\t\033[38;5;202mX:\033[0m";
cout << "\033[38;5;220m";
cin >> x;
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mY:\033[0m";
cout << "\033[38;5;220m";
cin >> y;
cout << "\033[0m";
point* p1 = new point(x, y);
Node* closestNode = pizzeria.FindClosest1(*p1);
if (closestNode != nullptr) {
cout << "\t\t\t\033[38;5;28mClosest node to the target point:" << closestNode->pizzeria.getName() << ""
<< closestNode->pizzeria.getCoordinate();
cout << "\033[0m";
Sleep(3000);
}
else {
cout << "\t\t\t\033[38;5;52mNo points in the KD Tree:(\033[0m";
Sleep(3000);
}
}
void AvailP() {
float x, y, z;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mDisplay available pizzerias\033[0m\n";
cout << "\t\t\t\033[38;5;202mX:\033[0m";
cout << "\033[38;5;220m";
cin >> x;
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mY:\033[0m";
cout << "\033[38;5;220m";
cin >> y;
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mEnter the radius:\033[0m\n";
cout << "\t\t\t\033[38;5;202mR:\033[0m";
cout << "\033[38;5;220m";
cin >> z;
cout << "\033[0m";
point* p1 = new point(x, y);
vector<Branch> pointsInRadius = pizzeria.FindPointsInCircle1(*p1, z);
if (!pointsInRadius.empty()) {
cout << "\t\t\t\033[38;5;52m";
cout << "Points within radius " << z << " from the target point:\n";
for (auto& point1 : pointsInRadius)
cout << point1.getName() << " : " << point1.getCoordinate() << "\n";
cout << "\033[0m";
}
else {
cout << "\t\t\t\033[38;5;52mThere is no point:(\n\033[0m";
}
Sleep(1000);
}
void ListP(string NameOfValley) {
//string NameOfValley;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mDisplay neighborhood pizzerias\033[0m\n";
/*cout << "\t\t\t\033[38;5;202mName of the neighborhood:\033[0m";
cout << "\033[38;5;220m";
std::cin.ignore(1000, '\n');
getline(cin, NameOfValley);
cout << "\033[0m"*/;
valley* v1 = Valley.search(NameOfValley);
if (v1 == NULL) {
cout << "\t\t\t\033[38;5;52mThere is no Valley:(\033[0m";
Sleep(3000);
}
else {
point* p1 = new point(v1->getPoint()[0].getX(), v1->getPoint()[0].getY());
point* p2 = new point(v1->getPoint()[1].getX(), v1->getPoint()[1].getY());
point* p3 = new point(v1->getPoint()[2].getX(), v1->getPoint()[2].getY());
point* p4 = new point(v1->getPoint()[3].getX(), v1->getPoint()[3].getY());
cout << "\t\t\t\033[38;5;28mPizzerias in " << NameOfValley << " neighborhood:";
pizzeria.FindPointsInRectangle(*p1, *p2, *p3, *p4);
cout << "\033[0m";
Sleep(3000);
}
}
void Help() {
cout << "\n\n\n\n";
cout << "\t\t\t\t\033[38;5;202m PIZZA HOUSE\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mAdd-N\033[0m""\t\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mAdd valley\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mAdd-P\033[0m""\t\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mAdd main branch\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mAdd-Br\033[0m""\t\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mAdd pizzeria\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mDel-Br\033[0m""\t\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mRemove pizzeria\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mList-P\033[0m""\t\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mDisplay of pizzerias in a neighborhood\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mList-Brs\033[0m""\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mDisplay pizzerias of a main branch\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mNear-P\033[0m""\t\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mDisplay nearest pizzeria\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mNear-Br\033[0m""\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mDisplay the nearest pizzeria of a branch\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mAvail-P\033[0m""\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mDisplay available pizzerias\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mMost-Brs\033[0m""\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mDisplay the pizzeria with the most branches\033[0m\n";
cout << "\t\t\t\033[38;5;202m*" << "\033[38;5;30mUndo\033[0m""\t\t\033[38;5;202m------>\033[0m\t\t""\033[38;5;30mReturn to previous commands\033[0m\n";
cout << "\t\t\t\033[38;5;202mInput format:COMMAND_NAME [NAME] && COMMAND_NAME [NAME]\033[0m\n";
cout << "\t\t\t\033[38;5;202mEnter:\033[0m";
}
void ListBrs(string NameOfPizzeria) {
//string NameOfPizzeria;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mDisplay of one-branch pizzerias\033[0m\n";
/*cout << "\t\t\t\033[38;5;202mName of the pizzeria:\033[0m";
cout << "\033[38;5;220m";
std::cin.ignore(1000, '\n');
getline(cin, NameOfPizzeria);
cout << "\033[0m";*/
MainBranchClass* Main = mainPizzeria.search(NameOfPizzeria);
Main->printBranch();
Sleep(3000);
}
void NearBr(string NameMainBranch)//=============
{
//string NameMainBranch;
float x, y;
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mDisplay the nearest pizzeria of a branch\033[0m\n";
//cout << "\t\t\t\033[38;5;202mName of the main branch:\033[0m";
//cout << "\033[38;5;220m";
//std::cin.ignore(1000, '\n');
//getline(cin, NameMainBranch);
//cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mX:\033[0m";
cout << "\033[38;5;220m";
cin >> x;
cout << "\033[0m";
cout << "\t\t\t\033[38;5;202mY:\033[0m";
cout << "\033[38;5;220m";
cin >> y;
cout << "\033[0m";
point* p1 = new point(x, y);
MainBranchClass* Main = mainPizzeria.search(NameMainBranch);
if (Main != NULL) {
KDtree pizzeriaBranches;
pizzeriaBranches.setNodes(Main->getBranches());
pizzeriaBranches.buildtree();
Node* closestNode = pizzeriaBranches.FindClosest1(*p1);
if (closestNode != nullptr) {
cout << "\t\t\t\033[38;5;28mClosest node to the target point:" << closestNode->pizzeria.getName() << ""
<< closestNode->pizzeria.getCoordinate();
cout << "\033[0m";
Sleep(3000);
}
else {
cout << "\t\t\t\033[38;5;52mNo points in the KD Tree:(\033[0m\n";
Sleep(3000);
}
}
else {
cout << "\t\t\t\033[38;5;52mMain branch not found:(\033[0m\n";
Sleep(3000);
}
}
void MostBrs()
{
MainBranchClass most = mainPizzeria.mostBranches();
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;28mThe pizzeria with the most branches:\033[0m";
cout << "\033[38;5;28m";
cout << most.getName() << "With " << most.getNumberOfBranch() << " branches";
cout << "\033[0m";
Sleep(3000);
}
void Undo() {
cout << "\n\n\n\n";
cout << "\t\t\t\033[38;5;30mUndo\033[0m\n";
int p;
cout << "\t\t\t\033[38;5;202mP:\033[0m";
cout << "\033[38;5;220m";
cin >> p;
cout << "\033[0m";
if (p < NumCommand) {
for (int i = NumCommand; i > p; i--, NumCommand--) {
Command* UndoCom = programstate.search(i);
if (UndoCom->getName() == "Add-P") {
MainBranchClass* newMainBranch = new MainBranchClass(UndoCom->getExecuteNode().getName(), UndoCom->getExecuteNode().getCoordinate().getX(), UndoCom->getExecuteNode().getCoordinate().getY());
pizzeria.DeleteP(UndoCom->getExecuteNode().getCoordinate());
mainPizzeria.Deleted(*newMainBranch);
programstate.Deleted(*UndoCom);
cout << "\t\t\t\033[38;5;88m";
cout << "Pizzeria" << UndoCom->getExecuteNode().getName() << "was removed:)\n";
cout << "\033[0m";
}
else if (UndoCom->getName() == "Add-Br") {
MainBranchClass* Main = mainPizzeria.search(pizzeria.searchBranch(UndoCom->getExecuteNode().getCoordinate().getX(), UndoCom->getExecuteNode().getCoordinate().getY()).getNameMainBranch());
Main->removeBranch(UndoCom->getExecuteNode().getCoordinate());
Main->ReduceNumberOfBeranch();
pizzeria.DeleteP(UndoCom->getExecuteNode().getCoordinate());
programstate.Deleted(*UndoCom);
cout << "\t\t\t\033[38;5;88m";
cout << "Pizzeria " << UndoCom->getExecuteNode().getName() << " was removed:)\n";
cout << "\033[0m";
}
else if (UndoCom->getName() == "Del-Br")
{
pizzeria.insert(UndoCom->getExecuteNode());
MainBranchClass* Main = mainPizzeria.search(UndoCom->getExecuteNode().getNameMainBranch());
Main->setBranch(UndoCom->getExecuteNode());
Main->AddNumberOfBranch();
programstate.Deleted(*UndoCom);
cout << "\t\t\t\033[38;5;28m";
cout << "Pizzeria " << UndoCom->getExecuteNode().getName() << " was added:)\n";
cout << "\033[0m";
}
else if (UndoCom->getName() == "Add-N") {
Valley.Deleted(UndoCom->getExecuteValley());
programstate.Deleted(*UndoCom);
cout << "\t\t\t\033[38;5;88m";
cout << "Pizzeria " << UndoCom->getExecuteValley().getName() << " was removed:)\n";
cout << "\033[0m";
}
}
}
while (1) {
cout << "\t\t\t\033[38;5;202m";
cout << "Press 1 to continue";
cout << "\033[0m";
string en;
cout << "\033[38;5;220m";
std::cin.ignore(1000, '\n');
getline(cin, en);
cout << "\033[0m";
if (en == "1")
break;
}
}
bool Vallid(const string& rule) {
stringstream str(rule);
string act;
str >> act;
if (act == "Add-N" || act == "Add-P" || act == "Add-Br" || act == "Del-Br" || act == "List-P" || act == "List-Brs" || act == "Near-P" || act == "Near-Br" || act == "Avail-P" || act == "Most-Brs" || act == "Undo") {
return true;
}
return false;
}
vector<string> sentence(const string& input) {
stringstream strinput(input);
string rule;
vector<string> sentence;
while (getline(strinput, rule, '&')) {
if (rule != "")
sentence.push_back(rule);
}
return sentence;
}
void Menu() {
string enter;
vector<string> input;
while (1) {
system("cls");
while (1) {
Help();
cout << "\033[38;5;220m";
//std::cin.ignore(1000, '\n');
getline(cin, enter);
cout << "\033[0m";
input = sentence(enter);
bool flag = true;
for (const auto& str : input) {
if (!Vallid(str)) {
//cout << "Invalid Format: " << str << endl;
flag = false;
break;
}
}
if (flag) {
system("cls");
break;
}
/*if (enter == "Add-N" || enter == "Add-P" || enter == "Add-Br" || enter == "Del-Br" || enter == "List-P" || enter == "List-Brs" || enter == "Near-Br" || enter == "Near-P" || enter == "Avail-P" || enter == "Most-Brs" || enter == "Undo") {
system("cls");
break;
}*/
system("cls");
}
for (const auto& rule1 : input) {
stringstream rulestr(rule1);
string act, str1;
rulestr >> act >> str1;
size_t start_pos = str1.find("[");
size_t end_pos = str1.find("]");
string name;
if (start_pos != string::npos && end_pos != string::npos)
name = str1.substr(start_pos + 1, end_pos - start_pos - 1);
if (act == "Add-N") { AddN(name); }
if (act == "Add-P") { AddP(name); }
if (act == "Add-Br") { AddBr(name); }
if (act == "Del-Br") { DelBr(); }
if (act == "List-P") { ListP(name); NumCommand++; }
if (act == "List-Brs") { ListBrs(name); NumCommand++; }
if (act == "Near-P") { NearP(); NumCommand++; }
if (act == "Near-Br") { NearBr(name); NumCommand++; }
if (act == "Avail-P") { AvailP(); NumCommand++; }
if (act == "Most-Brs") { MostBrs(); NumCommand++; }
if (act == "Undo") { Undo(); }
if (act != "Add-N" && act != "Add-P" && act != "Add-Br" && act != "Del-Br") {
Branch* temp = new Branch();
Command* command = new Command("***", *temp, NumCommand);
}
}
}
}