-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionEvaluator.cpp
310 lines (300 loc) · 9 KB
/
ExpressionEvaluator.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
//
// Created by FLM on 2015/12/5 0005.
//
#include "ExpressionEvaluator.h"
#include <sstream>
#include <stdexcept>
using namespace std;
OperatorPrecedence::Order Symbol::precedence() {
switch (this->type) {
case Add:
case Sub:
return OperatorPrecedence::AddSub;
case Multi:
case Divide:
return OperatorPrecedence::MultiDivide;
case Power:
return OperatorPrecedence::Power;
case Positive:
case Negative:
return OperatorPrecedence::PositiveNegative;
case ApplyFunction:
return OperatorPrecedence::Composition;
default:
assert(false);
}
}
void ExpressionEvaluator::stringDecomposition() {
stringstream ss;
ss << this->expression;
char c;
while ((c = ss.peek()) != EOF) {
if (c == ' ') {
ss.get();
continue;
} else if ((c >= '0' && c <= '9') || c == '.') {
double x;
ss >> x;
if (ss.fail()) throw invalid_argument("invalid expression");
symbolQueue.push(Symbol(x));
} else if (c == '+') {
ss.get();
if (symbolQueue.size() == 0) {
symbolQueue.push(Symbol(Symbol::Positive));
} else {
switch (symbolQueue.back().type) {
case Symbol::FunName:
case Symbol::Number:
case Symbol::theVariableX:
case Symbol::RightParenthese:
symbolQueue.push(Symbol(Symbol::Add));
break;
default:
symbolQueue.push(Symbol(Symbol::Positive));
break;
}
}
} else if (c == '-') {
ss.get();
if (symbolQueue.size() == 0) {
symbolQueue.push(Symbol(Symbol::Negative));
} else {
switch (symbolQueue.back().type) {
case Symbol::FunName:
case Symbol::Number:
case Symbol::theVariableX:
case Symbol::RightParenthese:
symbolQueue.push(Symbol(Symbol::Sub));
break;
default:
symbolQueue.push(Symbol(Symbol::Negative));
break;
}
}
} else if (c == '*') {
ss.get();
symbolQueue.push(Symbol(Symbol::Multi));
} else if (c == '/') {
ss.get();
symbolQueue.push(Symbol(Symbol::Divide));
} else if (c == '^') {
ss.get();
symbolQueue.push(Symbol(Symbol::Power));
} else if (c == '(') {
ss.get();
symbolQueue.push(Symbol(Symbol::LeftParenthese));
} else if (c == ')') {
ss.get();
symbolQueue.push(Symbol(Symbol::RightParenthese));
} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
// identifiers
string name;
char next;
do {// get full name string
name.push_back(ss.get());
next = ss.peek();
} while ((next >= 'a' && next <= 'z')
|| (next >= 'A' && next <= 'Z')
|| (next >= '0' && next <= '9')
|| next == '_');
if (name == "sin")
symbolQueue.push(Symbol(Sin));
else if (name == "cos")
symbolQueue.push(Symbol(Cos));
else if (name == "tan")
symbolQueue.push(Symbol(Tan));
else if (name == "exp")
symbolQueue.push(Symbol(Exp));
else if (name == "log")
symbolQueue.push(Symbol(Log));
else if (name == "x")
symbolQueue.push(Symbol(Symbol::theVariableX));
else
throw invalid_argument("invalid function name");
} else {
throw invalid_argument("invalid symbol");
}
}
}
void ExpressionEvaluator::reversePolishNotation() {
// Shunting-yard algorithm
stack<Symbol> operators;
while (!symbolQueue.empty()) {
Symbol sym = symbolQueue.front();
symbolQueue.pop();
switch (sym.type) {
case Symbol::FunName:
if (symbolQueue.front().type != Symbol::LeftParenthese)
throw invalid_argument("funtion name must be followed by a '('");
symbolPolish.push(sym);
operators.push(Symbol(Symbol::ApplyFunction));
break;
case Symbol::Number:
case Symbol::theVariableX:
symbolPolish.push(sym);
break;
case Symbol::Add:
case Symbol::Sub:
case Symbol::Multi:
case Symbol::Divide:
case Symbol::Positive:
case Symbol::Negative:
case Symbol::Power:
case Symbol::ApplyFunction:
while (!operators.empty() && operators.top().type != Symbol::LeftParenthese) {
Symbol top = operators.top();
if (top.precedence() < sym.precedence())break;
symbolPolish.push(operators.top());
operators.pop();
}
operators.push(sym);
break;
case Symbol::RightParenthese:
while (!operators.empty()
&& operators.top().type != Symbol::LeftParenthese) {
symbolPolish.push(operators.top());
operators.pop();
}
if (operators.empty())
throw invalid_argument("a '(' is missed in the expression");
assert(operators.top().type == Symbol::LeftParenthese);
operators.pop();
break;
case Symbol::LeftParenthese:
operators.push(sym);
break;
default:
assert(false);
}
} // while
while (!operators.empty()) {
if (operators.top().type == Symbol::LeftParenthese)
throw invalid_argument("a ')' is missed in the expression");
assert(operators.top().type != Symbol::RightParenthese);
symbolPolish.push(operators.top());
operators.pop();
}
}
Expression *ExpressionEvaluator::constructTree() {
stack<Expression *> exprStack;
while (!symbolPolish.empty()) {
Symbol sym = symbolPolish.front();
symbolPolish.pop();
switch (sym.type) {
case Symbol::RightParenthese:
case Symbol::LeftParenthese:
//no parenthesis is possible in polish notation
assert(false);
break;
case Symbol::Number:
exprStack.push(new Constant(sym.number));
break;
case Symbol::Add: {
assert(exprStack.size() >= 2);
Expression *b = exprStack.top();
exprStack.pop();
Expression *a = exprStack.top();
exprStack.pop();
Expression *ab = new Addition(a, b);
exprStack.push(ab);
break;
}
case Symbol::Sub: {
assert(exprStack.size() >= 2);
Expression *b = exprStack.top();
exprStack.pop();
Expression *a = exprStack.top();
exprStack.pop();
Expression *ab = new Addition(a, new Multiplication(new Constant(-1), b));
exprStack.push(ab);
break;
}
case Symbol::Multi: {
assert(exprStack.size() >= 2);
Expression *b = exprStack.top();
exprStack.pop();
Expression *a = exprStack.top();
exprStack.pop();
Expression *ab = new Multiplication(a, b);
exprStack.push(ab);
break;
}
case Symbol::Divide: {
assert(exprStack.size() >= 2);
Expression *b = exprStack.top();
exprStack.pop();
Expression *a = exprStack.top();
exprStack.pop();
Expression *ab = new Division(a, b);
exprStack.push(ab);
break;
}
case Symbol::FunName: {
switch (sym.funName) {
case Sin:
exprStack.push(new Trigo(Trigo::Sin));
break;
case Cos:
exprStack.push(new Trigo(Trigo::Cos));
break;
case Tan:
exprStack.push(new Trigo(Trigo::Tan));
break;
case Log:
exprStack.push(new Logarithm);
break;
case Exp:
exprStack.push(new Exponential);
break;
}
break;
}
case Symbol::theVariableX:
exprStack.push(new VariableX);
break;
case Symbol::ApplyFunction: {
assert(exprStack.size() >= 2);
Expression *x = exprStack.top();
exprStack.pop();
Expression *f = exprStack.top();
exprStack.pop();
Expression *fx = new Composition(f, x);
exprStack.push(fx);
break;
}
case Symbol::Positive:
break;
case Symbol::Negative: {
assert(exprStack.size() >= 1);
Expression *a = exprStack.top();
exprStack.pop();
Expression *minus_a = new Multiplication(new Constant(-1), a);
exprStack.push(minus_a);
break;
}
}
}
assert(exprStack.size() == 1);
Expression *e = exprStack.top();
bool changed;
Expression *simplify = e->simplify(changed);
if (simplify){
delete e;
return simplify;
}
else
return e;
}
Expression *ExpressionEvaluator::evaluate(const std::string &s) {
pos = 0;
//clear old data
while (!symbolPolish.empty())
symbolPolish.pop();
while (!symbolQueue.empty())
symbolQueue.pop();
if (s.size() == 0) throw invalid_argument("empty string");
expression = s;
stringDecomposition();
reversePolishNotation();
return constructTree();
}