-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgebraicExpression.cpp
More file actions
195 lines (149 loc) · 4.33 KB
/
AlgebraicExpression.cpp
File metadata and controls
195 lines (149 loc) · 4.33 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
#include "AlgebraicExpression.h"
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
// helper methods
int getPrecedence(char ch) {
if (ch == '+' || ch == '-') {
return 1;
}
else if (ch == '*' || ch == '/') {
return 2;
}
else {
return 0;
}
}
string invertString(const string exp) {
// get the length of the string
int n = exp.length();
// make a new string array
string newString = exp;
for (int i = 0, j = n - 1; i < j; i++, j--) {
// Swap the values until you reach the center
char tempChar = newString[i];
newString[i] = newString[j];
newString[j] = tempChar;
}
return newString;
}
bool isOperator(char scanChar) {
// possible operators
return scanChar == '+' ||
scanChar == '-' ||
scanChar == '*' ||
scanChar == '/';
}
string infix2prefix(const string exp) {
// enclose the expression in a bracker
// invert the string
string invExp = invertString("(" + exp + ")");
// get the size of the expression
int expSize = invExp.length();
// make stack
Stack charStack;
// make prefix string
string pString;
// Go through the whole inverted string
for (int i = 0; i < expSize; i++) {
// Scan a single character
char scanChar = invExp[i];
// Check if the scanned character is an operator
if (isOperator(scanChar)) {
// Check the precedence of the operators and remove until the current precedence is higher
string top;
charStack.getTop(top);
while (getPrecedence(scanChar) < getPrecedence(top[0])) {
// Add to the prefix string
pString = top + pString;
// pop the top
charStack.pop();
// Get the next top
charStack.getTop(top);
}
// Push the current operator in the stack
string stringScanChar(1, scanChar);
charStack.push(stringScanChar);
}
// Check if the scanned character is an ( or ) bracket
// The close bracket represents the open bracket since the string is reversed
else if (scanChar == ')') {
charStack.push(")");
}
// The open bracket represents the close bracket since the string is reversed
else if (scanChar == '(') {
// pop out all operands and add to the pstring stack until ')' is not found
string top;
charStack.getTop(top);
while (top != ")") {
// Add to the prefix string
pString = top + pString;
// pop the top
charStack.pop();
// Get the next top
charStack.getTop(top);
}
// pop ")" from the stack afterwards
charStack.pop();
}
// The character is a number
else {
// Add the character to the start of the prefix String
pString = scanChar + pString;
}
}
return pString;
}
double evaluatePrefix(const string exp) {
// Checking if empty string
if (exp == "") return 0;
// invert the expression
string invExp = invertString(exp);
// Get the length of the expression
int expLength = invExp.length();
// Create the stack for the operands
Stack stack;
// Create strings to get the operands from the stack
string operand1 = "";
string operand2 = "";
double result = 0;
for (int i = 0; i < expLength; i++) {
// Get current character
char currChar = invExp[i];
// Current Character is an operator
if (isOperator(currChar)) {
// Pop the two operands from the stack
stack.pop(operand1);
stack.pop(operand2);
// Convert the operands from string to integere
stringstream op1(operand1);
stringstream op2(operand2);
double intOp1 = 0;
double intOp2 = 0;
op1 >> intOp1;
op2 >> intOp2;
// Operate the operands
// Store the result in currResult
double currResult = 0;
if (currChar == '+') currResult += intOp1 + intOp2;
else if (currChar == '-') currResult += intOp1 - intOp2;
else if (currChar == '/') currResult += intOp1 / intOp2;
else if (currChar == '*') currResult += intOp1 * intOp2;
// Convert the double into a string
stringstream resultStingStream;
string stringCurrRresult;
resultStingStream << currResult;
resultStingStream >> stringCurrRresult;
// Push the currentResult back to the stack
stack.push(stringCurrRresult);
result = currResult;
}
// The current character is a number
else {
string currNumber(1, currChar);
stack.push(currNumber);
}
}
return result;
}