-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithms.cpp
executable file
·863 lines (817 loc) · 26.6 KB
/
Algorithms.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
#include "Calculator.h"
//these three things below are declared and inherited in & from the header file:
//stack<char> mystack; //for operators (each are 1 digit) ; used exclusively in shuntingYard method
//stack<string> mystack2; //used exclusively in reversePolish method
//queue<string> myqueue; //for operands, no matter how long they may be ; used in both methods
Algorithm::Algorithm() {
//nothing to initialize
}
Algorithm::~Algorithm() {
//nothing to delete
}
//this method is COMPLETE. Only needs to be updated with little fixes to prevent intentional crashing.
void Algorithm::shuntingYard(string expression) { //see wikipedia page for english interpretion of this algorithm (I based it almost entirely off that)
string strTemp = "";
string strTemp2 = "";
string strTemp3 = "";
int expressionLength = expression.length();
char variable = ' ';
for (int i = 0; i < expressionLength; i++) { //sort through entire expression (tokens left to read)
//sorts operators and numbers according to PEMDAS
variable = expression.at(i);
if (variable != ' ') { // operand or operator is being entered
if (variable == '^') { //exponent
mystack.push(variable);
}
else if (variable == '*' || variable == '/') { //mult or divis
if (mystack.top() == '+' || mystack.top() == '-' || mystack.empty() || mystack.top() == '(' ) {
mystack.push(variable);
}
else if (mystack.size() >= 1) {
strTemp2 = mystack.top();
myqueue.push(strTemp2);
mystack.pop();
strTemp2 = "";
mystack.push(variable);
}
else {
// throw exception("Error: at MD level");
}
}
else if (variable == '+' || variable == '-') { //add or subs
if (isNumNext(i, expression)) { //if num directly after "-" with no space, then -num will be num
strTemp += variable;
}
else if (mystack.empty() || mystack.top() == '(') {
mystack.push(variable);
}
else {
strTemp2 = mystack.top();
myqueue.push(strTemp2);
mystack.pop();
strTemp2 = "";
mystack.push(variable);
}
}
else if (variable == '(') { //open paren
mystack.push(expression.at(i));
}
else if (variable == ')') { //close paren
if (i == 2) {
// throw exception("Error: illegal start of expression");
}
if (mystack.empty()) {
// throw exception("Error: mismatching parentheses");
}
while (mystack.top() != '(' && mystack.size() > 1) {
strTemp2 = mystack.top();
myqueue.push(strTemp2);
mystack.pop();
strTemp2 = "";
}
if (mystack.empty()) { //no operators left
// throw exception("Error: mismatched parentheses");
} else if (mystack.top() == '(') { //last operator
mystack.pop();
} else { //???
// throw exception("Error: mismatched parentheses"); //used to be "figure out who I am"
//cout << mystack.top() << endl;
}
}
else if (variable == '$') { //nth root ; has same precedence as normal exponent
mystack.push(variable);
}
else if (variable == '#') { //square root ; has same precedence as normal exponent
mystack.push(variable);
}
else if (variable == 'L') { //log ; has same precedence as normal exponent
mystack.push(variable);
}
else if (variable == '!') { ////factorial ; has same precedence as normal exponent
mystack.push(variable);
}
else { //either number, variable, decimal point, or symbolic number is being entered
strTemp += variable;
if (expression.at(i + 1) == ')') { //test this
if (!strTemp.empty()) {
myqueue.push(strTemp);
strTemp = "";
}
}
}
}
//updates queue
if (i < expressionLength - 1 && variable != '^') { //not last iteration ; allow for slighly different exponent input
if (variable == ' '){ //enter newly complete operand into queue
if (!strTemp.empty()) {
myqueue.push(strTemp);
strTemp = "";
}
}
} else { //for final iteration so an extra ' ' is not required from user
if (!strTemp.empty()) {
myqueue.push(strTemp);
strTemp = "";
}
}
}
//pop entire stack into the queue (no tokens left to read)
while (!mystack.empty()) {
if (mystack.top() == '(' || mystack.top() == ')') {
// throw exception("Error: mismatched parentheses");
}
strTemp3 = mystack.top();
myqueue.push(strTemp3);
mystack.pop();
strTemp3 = "";
}
//mystack should now be empty
// these next 5 lines only exist for testing purposes, will not be part of final solution
/*int j = myqueue.size();
for (int i = 0; i < j; i++) {
cout << myqueue.front() << endl;
myqueue.pop();
} */
}
//method has been proven functional (with integer-only addition)
string Algorithm::reversePolish(string expression) { //returned string will give ultimate answer
string value2 = "";
string value1 = "";
string action1 = "";
char action = ' ';
while (!myqueue.empty()) {
if (!isOperator()) { //if queue element is a number, then add to stack, delete from queue
mystack2.push(myqueue.front());
myqueue.pop();
}
else { //if queue element is an action (operator), then evaluate
if (mystack2.size() < 2 ) { //&& expression.at(3) != '-') {
// throw exception("Error: user has not input sufficient values in the expression");
}
else {
value2 = mystack2.top(); //value2 is newest value on stack
mystack2.pop();
value1 = mystack2.top(); //value1 is second newest value on stack
mystack2.pop();
action1 = myqueue.front();
myqueue.pop();
//determine which operator class to use (initially)
//call matching calculate method and stick its result back in mystack2
//e.g. mystack2.push(specificOperationClassInstancePointer->calculate(value1, value2, action));
//if necessary, the operations classes will call each other to solve/simplify the given expression
//one string will ultimately be returned to this (reverse polish interpretation) method,
//no matter amount of places (classes) or times the expression needs to be evaluated/simplifed
if (action1.length() == 1) { //convert string to char so easy switch is possible
action = action1[0];
}
//additional pointers and cases will be created when the pertinent class files are available
//Adding* add = new Adding();
Logn* log = new Logn();
Exponents* expo = new Exponents();
NthRoot* nth = new NthRoot();
Factorial* fac = new Factorial();
Multiplication* mul = new Multiplication();
Addition* add = new Addition();
Division* div = new Division();
Subtraction* sub = new Subtraction();
switch (action) {
case '+': mystack2.push(add->calculate(value1, value2, action1));
break;
case '-': mystack2.push(sub->calculate(value1, value2, action1));
break;
case '*': mystack2.push(mul->calculate(value1, value2, action1));
break;
case '/': mystack2.push(div->calculate(value1, value2, action1));
break;
case '^': mystack2.push(expo->calculate(value1, value2, action1));
break;
case '$': mystack2.push(nth->calculate(value1, value2, action1));
break;
case '#' : //obsolete
break;
case 'L': mystack2.push(log->calculate(value1, value2, action1));
break;
case '!': mystack2.push(fac->calculate(value1, value2, action1));
break;
default: cout << "Error: none of the preset switch cases were chosen" << endl;
break;
}
//reset placeholders to empty states
value1 = "";
value2 = "";
action1 = "";
action = ' ';
}
}
}
int remainingSize = mystack2.size();
if (remainingSize == 1) {
result = mystack2.top();
return result;
}
else {
// throw exception("Error: the user input has too many values (and may be missing an operator)");
}
return "";
}
string Algorithm::formatExpression(string expression) {
string* exp = &expression; //init expression pointer
ensureEnclosingParens(exp); //handle parentheses
//formatNegOper(exp);
formatNEGfront(exp);
turningSign(exp);
formatSQRT(exp);
formatNRT(exp);
formatLOGS(exp);
formatFAC(exp);
//formatANSfromN(exp);
//formatANS(exp);
formatANSfromNMult(exp);
//formatANSfromN(exp);
formatNEG(exp);
formatCompress(exp);
formatMultOpe(exp);
//formatFOIL(exp);
formatImpMult(exp);
formatFOIL(exp);
ensureEnclosingParens(exp);
return *exp;
}
bool Algorithm::enclosingParens(string expression) {
char firstChar = expression.at(0);
int expressionLength = expression.length();
char lastChar = expression.at(expressionLength - 1);
if (firstChar == '(' && lastChar == ')') {
return true;
}
return false;
}
void Algorithm::ensureEnclosingParens(string* exp) {
string expression = *exp;
if (!enclosingParens(expression)) {
expression = "( " + expression + " )";
}
*exp = expression;
}
string Algorithm::ensureEnclosingParens(string expression) {
if (!enclosingParens(expression)) {
expression = "( " + expression + " )";
}
return expression;
}
void Algorithm::formatSQRT(string* exp) {
string expression = *exp;
if (expression.find("sqrt:") != string::npos) {
replaceAll(*exp, "sqrt:", " 2 $ "); //substitute square root operator (modified nth root) if detected
ensureEnclosingParens(exp);
}
}
void Algorithm::formatNRT(string* exp) {
string expression = *exp;
if (expression.find("rt:") != string::npos) { //if "sq" does not precede phrase, substitute nth root operator if detected
if (expression.find("rt:") != expression.find("sqrt:")) {
replaceAll(*exp, "rt:", " $ ");
ensureEnclosingParens(exp);
}
}
}
void Algorithm::formatLOG(string* exp) {
string expression = *exp;
//cout << "formattedExpression: " << expression << endl;
if (expression.find("log_") != string::npos) { //substitute log operator if detected
//find isolated log string by first finding its length
int pos0 = exp->find("log_");
//cout << "pos0: " << pos0 << endl;
int i = pos0;
while (exp->at(i) != ' ') {
i++;
}
int logLength = i - pos0;
//cout << "logLenth: " << logLength << endl;
string logPart = exp->substr(pos0, logLength);
//cout << "logPart: " << logPart << endl;
//get num1
int posNum1 = pos0 + 4;
int colonPos = posNum1;
while (exp->at(colonPos) != ':') { //find the difference in location between the two numbers
colonPos++;
}
int num1Length = colonPos - posNum1;
string num1 = exp->substr(posNum1, num1Length);
//cout << "num1: " << num1 << endl;
//get num2
int posNum2 = colonPos + 1; //account for this space plus the space of the colon
int num2Length = 0;
for (int l = colonPos + 1; exp->at(l) != ' '; l++) {
num2Length++;
}
string num2 = exp->substr(posNum2, num2Length);
//substitute
string newEx = " " + num1 + " L " + num2 + " "; // "L" is the new symbol for log
replaceAll(*exp, logPart, newEx);
ensureEnclosingParens(exp);
//cout << "after formatLOG: " << *exp << endl;
}
}
void Algorithm::formatLOGS(string* exp) {
while (exp->find("log_") != string::npos) {
formatLOG(exp);
}
}
bool Algorithm::replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
string Algorithm::replaceAll(std::string& str, const std::string& from, const std::string& to) {
if (from.empty())
return "";
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
return str;
}
bool Algorithm::isOperator() {
string operators = "^*/+-#$L!";
string input = myqueue.front();
if (operators.find(input) == string::npos) return false; //string::npos is returned if no matched value is found
return true;
}
bool Algorithm::isNumNext(int i, string expression) {
if (isdigit(expression.at(i + 1)) || isalpha(expression.at(i + 1))) {
return true;
}
else return false;
}
bool Algorithm::ansFound(string* exp) {
if (exp->find("ans") != string::npos) {
return true;
}
else return false;
}
bool Algorithm::ansFoundNth(string* exp) {
if (exp->find("ans_") != string::npos) {
return true;
}
else return false;
}
bool Algorithm::wantDouble() {
char ans = ' ';
cout << "Want your answer as a decimal? Type Y or N." << endl;
cin >> ans;
ans = toupper(ans);
if (ans == 'Y') {
return true;
}
else if (ans == 'N') {
return false;
}
while (ans != 'Y' && ans != 'N') {
cout << "Please retype Y or N:" << endl;
cin >> ans;
ans = toupper(ans);
if (ans == 'Y') {
return true;
}
else if (ans == 'N') {
return false;
}
}
return false;
//cin.ignore();
}
double Algorithm::toDouble(string answer) {
/*double doubleAns = atof(answer.c_str());
return doubleAns;*/
/*double value;
try
{
value = boost::lexical_cast<double>(my_string);
}
catch (boost::bad_lexical_cast const&)
{
value = 0;
}*/
double num = stod(answer);
return num;
}
void Algorithm::formatMultOpe(string* exp) {
string expression = *exp;
int pos = 0;
int j = 0;
for (unsigned int i = 0; i < expression.size(); i++){
if (i + 2 <= expression.size()){ // operations
if ((expression.at(i) == '+' || expression.at(i) == '-' || expression.at(i) == '*' || expression.at(i) == '/')
&& !(isalpha(expression.at(i + 2)) || isdigit(expression.at(i + 2)) || expression.at(i + 2) == '(' || expression.at(i + 2) == ' '))
{
if (expression.at(i + 2) = '-' && !(isdigit(expression.at(i + 3)) || isalpha(expression.at(i + 3)))){
// throw exception("Error: two consecutive operators are not allowed");
}
}
if (i + 5 <= expression.size() - 1 && expression.at(i + 2) == '('){
if ((expression.at(i) == '+' || expression.at(i) == '-' || expression.at(i) == '*' || expression.at(i) == '/') &&
!(expression.at(i + 5) == 'L' || expression.at(i + 5) == '#' || expression.at(i + 5) == '$'
|| expression.at(i + 5) == ' ' || isdigit(expression.at(i + 5)) || isalpha(expression.at(i + 5)))) {
// throw exception("Error: two consecutive operators are not allowed");
}
}
if ((expression.at(i) == '(') && (expression.at(i + 2) == '+' || expression.at(i + 2) == '*' // for parenthesis
|| expression.at(i + 2) == '/' || expression.at(i + 2) == '^' ))
{
// throw exception("Error: two consecutive operators are not allowed");
}
}
if (i + 1 <= expression.size()){ // exceptions for exponents
if ((expression.at(i) == '^') && (expression.at(i + 1) == '+' || expression.at(i + 1) == '*'
|| expression.at(i + 1) == '/' || expression.at(i + 1) == '^' || expression.at(i + 1) == ')'))
{
// throw exception("Error: two consecutive operators are not allowed");
}
}
if (i + 2 <= expression.size()) {
if ((expression.at(i) == '#' || expression.at(i) == '$') && // for roots
(expression.at(i + 2) == '+' || expression.at(i + 2) == '*'
|| expression.at(i + 2) == '/' || expression.at(i + 2) == '^'
|| expression.at(i + 2) == ')'))
{
// throw exception("Error: two consecutive operators are not allowed");
}
/*if (expression.at(i) == 'L' && expression.at(i + 2) != '(') { // not sure if this works
if ((expression.at(i) == '#' || expression.at(i) == '$') &&
!(expression.at(i + 4) == '+' || expression.at(i + 4) == '-'
|| expression.at(i + 4) == '*' || expression.at(i + 4) == '/'
|| expression.at(i + 4) == '^' || expression.at(i + 4) == ')' || expression.at(i + 4) == '('))
{
throw exception("Error: two consecutive operators are not allowed");
}
}*/
// exception for logs
if (expression.at(i) == 'L' && expression.at(i + 2) == '(') {
j = i + 5;
while (expression.at(j) != ' ') {
j++;
}
if ((expression.at(i) == 'L') &&
!(expression.at(j + 1) == '+' || expression.at(j + 1) == '*'
|| expression.at(j + 1) == '/' || expression.at(j + 1) == '^' || expression.at(j + 1) == ')'
|| expression.at(j + 1) == '$' || expression.at(j + 1) == '#' || expression.at(j + 1) == 'L'))
{
// throw exception("Error: two consecutive operators are not allowed");
}
}
if ((expression.at(i) == 'L') &&
(expression.at(i + 2) == '+' || expression.at(i + 2) == '-' || expression.at(i + 2) == '*'
|| expression.at(i + 2) == '/' || expression.at(i + 2) == '^'
|| expression.at(i + 2) == ')'))
{
// throw exception("Error: two consecutive operators are not allowed");
}
}
}
}
void Algorithm::turningSign(string* exp) {
string expression = *exp;
if (expression.find("-+") != string::npos) {
replaceAll(*exp, "-+", "-");
ensureEnclosingParens(exp);
}
else if (expression.find("- +") != string::npos){
replaceAll(*exp, "- +", "-");
ensureEnclosingParens(exp);
}
if (expression.find("--") != string::npos) {
replaceAll(*exp, "--", "+");
ensureEnclosingParens(exp);
}
else if (expression.find("- -") != string::npos) {// && !isdigit(expression.find("- -") + 3) && !isalpha(expression.find("- -") + 3)){
if ((expression.find("- -")+3) == ' ') {
replaceAll(*exp, "- -", "+");
}
else {
replaceAll(*exp, "- -", "+ ");
}
//replaceAll(*exp, "- -", "+ ");
ensureEnclosingParens(exp);
}
if (expression.find(") (") != string::npos)
{
replaceAll(*exp, ") (", ") * (");
ensureEnclosingParens(exp);
}
else if (expression.find(")(") != string::npos)
{
replaceAll(*exp, ")(", ") * (");
ensureEnclosingParens(exp);
}
string str = "";
string str2 = "";
for (int i = 0; i < expression.size(); i++) {
if (i + 2 <= expression.size()- 1){
if ((expression.find(isdigit(i)) || expression.find(isalpha(i))) && expression.at(i + 2) == '(') {
//str = expression.at(i) + ' ' + expression.at(i + 2);
//str2 = expression.at(i) + ' ' + '*' + ' ' + expression.at(i + 2);
replaceAll(*exp, str, str2);
ensureEnclosingParens(exp);
}
}
}
}
void Algorithm::formatFAC(string* exp) {
string expression = *exp;
if (expression.find("!") != string::npos) {
replaceAll(*exp, "!", " ! 1 "); // substitute 1 as dummy placeholder so shunting yard doesn't get confused
ensureEnclosingParens(exp);
}
}
void Algorithm::formatNEG(string* exp) {
string expression = *exp;
/*if (expression.find("- (") == 2) {
int lgth = expression.length() -3 ;
string expression1 = expression.substr(5, lgth-5);
string newExp = "-1 * ( " + expression1 + " )";
replace(*exp, expression, newExp);
ensureEnclosingParens(exp);
}
else if (expression.find("-(") == 2) {
int lgth = expression.length() - 2;
string expression1 = expression.substr(4, lgth - 5);
string newExp = "-1 * ( " + expression1 + " )";
replace(*exp, expression, newExp);
ensureEnclosingParens(exp);
}*/
//else if (expression.find("-") == 2 && isdigit(expression.at(3)) || isalpha(expression.at(3))//there is are multiple digits/things afterwards) {
// //apply neg to whole operand e.g. -1 * (operand)
//}
if (expression.find("- (") != string::npos) {
if (expression.find("- (") == 2){
replaceAll(*exp, "- (", "-1 * (");
ensureEnclosingParens(exp);
}
/*else {
replaceAll(*exp, "- (", "+ (-1) * (");
ensureEnclosingParens(exp);
}*/
//replaceAll(*exp, "- (", "-1 * (");
//ensureEnclosingParens(exp);
}
if (expression.find("-(") != string::npos) {
//replaceAll(*exp, "-(", "-1 * (");
//ensureEnclosingParens(exp);
if (expression.find("-(") == 2){
replaceAll(*exp, "-(", "-1 * (");
ensureEnclosingParens(exp);
}
else {
replaceAll(*exp, "-(", "+ (-1) * (");
ensureEnclosingParens(exp);
}
}
}
void Algorithm::formatNEGfront(string* exp) {
string expression = *exp;
//if (expression.find("-") == 2 && isdigit(expression.at(3)) || isalpha(expression.at(3))) { //there is are multiple digits/things afterwards
// //apply neg to whole operand e.g. -1 * (operand)
// int j = 3;
// while (isdigit(expression.at(j)) || isalpha(expression.at(j))) {
// j++;
// }
// int lgth = j - 3;
// string expression1 = expression.substr(3, lgth - 3);
// string newExp = "-1 * ( " + expression1 + " )";
// replace(*exp, expression, newExp);
// ensureEnclosingParens(exp);
//}
if (expression.find("- (") == 2) {
//find length of operand
//substr from 5 to end of operand
//replace this part of string with modified substr
//if (expression.find("- ( ") == 2) {
// int j = 6;
// while (expression.at(j) != ' ' && j <= expression.length()) {
// j++;
// }
// int lgth = j - 6;
// string expression1 = expression.substr(6, lgth);
// string expression0 = expression.substr(2, lgth); //the substr + the part before with "-"
// string newExp = "-1 * ( " + expression1 + " )";
// replace(*exp, expression0, newExp);
// ensureEnclosingParens(exp);
//}
//else {
// int j = 5;
// while (expression.at(j) != ' ' && j <= expression.length()) {
// j++;
// }
// int lgth = j - 5;
// string expression1 = expression.substr(5, lgth);
// string expression0 = expression.substr(2, lgth); //the substr + the part before with "-"
// string newExp = "-1 * ( " + expression1 + " )";
// replace(*exp, expression0, newExp);
// ensureEnclosingParens(exp);
//
//}
//for (int i = 2; i < expression.length() - 1; i++) {
// if (expression.at(i) == '-' && isdigit(expression.at(i + 1)) || isalpha(expression.at(i + 1))) {
// //apply neg to whole operand e.g. -1 * (operand)
// int j = i + 1;
// while (expression.at(j) != ' ' && j <= expression.length() - 1) {
// j++;
// }
// int lgth = j - i + 1;
// /*int lgth2 = lgth;
// while (lgth2 < expression.length()) {
// lgth2++;
// }
// int afterOper = lgth2 - lgth;
// int operLgth = expression.length() - afterOper;*/
// string expression1 = expression.substr(i + 1, lgth);
// string newexp = "- ( " + expression1 + " )"; //written to work w/ formatNEGfront
// replace(*exp, expression1, newexp);
// ensureEnclosingParens(exp);
// }
}
if (expression.find("- (") == 2) {
int lgth = expression.length() - 3;
string expression1 = expression.substr(5, lgth - 5);
string newExp = "-1 * ( " + expression1 + " )";
replace(*exp, expression, newExp);
ensureEnclosingParens(exp);
}
else if (expression.find("-(") == 2) {
int lgth = expression.length() - 2;
string expression1 = expression.substr(4, lgth - 5);
string newExp = "-1 * ( " + expression1 + " )";
replace(*exp, expression, newExp);
ensureEnclosingParens(exp);
}
}
void Algorithm::formatCompress(string* exp) {
string expression = *exp;
for (int i = 0; i < expression.length(); i++) {
if (isdigit(expression.at(i)) || isalpha(expression.at(i))) { //if a number or digit is found at position i
if (expression.at(i + 1) == ')') {
stringstream ss;
string paren = "";
char c = expression.at(i + 1);
ss << c;
ss >> paren;
replace(*exp, paren, " )");
ensureEnclosingParens(exp);
}
}
}
}
void Algorithm::formatImpMult(string* exp) {
string expression = *exp;
int operLength;
for (int i = 0; i < expression.length()-1; i++) {
if (isdigit(expression.at(i)) || isalpha(expression.at(i))) {
int j = i;
while (isdigit(expression.at(j)) || isalpha(expression.at(j))) {
j++;
}
operLength = j - i;
stringstream ss;
if (expression.at(i + operLength) == ' ' && expression.at(i + operLength + 1) == '(') {
string spaceParen = " (";
char c = expression.at(i);
ss << c;
string num = ss.str();
ss << spaceParen;
string target = ss.str();
string rep = num + " * (";
replace(*exp, target, rep);
}
else if (expression.at(i + operLength) == '(') {
string paren = "(";
char c = expression.at(i);
ss << c;
string num = ss.str();
ss << paren;
string target = ss.str();
string rep = num + " * (";
replace(*exp, target, rep);
}
ensureEnclosingParens(exp);
}
}
}
void Algorithm::formatNegOper(string* exp) {//if no parentheses from - to the next space or end of expression, insert them after -
string expression = *exp;
for (int i = 2; i < expression.length() - 1; i++) {
if (expression.at(i) == '-' && isdigit(expression.at(i+1)) || isalpha(expression.at(i+1))) {
//apply neg to whole operand e.g. -1 * (operand)
int j = i+1;
while (expression.at(j) != ' ' && j <= expression.length()-1) {
j++;
}
int lgth = j - i - 1;
/*int lgth2 = lgth;
while (lgth2 < expression.length()) {
lgth2++;
}
int afterOper = lgth2 - lgth;
int operLgth = expression.length() - afterOper;*/
string expression1 = expression.substr(i+1, lgth);
string expression0 = expression.substr(i, lgth);
string newexp = "- ( " + expression1 + " )"; //written to work w/ formatNEGfront
replace(*exp, expression0, newexp);
ensureEnclosingParens(exp);
}
}
//if (expression.at(2) == '-' && isdigit(expression.at(3)) || isalpha(expression.at(3))) {
// //apply neg to whole operand e.g. -1 * (operand)
// int j = 3;
// while (expression.at(j) != ' ' && j <= expression.length()) {
// j++;
// }
// int lgth = j - 3;
// /*int lgth2 = lgth;
// while (lgth2 < expression.length()) {
// lgth2++;
// }
// int afterOper = lgth2 - lgth;
// int operLgth = expression.length() - afterOper;*/
// string expression1 = expression.substr(3, lgth);
// string newexp = "- ( " + expression1 + " )"; //written to work w/ formatNEGfront
// replace(*exp, expression1, newexp);
// ensureEnclosingParens(exp);
//
//}
}
bool Algorithm::isSelection(string selection) {
string selections = "AaBbCcQq";
if (selections.find(selection) == string::npos) return false; //string::npos is returned if no matched value is found
return true;
}
bool Algorithm::isFOIL(string* exp) {
string expression = *exp;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int countTot = 0;
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int upper = 0; //upper bound; second left paren
int lower = 0; //lower bound; first right paren
for (int i = 0; i < expression.length(); i++) {
if (expression.at(i) == ('(')) {
count1++;
if (count1 == 2) {
upper = i;
}
}
if (expression.at(i) == (')')) {
++count2;
if (count2 == 1) {
lower = i;
}
}
if (expression.at(i) == ('*') || expression.at(i) == ('/')) {
count3++;
}
if (count1 >= 2 && count2 >= 2 && expression.find('*') < upper && expression.find('*') > lower) {
return true;
}
else if (count1 >= 2 && count2 >= 2 && expression.find('/') < upper && expression.find('/') > lower) {
return true;
}
}
return false;
/*if (expression.find('(') != string::npos) {
int pos1 = expression.find('(');
if (expression.find('(') != pos1) {
upper = expression.find('(');
count++;
}
}
if (expression.find(')') != string::npos) {
int pos2 = expression.find(')');
if (expression.find(')') != pos2) {
lower = expression.find(')');
count++;
}
}
if (expression.find('*') != string::npos && expression.find('*') < upper && expression.find('*') > lower) {
count++;
}
if (count == 3) {
return true;
}
else {
return false;
}*/
}
void Algorithm::formatFOIL(string* exp) {
string expression = *exp;
if (isFOIL(exp)) {
string newEx = "1 * " + expression;
replace(*exp, expression, newEx);
ensureEnclosingParens(exp);
}
}