-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotationConverter.cpp
2015 lines (1650 loc) · 84.7 KB
/
NotationConverter.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "NotationConverter.hpp"
//Definitions for the doubly-linked list
//constructor for the list
DoubleList::DoubleList() {
header = new Element;
trailer = new Element;
header->element = '+';
trailer->element = '-';
header->next = trailer;
trailer->prev = header;
}
//destructor for the list
DoubleList::~DoubleList() {
while(!empty_list()) {
removeFrontList();
}
delete header;
delete trailer;
}
bool DoubleList::empty_list() const {
return (header->next == trailer);
}
const char &DoubleList::front_item() const {
return header->next->element;
}
const char &DoubleList::back_item() const {
return trailer->prev->element;
}
void DoubleList::addFrontList(const char &e) {
Element *new_el = new Element;
new_el->element = e;
new_el->next = header->next;
new_el->prev = header;
header->next->prev = new_el;
header->next = new_el;
}
void DoubleList::addBackList(const char &e) {
Element *new_el = new Element;
new_el->element = e;
new_el->next = trailer;
new_el->prev = trailer->prev;
trailer->prev->next = new_el;
trailer->prev = new_el;
}
const char &DoubleList::removeItem(Element *node) {
Element *temp_prev = node->prev;
Element *temp_next = node->next;
temp_prev->next = temp_next;
temp_next->prev = temp_prev;
Element *temp = node;
delete node;
return temp->element;
}
const char &DoubleList::removeFrontList() {
return removeItem(header->next);
}
const char &DoubleList::removeBackList() {
// if (trailer->next == header) {
// throw "List Empty!";
// }
return removeItem(trailer->prev);
}
void DoubleList::printItems() {
Element *iterator = new Element;
iterator = header->next;
while (iterator != trailer) {
cout<<""<<iterator->element;
iterator = iterator->next;
}
cout<<"\n";
}
//Defintions for the deque
//constructor
LinkedDeque::LinkedDeque(): list(), deque_size(0) {} //initializations of member data
//returns the current size of the deque
int LinkedDeque::size() const {
return deque_size;
}
//evaluates if the deque is empty
bool LinkedDeque::empty() const {
return deque_size == 0;
}
//returning the element at the front of the deque
const char &LinkedDeque::front() const {
if (empty()) {
throw "Deque Empty!";
}
return list.front_item();
}
//returning the element at the back of the deque
const char &LinkedDeque::back() const {
if (empty()) {
throw ("Deque Empty!");
}
return list.back_item();
}
//inserting to the front of the deque
void LinkedDeque::insertFront(const char &e) {
list.addFrontList(e);
deque_size++; //increment the size of the deque
}
//inserting to the back of the deque
void LinkedDeque::insertBack(const char &e) {
deque_size++;
list.addBackList(e);
}
//removing from the front of the deque
const char &LinkedDeque::removeFront() {
if (empty()) {
throw ("Deque Empty!");
}
deque_size--;
return list.removeFrontList();
}
//removing from the back of the deque
const char &LinkedDeque::removeBack() {
if (empty()) {
throw ("Deque Empty!");
}
deque_size--;
return list.removeBackList();
}
void LinkedDeque::printDeque() {
list.printItems();
}
//Definitions for the NotationConverter
//constructor
//it will cause the call of the constructor of the deque, and set original value to default
NotationConverter::NotationConverter(): dequeConverted(), stackOperators(), dequeOriginal() {}
//CAN BE REMOVED!!!
//printer method will cause the call of the printer method of the deques
void NotationConverter::Printer() {
cout<<"\nPrinting the orginal notation deque: ";
dequeOriginal.printDeque();
cout<<"\nPrinting the operators stack:";
stackOperators.printDeque();
cout<<"\nPrinting the converted deque: ";
dequeConverted.printDeque();
}
//postfix notation to infix notation
string NotationConverter::postfixToInfix(string inStr) {
char iterat;
//first we will collect the original notation in a neat form in a seperate stack, originalNotation
//a local variable "sizeOriginal" will help in managing the originalNotation deque, and "sizeOperators"
//will do so for the stackOperators
int sizeOriginal = 0;
int sizeOperators = 0;
for (unsigned int i = 0; i < inStr.length(); i++) {
iterat = inStr[i];
//if the character is among lowercase or uppercase letters
if ((iterat >= 'A' && iterat <= 'Z') || (iterat >= 'a' && iterat <= 'z')) {
dequeOriginal.insertBack(iterat);
sizeOriginal++;
} else if (iterat == '(' || iterat == ')' || iterat == '+' || iterat == '-' || iterat == '*' || iterat == '/') {
dequeOriginal.insertBack(iterat);
sizeOriginal++;
sizeOperators++;
} else if (iterat == ' ') { continue; }
else {
cout<<"\nINVALID NOTATION!!!\n"; //MUST THROW AN ERROR
}
}
//In this type of conversion we are less concerned about the precedence rules of
//operations but are concerned about the role of parantheses
//In this implementation we will stackOperators as a general stack to store
//our elements, rearrange them, and overall manage them with operators
//The dequeConverted will serve as a temporary container (stack) that will
//temporarily store elements from the stack, so stack can manage the elements
//one by one, like inserting parantheses wherever needed
//Generally we go through elements from the dequeOriginal one element at a
//time: if we encounter an operator, we simply push it into the temporary container,
//dequeConverted, and if we encounter an operand, we pop 2 elements from the stack,
//enque (add to the front) in the dequeConverted, put necessary parantheses and pop the
//two elements back from the dequeConverted
int trackStackSize = 0;
char holder;
char manager;
bool met_inner_paranthesis = false;
for (int i = 0; i < sizeOriginal; i++) {
//cout<<"\nCYCLE";
holder = dequeOriginal.removeFront();
//cout<<"\nHOLDER: "<<holder;
//if the holder value is among alphabetic letters, the holder holds an operand
//we simply push it into the stack
if ((holder >= 'A' && holder <= 'Z') || (holder >= 'a' && holder <= 'z')) {
//cout<<"\nINSERTING THIS INTO THE STACK: "<<holder;
stackOperators.insertBack(holder);
trackStackSize++;
}
//else the holder encountered an operation: we turn to the stack, pop two elements from there,
//and add to the front of the dequeConverted
else {
//cout<<"\nHOLDER IN ELSE BLOCK: "<<holder<<endl;
trackStackSize++; //???? DO WE INCREMENT THE TRACK STACK SIZE?
manager = stackOperators.removeBack();
//cout<<"\n\nMANAGER HERE: "<<manager;
//we check if the element we have got from the stack is a closed paranthesis
//if we did, we will keep pulling elements until we encounter its paired open
//paranthesis
if (manager == ')') {
dequeConverted.insertFront(manager);
while(manager != '(') {
manager = stackOperators.removeBack();
//we check for any other inner parantheses
//if we encounter another closed paranthesis,
//we make the bool variable true until we ecounter its pair
if (manager == ')') {
met_inner_paranthesis = true;
while(met_inner_paranthesis == true) {
dequeConverted.insertFront(manager);
manager = stackOperators.removeBack();
if (manager == '(') {
met_inner_paranthesis = false;
dequeConverted.insertFront(manager);
manager = stackOperators.removeBack();
if(manager == '(') {
dequeConverted.insertFront(manager);
} else {
stackOperators.insertBack(manager);
}
}
}
} else {
dequeConverted.insertFront(manager);
}
}
//assuming the current value of the manager is '(' after the loop
//dequeConverted.insertFront(manager);
manager = stackOperators.removeBack();
if (manager == ')') {
dequeConverted.insertFront(manager);
while(manager != '(') {
manager = stackOperators.removeBack();
if (manager == ')') {
met_inner_paranthesis = true;
while(met_inner_paranthesis == true) {
dequeConverted.insertFront(manager);
manager = stackOperators.removeBack();
if (manager == '(') {
met_inner_paranthesis = false;
dequeConverted.insertFront(manager);
manager = stackOperators.removeBack();
if(manager == '(') {
dequeConverted.insertFront(manager);
} else {
stackOperators.insertBack(manager);
}
}
}
} else {
dequeConverted.insertFront(manager);
}
}
} else {
dequeConverted.insertFront(manager);
}
} else {
dequeConverted.insertFront(manager);
//we take the seocond operand from the stack
manager = stackOperators.removeBack();
//we check if the second operand is also enclosed into the parantheses
if (manager == ')') {
dequeConverted.insertFront(manager);
while(manager != '(') {
manager = stackOperators.removeBack();
//we check for any additional parantheses
if (manager == ')') {
met_inner_paranthesis = true;
while(met_inner_paranthesis == true) {
dequeConverted.insertFront(manager);
manager = stackOperators.removeBack();
if (manager == '(') {
met_inner_paranthesis = false;
dequeConverted.insertFront(manager);
manager = stackOperators.removeBack();
if (manager == '(') {
dequeConverted.insertFront(manager);
} else {
stackOperators.insertBack(manager);
}
}
}
} else {
dequeConverted.insertFront(manager);
}
}
//dequeConverted.insertFront(manager);
} else {
dequeConverted.insertFront(manager);
}
}
//now we add an open paranthesis to the stack
stackOperators.insertBack('(');
trackStackSize++;
//now we push the two operands back from the dequeConverted
//first we take one element
//we need to check if we are pulling an open paranthesis from the dequeConverted
//in this case, we will keep pulling until we encounter its paired closed paranthesis
manager = dequeConverted.removeFront();
//cout<<"\n\n\nMANAGER FROM THE DEQUECONVERTED: "<<manager<<endl;
if (manager == '(') {
stackOperators.insertBack(manager);
while(manager != ')') {
manager = dequeConverted.removeFront();
//we check for any other inner parantheses
//if we encounter another closed paranthesis,
//we make the bool variable true until we ecounter its pair
if (manager == '(') {
met_inner_paranthesis = true;
while(met_inner_paranthesis == true) {
stackOperators.insertBack(manager);
manager = dequeConverted.removeFront();
if (manager == ')') {
met_inner_paranthesis = false;
stackOperators.insertBack(manager);
manager = dequeConverted.removeFront();
if(manager == '(') {
dequeConverted.insertFront(manager);
} else {
stackOperators.insertBack(manager);
}
}
}
} else {
stackOperators.insertBack(manager);
}
}
//assuming the current value of the manager is ')' after the loop
//stackOperators.insertBack(manager);
} else {
stackOperators.insertBack(manager);
}
//now we insert the operation, which is holder at this point, in between
stackOperators.insertBack(holder);
//now we can insert the second operand
//again we check if the element we are pulling is an open paranthesis,
//in which case will will keep pulling until we encounter its paired closed
//paranthesis
manager = dequeConverted.removeFront();
if (manager == '(') {
stackOperators.insertBack(manager);
while(manager != ')') {
manager = dequeConverted.removeFront();
//we check for any other inner parantheses
//if we encounter another closed paranthesis,
//we make the bool variable true until we ecounter its pair
if (manager == '(') {
met_inner_paranthesis = true;
while(met_inner_paranthesis == true) {
stackOperators.insertBack(manager);
manager = dequeConverted.removeFront();
if (manager == ')') {
met_inner_paranthesis = false;
stackOperators.insertBack(manager);
manager = dequeConverted.removeFront();
if(manager == ')') {
stackOperators.insertBack(manager);
} else {
stackOperators.insertBack(manager);
}
}
}
} else {
stackOperators.insertBack(manager);
}
}
//assuming the current value of the manager is ')' after the loop
//stackOperators.insertBack(manager);
} else {
stackOperators.insertBack(manager);
}
//now we add the closing paranthesis to complete a new operand in the stack
stackOperators.insertBack(')');
trackStackSize++;
}
}
string postfix_to_infix;
for (int i = 0; i < trackStackSize; i++) {
cout<<stackOperators.removeFront();
}
string valid = "";
char el;
//now we put the necessary spaces between the elements
for (int i = 0; i < trackStackSize; i++) {
el = postfix_to_infix[i];
valid += el;
if (el == '(') {
while(el == '(') {
i++;
el = postfix_to_infix[i];
if (el != '(') {
valid += " ";
}
valid += el;
}
} else if (el == ')') {
while(el == ')') {
i++;
el = postfix_to_infix[i];
if (el != ')') {
valid += " ";
}
valid += el;
}
}
valid += " ";
}
return valid;
}
//postfix notation to prefix nottion
string NotationConverter::postfixToPrefix(string inStr) {
//we can first convert the expression to infix and then from infix to prefix
string postfix_to_infix = postfixToInfix(inStr);
string infix_to_prefix = infixToPrefix(postfix_to_infix);
string valid = "";
char el;
//now we put the necessary spaces between the elements
for (unsigned int i = 0; i < infix_to_prefix.length(); i++) {
el = infix_to_prefix[i];
valid += el;
if (el == '(') {
while(el == '(') {
i++;
el = infix_to_prefix[i];
if (el != '(') {
valid += " ";
}
valid += el;
}
} else if (el == ')') {
while(el == ')') {
i++;
el = infix_to_prefix[i];
if (el != ')') {
valid += " ";
}
valid += el;
}
}
valid += " ";
}
return valid;
}
//infix notation to postfix notation
string NotationConverter::infixToPostfix(string inStr) {
char iterat;
//first we will collect the original notation in a neat form in a seperate stack, originalNotation
//a local variable "sizeOriginal" will help in managing the originalNotation deque, and "sizeOperators"
//will do so for the stackOperators
int sizeOriginal = 0;
int sizeOperators = 0;
for (unsigned int i = 0; i < inStr.length(); i++) {
iterat = inStr[i];
//if the character is among lowercase or uppercase letters
if ((iterat >= 'A' && iterat <= 'Z') || (iterat >= 'a' && iterat <= 'z')) {
dequeOriginal.insertBack(iterat);
sizeOriginal++;
} else if (iterat == '(' || iterat == ')' || iterat == '+' || iterat == '-' || iterat == '*' || iterat == '/') {
dequeOriginal.insertBack(iterat);
sizeOriginal++;
sizeOperators++;
} else if (iterat == ' ') { continue; }
else {
cout<<"\nINVALID NOTATION!!!\n"; //MUST THROW AN ERROR
}
}
//We will pop the operators from the dequeOriginal into the stackOperators, and operands into the dequeConverted
//if there are multiple operators in the operatorsStack, the operator on top will be popped
//and pushed into the dequeConverted,according to precedence rules of operators
//Rules of Operators:
//1st '(' or ')'
//2nd '*' or '/'
//3rd '+' or '-'
//if we encounter a closed paranthesis, we will keep poping the operators form the stackOperators
//until we encounter an open paranthesis in the stackOperators
char holder;
char stackItem;
int trackSizeConverted = 0;
int trackSizeOper = 0;
//because we already took care of invalid characters, we can assume all charactersother than
//operators are variables of the expression
for (int i = 0; i < sizeOriginal; i++) {
holder = dequeOriginal.removeFront();
//if not an operator, then a variables to be pushed into the dequeConverter directly
if (holder != '(' && holder != ')' && holder != '/' && holder!= '*' && holder != '+' && holder != '-') {
dequeConverted.insertBack(holder);
trackSizeConverted++;
}
//if an operator, we check with the stackOperators
else {
//if the tracker of the operators size is 0, then the stack is empty, not need to check the stack
if (trackSizeOper == 0) {
stackOperators.insertBack(holder);
trackSizeOper++;
} else {
//the stack is not empty, therefore we check the precedences
//we pop the operator from the stack, and see of the precedence of that operator is less than that of the operator at hand
//if less, we simply push the operator at hand into the stack
stackItem = stackOperators.removeBack();
trackSizeOper--;
switch (stackItem) {
case '(':
//if it is any paranthesis, we simply push it back and push the operator at hand into the stack
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
//We can assume the closed paranthesis will not be among the stack items
case '*':
//we now compare the precedence of the operator at hand
switch (holder) {
case '*':
//precedence of the operator at hand and that of the stack operator are equal
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//the stackOperators is empty, we simply push the operator at hand into the stackOperator
if (trackSizeOper == 0) {
stackOperators.insertBack(holder);
trackSizeOper++;
}
// ??? WE WILL SEE IF WE WILL NEED TO CHECK FURTHER INSIDE THE OPERATORS STACK
//otherwise we check if the precedence of the leftover operators is higher or lower than that
//of the operator at hand
break;
case '/':
//precedence of the operator at hand and that of the stack operator are equal
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//the stackOperators is empty, we simply push the operator at hand into the stackOperator
if (trackSizeOper == 0) {
stackOperators.insertBack(holder);
trackSizeOper++;
}
// ??? WE WILL SEE IF WE WILL NEED TO CHECK FURTHER INSIDE THE OPERATORS STACK
//otherwise we check if the precedence of the leftover operators is higher or lower than that
//of the operator at hand
break;
case '+':
//precedence of the stack item is higher than that of the operator at hand
//the stack operator will be pushed into the converted deque
//the operator at hand will be pushed into the operator stack
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '-':
//precedence of the stack item is higher than that of the operator at hand
//the stack operator will be pushed into the converted deque
//the operator at hand will be pushed into the operator stack
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '(':
//if it is an open paranthesis, we simply push it into the operator stack
//we push the stack operator back first
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case ')':
//if it is a closed paranthesis, we keep popping operators from the operator
//stack until we encounter an open paranthesis, at which point we get rid of
//the open paranthesis from the operator stack, as well as not push it into
//the converted deque
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackItem = stackOperators.removeBack();
trackSizeOper--;
//two conditions to terminate the loop: stack is empty or encountered open paranthesis
while (true) {
//cout<<"INSIDE THE BLOCK IN THE MULTIPLICATION BLOCK";
//we also check if the stack has become empty
if (trackSizeOper == 0) {
break;
}
if (stackItem == '(') {
break;
} else {
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
}
stackItem = stackOperators.removeBack();
trackSizeOper--;
}
break;
}
break;
case '/':
//we now compare the precedence of the operator at hand
switch (holder) {
case '*':
//precedence of the operator at hand and that of the stack operator are equal
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//we push the operator at hand into the stackOperator
stackOperators.insertBack(holder);
trackSizeOper++;
// ??? WE WILL SEE IF WE WILL NEED TO CHECK FURTHER INSIDE THE OPERATORS STACK
//otherwise we check if the precedence of the leftover operators is higher or lower than that
//of the operator at hand
break;
case '/':
//precedence of the operator at hand and that of the stack operator are equal
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//we push the operator at hand into the stackOperator
stackOperators.insertBack(holder);
trackSizeOper++;
// ??? WE WILL SEE IF WE WILL NEED TO CHECK FURTHER INSIDE THE OPERATORS STACK
//otherwise we check if the precedence of the leftover operators is higher or lower than that
//of the operator at hand
break;
case '+':
//precedence of the stack item is higher than that of the operator at hand
//the stack operator will be pushed into the converted deque
//the operator at hand will be pushed into the operator stack
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '-':
//precedence of the stack item is higher than that of the operator at hand
//the stack operator will be pushed into the converted deque
//the operator at hand will be pushed into the operator stack
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '(':
//if it is an open paranthesis, we simply push it into the operator stack
//we push the stack operator back first
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case ')':
//if it is a closed paranthesis, we keep popping operators from the operator
//stack until we encounter an open paranthesis, at which point we get rid of
//the open paranthesis from the operator stack, as well as not push it into
//the converted deque
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackItem = stackOperators.removeBack();
trackSizeOper--;
//two conditions to terminate the loop: stack is empty or encountered open paranthesis
while (true) {
//we also check if the stack has become empty
if (trackSizeOper == 0) {
break;
}
if (stackItem == '(') {
break;
} else {
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
}
stackItem = stackOperators.removeBack();
trackSizeOper--;
}
break;
}
break;
case '+':
//we now compare the precedence of the operator at hand
switch (holder) {
case '+':
//the precedence of the operator at hand is equal to that of the stack operator
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//we push the operator at hand into the stackOperator
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '-':
//the precedence of the operator at hand is equal to that of the stack operator
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//we push the operator at hand into the stackOperator
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '*':
//the precedence of the operator at hand is higher than that of the stack operator
//we simply push both of them into the operator stack, in corresponding order
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeConverted++;
break;
case '/':
//the precedence of the operator at hand is higher than that of the stack operator
//we simply push both of them into the operator stack, in corresponding order
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeConverted++;
break;
case '(':
//if it is an open paranthesis, we simply push it into the operator stack
//we push the stack operator back first
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case ')':
//if it is a closed paranthesis, we keep popping operators from the operator
//stack until we encounter an open paranthesis, at which point we get rid of
//the open paranthesis from the operator stack, as well as not push it into
//the converted deque
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackItem = stackOperators.removeBack();
trackSizeOper--;
//two conditions to terminate the loop: stack is empty or encountered open paranthesis
while (true) {
//we also check if the stack has become empty
if (trackSizeOper == 0) {
break;
}
if (stackItem == '(') {
break;
} else {
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
}
stackItem = stackOperators.removeBack();
trackSizeOper--;
}
break;
}
break;
case '-':
//we now compare the precedence of the operator at hand
switch (holder) {
case '+':
//the precedence of the operator at hand is equal to that of the stack operator
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//we push the operator at hand into the stackOperator
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '-':
//the precedence of the operator at hand is equal to that of the stack operator
//we push the stack item into the dequeConverted
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
//we push the operator at hand into the stackOperator
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case '*':
//the precedence of the operator at hand is higher than that of the stack operator
//we simply push both of them into the operator stack, in corresponding order
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeConverted++;
break;
case '/':
//the precedence of the operator at hand is higher than that of the stack operator
//we simply push both of them into the operator stack, in corresponding order
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeConverted++;
break;
case '(':
//if it is an open paranthesis, we simply push it into the operator stack
//we push the stack operator back first
stackOperators.insertBack(stackItem);
trackSizeOper++;
stackOperators.insertBack(holder);
trackSizeOper++;
break;
case ')':
//if it is a closed paranthesis, we keep popping operators from the operator
//stack until we encounter an open paranthesis, at which point we get rid of
//the open paranthesis from the operator stack, as well as not push it into
//the converted deque
dequeConverted.insertBack(stackItem);
trackSizeConverted++;
stackItem = stackOperators.removeBack();
trackSizeOper--;
//two conditions to terminate the loop: stack is empty or encountered open paranthesis
while (true) {
//we also check if the stack has become empty
if (trackSizeOper == 0) {
break;
}