-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.h
More file actions
646 lines (551 loc) · 28.2 KB
/
Menu.h
File metadata and controls
646 lines (551 loc) · 28.2 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//
// Created by Kamil Bonkowski on 30/03/2021.
//
#ifndef DATASTRUCTURES_MENU_H
#define DATASTRUCTURES_MENU_H
#include "Array.h"
#include "ListTwoDirection.h"
#include "File.h"
#include "Heap.h"
#include "RedBlackTree.h"
class Menu {
public:
Timer *timer = timer->getInstance();
int visit_counter = 0;
string file_name;
string file_name_and_extension;
string path;
int mode_interface() {
int mode = 0;
cout << "-----------------------------------------------" << endl;
cout << " Welcome to Data Structures Time Analyzer " << endl;
cout << "-----------------------------------------------" << endl;
cout << " 1. Test Mode" << endl;
cout << " 2. Read Mode" << endl;
cout << " 3. Show average operations time" << endl;
cout << " 4. About" << endl;
cout << "-----------------------------------------------" << endl;
cout << endl;
cout << " Press 0 to Exit" << endl;
do {
cin >> mode;
if (mode >= 0 && mode <= 4) {
switch (mode) {
case 1: {
regex reg("[a-zA-Z0-9]+");
cout << "File name: ";
cin >> file_name;
if (regex_match(file_name, reg)) {
file_name_and_extension = file_name + ".txt";
File *file = new File(file_name_and_extension);
timer->clear_data();
clearScreen();
data_structures_interface();
} else {
cout << "Incorrect file name syntax" << endl;
}
}
break;
case 2: {
visit_counter++;
char choice;
clearScreen();
if (visit_counter > 1) {
cout << "Do you want to remove previous measurements [y/n]" << endl;
do {
cin >> choice;
if (choice != 'y' && choice != 'n') {
cout << "Incorrect choice. Try again !" << endl;
}
} while (choice != 'y' && choice != 'n');
if (choice == 'y') {
timer->clear_data();
}
}
cout << "Type a path to your file directory :" << endl;
cin >> path;
file_name_and_extension = path;
fstream file;
file.open(file_name_and_extension, ios::in);
if (!file.good()) {
cout << "File does not exist !" << endl;
mode_interface();
} else {
cout << file_name_and_extension << endl;
cout << "File loaded successfully !" << endl;
data_structures_interface();
}
break;
}
case 3: {
timer->view_data();
mode_interface();
}
case 4: {
clearScreen();
about_interface();
break;
}
default:
exit(1);
}
} else {
cout << "Mode not found try again !" << endl;
}
} while (mode < 0 || mode > 4);
}
void about_interface() {
int choice = 0;
cout << "----------------------------------------------------- " << endl;
cout << " About " << endl;
cout << "----------------------------------------------------- " << endl;
cout << " * Test Mode * - allows you to specify amount, " << endl;
cout << " lower bounds and upper bounds of randomly " << endl;
cout << " generated numbers " << endl;
cout << "----------------------------------------------------- " << endl;
cout << " * Read Mode * - program can read your own .txt file" << endl;
cout << " organized with proper pattern " << endl;
cout << endl;
cout << " *File Pattern* " << endl;
cout << " [1-st line] number of elements " << endl;
cout << " [2-n to n-th line] elements of the array " << endl;
cout << " separated with new line " << endl;
cout << "----------------------------------------------------- " << endl;
cout << " * Show average operation time * - allow to view " << endl;
cout << " each operation average time " << endl;
cout << "----------------------------------------------------- " << endl;
cout << endl;
cout << " Press 0 to Return" << endl;
do {
cin >> choice;
if (choice == 0) {
mode_interface();
} else {
cout << "Sign not allowed here ! [Press 0 to Return] " << endl;
}
} while (choice != 0);
}
int select_option() {
int choice = 0;
do {
cin >> choice;
if (choice >= 0 && choice <= 7) {
return choice;
} else {
cout << "No operation found try again !" << endl;
}
} while (choice < 0 || choice > 7);
};
int operations_interface_tree(string name) {
cout << " ------------------------------------------- " << endl;
cout << " Operations on " << name << endl;
cout << " ------------------------------------------- " << endl;
cout << " 1. Add element " << endl;
cout << " 2. Remove element " << endl;
cout << " 3. Remove element by index " << endl;
cout << " 4. Find element by value " << endl;
cout << " 5. Find element by index " << endl;
cout << " 6. Show heap " << endl;
cout << " ------------------------------------------- " << endl;
cout << endl;
cout << " Press 0 to Return " << endl;
return select_option();
}
int operations_interface_RB_TREE(string name) {
cout << " ------------------------------------------- " << endl;
cout << " Operations on " << name << endl;
cout << " ------------------------------------------- " << endl;
cout << " 1. Add element " << endl;
cout << " 2. Remove element " << endl;
cout << " 3. Find element by value " << endl;
cout << " 4. Show Red Black Tree " << endl;
cout << " ------------------------------------------- " << endl;
cout << endl;
cout << " Press 0 to Return " << endl;
return select_option();
}
int operations_interface(string name) {
cout << " ------------------------------------------- " << endl;
cout << " Operations on " << name << endl;
cout << " ------------------------------------------- " << endl;
cout << " 1. Add element " << endl;
cout << " 2. Add element by index " << endl;
cout << " 3. Remove element " << endl;
cout << " 4. Remove element by index " << endl;
cout << " 5. Find element by value " << endl;
cout << " 6. Find element by index " << endl;
cout << " 7. Show " << name << endl;
cout << " ------------------------------------------- " << endl;
cout << endl;
cout << " Press 0 to Return " << endl;
return select_option();
}
void data_structures_interface() {
int choice = 0;
cout << "-------------------------------------------- " << endl;
cout << " Pick one Data Structure " << endl;
cout << "-------------------------------------------- " << endl;
cout << " 1. Table " << endl;
cout << " 2. Doubly Linked List " << endl;
cout << " 3. Heap " << endl;
cout << " 4. Red-Black Tree " << endl;
cout << "-------------------------------------------- " << endl;
cout << endl;
cout << " Press 0 to Return " << endl;
do {
cin >> choice;
if (choice >= 0 && choice <= 4) {
switch (choice) {
case 0: {
clearScreen();
mode_interface();
}
// ARRAY DATA STRUCTURE
case 1: {
clearScreen();
Array *array = new Array(file_name_and_extension);
char add_more = ' ';
int operation = operations_interface("Array");
do {
int visit_count = 0;
switch (operation) {
//Array OPTIONS
//#0 Return to previous window
case 0: {
clearScreen();
data_structures_interface();
delete array;
array = nullptr;
}
//#1 Add to array
case 1: {
array->add();
break;
}
//#2 Add to array by index
case 2: {
int index = 0;
cout << "index: ";
cin >> index;
if (index < 0 || index > array->size) {
cout << "Incorrect index !" << endl;
} else {
array->add(index);
}
break;
}
//#3 Remove element
case 3: {
array->remove();
break;
}
//#4 Remove element by index
case 4: {
int index = 0;
cout << "Index of removing element: ";
cin >> index;
array->remove(index);
break;
}
//#5 Find element by value
case 5: {
array->find();
break;
}
//#6 Find element by index
case 6: {
int index = 0;
cout << "Index of the value: ";
cin >> index;
array->find(index);
break;
}
case 7: {
array->show();
break;
}
default:
cout << "No option found try again ! [Press 0 to Return]" << endl;
}
} while (operation = operations_interface("Array"));
data_structures_interface();
break;
}
//*******************************************************************************************************************
//LIST DATA STRUCTURE
case 2: {
clearScreen();
ListTwoDirection *list = new ListTwoDirection(file_name_and_extension);
int operation = operations_interface("List");
do {
switch (operation) {
//List OPTIONS
//#0 Return to previous screen
case 0: {
clearScreen();
data_structures_interface();
delete list;
list = nullptr;
}
//#1 Add to list [LIST]
case 1: {
int value = 0;
cout << "list.add(" << list->get_size() << ") = ";
cin >> value;
list->add(value, list->get_size());
break;
}
// #2 Add to list by index [LIST]
case 2: {
int value = 0;
int index = 0;
do {
cout << "Index: ";
cin >> index;
if (index < 0 || index > list->get_size()) {
cout << "Incorrect index ! Try again" << endl;
}
} while (index > list->get_size() || index < 0);
cout << "list.add(" << index << ") = ";
cin >> value;
list->add(value, index);
break;
}
//#3 Remove element [LIST]
case 3: {
int index = list->get_size() - 1;
int times = 0;
if (list->get_size() > 0) {
cout << "How many times do you want to delete :" << endl;
cin >> times;
if (times <= list->get_size()) {
list->delete_manager(index, times);
} else {
cout << "There are only " << list->get_size() << " elements in the list"
<< endl;
}
} else {
cout << "List is empty !" << endl;
}
break;
}
//#4 Remove element by index [LIST]
case 4: {
int index = 0;
int times = 0;
if (list->get_size() > 0) {
do {
cout << "Index: ";
cin >> index;
if (index < 0 || index > list->get_size()) {
cout << "Incorrect index ! Try again" << endl;
}
} while (index > list->get_size() || index < 0);
cout << "How many times do you want to delete :" << endl;
cin >> times;
if (times <= list->get_size()) {
list->delete_manager(index, times);
} else {
cout << "There are only " << list->get_size() << " elements in the list"
<< endl;
}
} else {
cout << "List is empty !" << endl;
}
break;
}
// #5 Find element [LIST]
case 5: {
if (list->get_size() > 0) {
list->find();
} else {
cout << "List is empty nothing to find !" << endl;
}
break;
}
//#6 Find by index
case 6: {
int index = 0;
if (list->get_size() > 0) {
do {
cout << "Value index: ";
cin >> index;
if (index < 0 || index > list->get_size()) {
cout << "Incorrect index ! Try again" << endl;
}
} while (index > list->get_size() || index < 0);
list->find(index);
} else {
cout << "List is empty nothing to find !" << endl;
}
break;
}
//#7 Show List
case 7: {
list->show();
break;
}
default:
cout<<"No operation found !"<<endl;
}
} while (operation = operations_interface("List"));
data_structures_interface();
break;
}
//*******************************************************************************************************************
//HEAP DATA STRUCTURE
case 3: {
clearScreen();
Heap *heap = new Heap(file_name_and_extension);
int operation = operations_interface_tree("Heap");
do {
switch (operation) {
//Heap OPTIONS
//#0 Return to previous screen
case 0: {
clearScreen();
data_structures_interface();
delete heap;
heap = nullptr;
}
//#1 Add to heap [HEAP]
case 1: {
heap->add();
break;
}
//#2 Remove element [HEAP]
case 2: {
heap->remove(heap->get_size() - 1);
break;
}
//#3 Remove element by index [HEAP]
case 3: {
int index = 0;
cout << "Index: ";
cin >> index;
heap->remove(index);
break;
}
// #4 Find element [HEAP]
case 4: {
if (heap->get_size() > 0) {
heap->find();
} else {
cout << "Heap is empty nothing to find !" << endl;
}
break;
}
//#5 Find element by index [HEAP]
case 5: {
int index = 0;
if (heap->get_size() > 0) {
cout << "Index: ";
cin >> index;
if (index >= 0 && index < heap->get_size()) {
heap->find(index);
}
} else {
cout << "Heap is empty nothing to find !" << endl;
}
break;
}
//#6 Show heap [HEAP]
case 6: {
char show = 'y';
if (heap->get_size() > 100) {
cout << "There are " << heap->get_size()
<< " values in the heap do you want to continue ? [y/n]" << endl;
cin >> show;
while (show != 'y' || show != 'n') {
cout << "Incorrect character. Try again !" << endl;
cin >> show;
}
if (show == 'y') {
heap->show();
}
} else {
heap->show();
}
break;
}
}
} while (operation = operations_interface_tree("Heap"));
data_structures_interface();
break;
}
//*******************************************************************************************************************
//Red-Black Tree DATA STRUCTURE
case 4: {
clearScreen();
RedBlackTree *rbt = new RedBlackTree(file_name_and_extension);
int operation = operations_interface_RB_TREE("Red-Black Tree");
do {
switch (operation) {
case 0: {
clearScreen();
data_structures_interface();
delete rbt;
rbt = nullptr;
}
case 1: {
//Add value to R-B-tree
int data = 0;
cout << "Type a value you want to add: ";
cin >> data;
rbt->addNode(data);
break;
}
case 2: {
//Remove value from R-B-tree
int data = 0;
int tmp = 0;
cout << "Type a value you want to delete: ";
cin >> data;
rbt->deleteNode(data);
break;
}
case 3: {
//Find in R-B-tree by value
int data = 0;
cout << "Type a value you are looking for: ";
cin >> data;
int times = 0;
cout << "How many times should time be measured: " << endl;
cin >> times;
for (int i = 0; i < times; i++) {
auto start = std::chrono::steady_clock::now(); //START [LIST FIND BY VALUE]
if (rbt->find(rbt->getRoot(), data)) {
cout << "Value " << data << " is in the RED BLACK TREE" << endl;
} else {
cout << "Value not found" << endl;
}
auto end = std::chrono::steady_clock::now();
double elapsed_time = double(
std::chrono::duration_cast<std::chrono::nanoseconds>(
end - start).count());
timer->calculate_average_elapsed_time(elapsed_time, "FIND_IN_RED_BLACK_TREE");
timer->showAvgTime("FIND_IN_RED_BLACK_TREE");
}
break;
}
case 4: {
//Show R-B-tree
rbt->preorderTraversal(rbt->getRoot());
}
}
} while (operation = operations_interface_RB_TREE("Red-Black Tree"));
data_structures_interface();
break;
}
//*******************************************************************************************************************
}
} else {
cout << "No structure found try again !" << endl;
}
} while ((choice < 0 || choice > 5));
}
void clearScreen() {
cout << "\033[2J\033[1;1H";
}
};
#endif //DATASTRUCTURES_MENU_H