-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionCalculate.cpp
More file actions
521 lines (495 loc) · 16.2 KB
/
ExpressionCalculate.cpp
File metadata and controls
521 lines (495 loc) · 16.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
//
// Created by chen96 on 28/12/2019.
//
#include "ExpressionCalculate.h"
#include "Expression.h"
#include "globalVariables.h"
#include <queue>
#include <stack>
#include <regex>
#include <map>
#include <stdexcept>
#include <exception>
// Value class
Value::Value(double val1) : val(val1) {}
double Value::calculate() {
return val;
}
Value::~Value() = default;
// BinaryOperator class
BinaryOperator::BinaryOperator(Expression *left1, Expression *right1) {
left = left1;
right = right1;
}
BinaryOperator::~BinaryOperator() {
delete left;
delete right;
}
// UnaryOperator class
UnaryOperator::UnaryOperator(Expression *exp1) {
exp = exp1;
}
UnaryOperator::~UnaryOperator() {
delete exp;
}
// Variable class
Variable::Variable(string name1, double value1) {
name = name1;
value = value1;
}
Variable &Variable::operator++(int) {
value++;
return *this;
}
Variable &Variable::operator--(int) {
value--;
return *this;
}
Variable &Variable::operator++() {
value++;
return *this;
}
Variable &Variable::operator--() {
value--;
return *this;
}
Variable &Variable::operator+=(double num) {
value += num;
return *this;
}
Variable &Variable::operator-=(double num) {
value -= num;
return *this;
}
double Variable::calculate() {
return value;
}
Variable::~Variable() = default;
// Plus class
Plus::Plus(Expression *left1, Expression *right1) : BinaryOperator(left1, right1) {}
double Plus::calculate() {
return left->calculate() + right->calculate();
}
Plus::~Plus() = default;
// Minus class
Minus::Minus(Expression *left1, Expression *right1) : BinaryOperator(left1, right1) {}
double Minus::calculate() {
return left->calculate() - right->calculate();
}
Minus::~Minus() = default;
// Mul class
Mul::Mul(Expression *left1, Expression *right1) : BinaryOperator(left1, right1) {}
double Mul::calculate() {
return left->calculate() * right->calculate();
}
Mul::~Mul() = default;
// Div class
Div::Div(Expression *left1, Expression *right1) : BinaryOperator(left1, right1) {}
double Div::calculate() {
if (right->calculate() == 0) {
throw "Division by zero";
}
return left->calculate() / right->calculate();
}
Div::~Div() = default;
// UPlus class (Unary plus)
UPlus::UPlus(Expression *exp1) : UnaryOperator(exp1) {}
double UPlus::calculate() {
return exp->calculate();
}
UPlus::~UPlus() = default;
// UMinus class (Unary minus)
UMinus::UMinus(Expression *exp1) : UnaryOperator(exp1) {
exp = new Mul(new Value(-1.0), exp1);
}
double UMinus::calculate() {
return exp->calculate();
}
UMinus::~UMinus() = default;
// Interpreter class
Interpreter::Interpreter() {
precedence["+"] = 1;
precedence["-"] = 1;
precedence["*"] = 2;
precedence["/"] = 2;
precedence["("] = 3;
precedence[")"] = 3;
precedence["!-!"] = 4;
precedence["!+!"] = 4;
}
// gets the precedence of an operator from the precedence map
int Interpreter::getOpPrecedence(string op) {
return precedence[op];
}
// The interpret function creates an expression from a string, using Shunting-yard algorithm
Expression *Interpreter::interpret(string str) {
queue<string> output;
stack<string> operators;
// iterates over the given string
int iterator = 0;
smatch numsAndVarsMatch;
int countNumsAndVars = 0;
int pos = 0;
// checks if the string is an invalid expression
if ((!checkValidNumOp(str)) || (!checkParentheses(str)) || (!checkValidOrder(str))) {
throw "Invalid expression";
}
smatch unaryMinus;
regex findUnaryM("(^(\\-)|(\\(-))");
// the number of unary minus
int numMinus = 0;
// counts the number of unary minus in the string
numMinus = distance(
sregex_iterator(str.begin(), str.end(), findUnaryM),
sregex_iterator());
int *positionsM = new int[numMinus];
if (positionsM == nullptr) {
throw "Error: memory could not be allocated";
}
int numPosition = 0;
int itrStr = 0;
// the loop adds to the position array - the positions of the unary minus in the string
for (sregex_iterator it = sregex_iterator(str.begin(), str.end(), findUnaryM); it != sregex_iterator(); it++) {
unaryMinus = *it;
positionsM[numPosition] = unaryMinus.position();
numPosition++;
}
// replacing the unary minus to "!-!"
for (int i = 0; i < numMinus; i++) {
if ((positionsM[i] == 0) && (str[0] == '-')) {
str.replace(positionsM[i], 1, "!-!");
itrStr++;
} else {
str.replace(positionsM[i] + 2 * itrStr + 1, 1, "!-!");
itrStr++;
}
}
delete[] positionsM;
smatch unaryPlus;
regex findUnaryP(R"(((^\+)|(\(\+)))");
// the number of unary plus
int numPlus = 0;
// counts the number of unary plus in the string
numPlus = distance(
sregex_iterator(str.begin(), str.end(), findUnaryP),
sregex_iterator());
int *positionsP = new int[numPlus];
if (positionsP == nullptr) {
throw "Error: memory could not be allocated";
}
numPosition = 0;
itrStr = 0;
// the loop adds to the position array - the positions of the unary plus in the string
for (sregex_iterator it = sregex_iterator(str.begin(), str.end(), findUnaryP); it != sregex_iterator(); it++) {
unaryPlus = *it;
positionsP[numPosition] = unaryPlus.position();
numPosition++;
}
// replacing the unary plus to "!+!"
for (int i = 0; i < numPlus; i++) {
if (positionsP[i] == 0) {
str.replace(positionsP[i], 1, "!+!");
itrStr++;
} else {
str.replace(positionsP[i] + 2 * itrStr + 1, 1, "!+!");
itrStr++;
}
}
delete[] positionsP;
// checks if the string is an invalid expression
if (!checkVarAndP(str)) {
throw "Invalid Expression";
}
regex findNumsAndVars(R"([^(\-|\(|\)|\+|\*|\/|\!)]+)");
sregex_iterator it = sregex_iterator(str.begin(), str.end(), findNumsAndVars);
// counts the number of numbers and variables in the string
countNumsAndVars = distance(
sregex_iterator(str.begin(), str.end(), findNumsAndVars),
sregex_iterator());
// An array that contains variables and numbers even if they are invalid
string variablesAndNums[countNumsAndVars];
for (sregex_iterator it = sregex_iterator(str.begin(), str.end(), findNumsAndVars); it != sregex_iterator(); it++) {
numsAndVarsMatch = *it;
variablesAndNums[pos] = numsAndVarsMatch.str();
pos++;
}
pos = 0;
// iterating the string
while (iterator < str.length()) {
// the current char
char sign = str[iterator];
string strSign(1, sign);
// checks if the string is an operator
if (checkIfOp(strSign)) {
while ((!operators.empty())&& ((precedence[operators.top()] >= precedence[strSign]) && (operators.top().compare("(") != 0))) {
output.push(operators.top());
operators.pop();
}
operators.push(strSign);
iterator++;
} else if (strSign.compare("(") == 0) {
operators.push(strSign);
iterator++;
} else if (strSign.compare(")") == 0) {
if (!operators.empty()) {
// removes all the operators from stack until it gets to "("
while ((!operators.empty()) && (operators.top().compare("(") != 0)) {
output.push(operators.top());
operators.pop();
}
} else {
throw "Mismatched parentheses";
}
if (!operators.empty()) {
if (operators.top().compare("(") == 0) {
operators.pop();
}
} else {
throw "Mismatched parentheses";
}
iterator++;
// checks if it an unary minus/plus (starts with "!")
} else if (strSign.compare("!") == 0) {
string subUnary = str.substr(iterator, 3);
if ((subUnary.compare("!-!") == 0) || (subUnary.compare("!+!") == 0)) {
operators.push(subUnary);
iterator += 3;
}
} else {
// checks if there are variables or numbers in the string
if (countNumsAndVars == 0) {
throw "The Expression does not contain numbers or variables";
} else {
// checks if the string is a number
if (checkIfNum(variablesAndNums[pos])) {
output.push(variablesAndNums[pos]);
iterator += variablesAndNums[pos].length();
pos++;
// checks if the string is a variable
} else if (checkIfVariable(variablesAndNums[pos])) {
output.push(variablesAndNums[pos]);
iterator += variablesAndNums[pos].length();
pos++;
} else {
throw "The Expression contains invalid numbers or variables";
}
}
}
}
while (!operators.empty()) {
// checks if there are parenthesis left in the stack
if ((operators.top().compare("(") == 0) || ((operators.top().compare(")") == 0))) {
throw "Mismatched parentheses";
}
output.push(operators.top());
operators.pop();
}
return postfixToExp(output);
}
// The function adds variables and their values to the interpreter's map
void Interpreter::setVariables(string s) {
smatch sVars;
regex split("[^;]+");
int count = 0;
// the number of assignments in the string
count = distance(
sregex_iterator(s.begin(), s.end(), split),
sregex_iterator());
string *vars = new string[count];
if (vars == nullptr) {
throw "Error: memory could not be allocated";
}
int counter = 0;
// copy the assignments to vars array
for (sregex_iterator it = sregex_iterator(s.begin(), s.end(), split); it != sregex_iterator(); it++) {
sVars = *it;
vars[counter] = sVars.str();
counter++;
}
regex eqSplit("[^=]+");
smatch splitMatch;
string eq[2];
// goes over the vars array
for (int i = 0; i < count; i++) {
int pos = 0;
// checks if the equation is valid
if (checkValidEquation(vars[i])) {
// copy the var and value to the eq array
for (sregex_iterator it = sregex_iterator(vars[i].begin(), vars[i].end(), eqSplit); it != sregex_iterator();
it++) {
splitMatch = *it;
eq[pos] = splitMatch.str();
pos++;
}
map<string, double>::iterator it;
// checks if the var is already exist in the map and deletes its old value
it = varsAndValues.find(eq[0]);
if (it != varsAndValues.end()) {
varsAndValues.erase(it);
}
// insert the new variable and its value to the map
varsAndValues.insert(pair<string, double>(eq[0], stod(eq[1])));
} else {
throw "Illegal variable assignment";
}
}
delete[] vars;
}
// the function creates an expression from postfix queue
Expression *Interpreter::postfixToExp(queue<string> qs) {
Expression *e1;
stack<Expression *> sEvaluate;
while (!qs.empty()) {
// checks if the front of the queue is a number
if (checkIfNum(qs.front())) {
// converts the string to double
double number = stod(qs.front());
Expression *numE = new Value(number);
sEvaluate.push(numE);
qs.pop();
// checks if the string is an unary minus
} else if (qs.front().compare("!-!") == 0) {
Expression *unaryM = new UMinus(sEvaluate.top());
sEvaluate.pop();
sEvaluate.push(unaryM);
qs.pop();
// checks if the string is an unary plus
} else if (qs.front().compare("!+!") == 0) {
Expression *unaryP = new UPlus(sEvaluate.top());
sEvaluate.pop();
sEvaluate.push(unaryP);
qs.pop();
// checks if the string is an operator
} else if (checkIfOp(qs.front())) {
string op = qs.front();
qs.pop();
if (op.compare("+") == 0) {
Expression *right = sEvaluate.top();
sEvaluate.pop();
Expression *left = sEvaluate.top();
sEvaluate.pop();
Expression *plusE = new Plus(left, right);
sEvaluate.push(plusE);
} else if (op.compare("-") == 0) {
Expression *right = sEvaluate.top();
sEvaluate.pop();
Expression *left = sEvaluate.top();
sEvaluate.pop();
Expression *minusE = new Minus(left, right);
sEvaluate.push(minusE);
} else if (op.compare("/") == 0) {
Expression *right = sEvaluate.top();
sEvaluate.pop();
Expression *left = sEvaluate.top();
sEvaluate.pop();
Expression *divE = new Div(left, right);
sEvaluate.push(divE);
} else {
Expression *right = sEvaluate.top();
sEvaluate.pop();
Expression *left = sEvaluate.top();
sEvaluate.pop();
Expression *mulE = new Mul(left, right);
sEvaluate.push(mulE);
}
} else {
// checks if the variable is in the map
if (checkIfInMapClientToSim(qs.front())) {
Expression *var = new Variable(qs.front(), globalVariables::symbolTableClientToSim[qs.front()].first);
sEvaluate.push(var);
qs.pop();
}else if(checkIfInMapSimToClient(qs.front())){
Expression *var = new Variable(qs.front(), globalVariables::symbolTableSimToClient[qs.front()].first);
sEvaluate.push(var);
qs.pop();
}else {
throw "Undefined variable";
}
}
}
return sEvaluate.top();
}
// The function returns true if the string is an operator
bool Interpreter::checkIfOp(string s) {
regex findOp(R"(^(\-|\+|\*|\/)$)");
int count = distance(
sregex_iterator(s.begin(), s.end(), findOp),
sregex_iterator());
return count == 1;
}
// The function returns true if the string is a number
bool Interpreter::checkIfNum(string s) {
regex findNum("^(([0-9]+)|([0-9]+.[0-9]+))$");
int count = distance(
sregex_iterator(s.begin(), s.end(), findNum),
sregex_iterator());
return count == 1;
}
// The function returns true if the string is a valid equation (var=val)
bool Interpreter::checkValidEquation(string s) {
regex findVar("^([A-Z]|[a-z]|\\_)([0-9]|\\_|[A-Z]|[a-z])*=(([0-9]+)|([0-9]+.[0-9]+))$");
int count = distance(
sregex_iterator(s.begin(), s.end(), findVar),
sregex_iterator());
return count == 1;
}
// The function returns true if the string is a variable
bool Interpreter::checkIfVariable(string s) {
regex findVar("^([A-Z]|[a-z]|\\_)([0-9]|\\_|[A-Z]|[a-z])*$");
int count = distance(
sregex_iterator(s.begin(), s.end(), findVar),
sregex_iterator());
return count == 1;
}
// The function returns true if the string does not contains more than one operator in a row
bool Interpreter::checkValidNumOp(string s) {
regex findMoreThanOneOp("(\\/|\\*|\\+|\\-|!-!|!+!){2,}");
int count = distance(
sregex_iterator(s.begin(), s.end(), findMoreThanOneOp),
sregex_iterator());
return count == 0;
}
// The function returns true if the string contains variables in parenthesis when there is Uminus/Uplus
bool Interpreter::checkVarAndP(string s) {
regex findVarAndP(R"(((!+!|!-!)(\)|((\_)([0-9]|\_|[A-Z]|[a-z])*))))");
int count = distance(
sregex_iterator(s.begin(), s.end(), findVarAndP),
sregex_iterator());
return count == 0;
}
// The function returns true if the string contains valid attachment of parenthesis
bool Interpreter::checkParentheses(string s) {
regex findInvalidP1("(\\(\\))");
int count1 = distance(
sregex_iterator(s.begin(), s.end(), findInvalidP1),
sregex_iterator());
regex findInvalidP2("(\\)\\()");
int count2 = distance(
sregex_iterator(s.begin(), s.end(), findInvalidP2),
sregex_iterator());
return (count1 + count2) == 0;
}
// The function returns true if the string has a valid order of parenthesis and vars/nums/operators
bool Interpreter::checkValidOrder(string s) {
regex findInvalidOrder1(R"((([A-Z]|[a-z]|\_)([0-9]|\_|[A-Z]|[a-z])*|(([0-9]+)|([0-9]+.[0-9]+)))\()");
int count1 = distance(
sregex_iterator(s.begin(), s.end(), findInvalidOrder1),
sregex_iterator());
regex findInvalidOrder2(R"(\)(([A-Z]|[a-z]|\_)([0-9]|\_|[A-Z]|[a-z])*|(([0-9]+)|([0-9]+.[0-9]+))))");
int count2 = distance(
sregex_iterator(s.begin(), s.end(), findInvalidOrder2),
sregex_iterator());
regex findInvalidOrder3(R"(((\+|\-|\*|\/)(\))|(\+|\-|\*|\/)$))");
int count3 = distance(
sregex_iterator(s.begin(), s.end(), findInvalidOrder3),
sregex_iterator());
return (count1 + count2 + count3) == 0;
}
// // The function returns true if the map ClientToSim contains the string
bool Interpreter::checkIfInMapClientToSim(string s) {
return !(globalVariables::symbolTableClientToSim.find(s) == globalVariables::symbolTableClientToSim.end());
}
// // The function returns true if the map contains the string
bool Interpreter::checkIfInMapSimToClient(string s) {
return !(globalVariables::symbolTableSimToClient.find(s) == globalVariables::symbolTableSimToClient.end());
}