-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShuntingYard.cpp
285 lines (255 loc) · 9.63 KB
/
ShuntingYard.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
//
// Created by Tomer Yona on 2018-12-20.
//
#include "ShuntingYard.h"
#include "Number.h"
#include "Plus.h"
#include "Minus.h"
#include "Multiply.h"
#include "Divide.h"
/*
* this function checks if the give character is
*/
bool ShuntingYard::isNumber(char a) {
bool ans= false;
if( a > 47 && a < 58 )
ans = true;
return ans;
}
/*this funtion check if the given char is a math operator*/
bool ShuntingYard::isOperator(char a) {
bool ans = false;
if ( a == '+' || a == '-' || a == '*' || a == '/' )
ans = true;
return ans;
}
/*this funtion will eventually return the final expression after all caculations*/
Expression *ShuntingYard::algorithm(string expr, SymbolsTable *currTable) {
/* the queue is for storing mostly numbers according to shunting yard algorithm*/
queue<string> myQueue;
/*the stack is for storing operators and brackets according to shunting yard algorithm*/
stack<char> myStack;
bool doubleOperator=false;
char oper_toSave;
for(unsigned int i = 0; i < expr.length(); i++){
/*in this condition we check whether the givin char is a digit if it is, we keep on checking if there are
* anymore digits following the original digit and after we have found the complete number we
* push it into the queue, if the number is negative then after we push the current number
* into the queue we push also zero into the queue for example -4= 0-4*/
if ( isNumber(expr[i])|| isChar(expr[i]) ){
string number;
if(isNumber(expr[i])){
number = prepareNumber(expr,i);
i = i + number.length() - 1;
myQueue.push(number);
}
//in this case we take care of var type for example "roll"
else{
//cout << "shunt input: " << expr << endl;
string var = prepareVariable(expr,i);
//cout << "shunt middle: " << var << endl;
i = i + var.length() - 1;
//cout << "shunting yard: to get var value" << endl;
double d_number = currTable->getVarValue(var);
number = to_string(d_number);
myQueue.push(number);
}
doubleOperator=false;
}
/*in this condition we check if the current character is an operator and the we check if top of
* the stack is an operator that has a priority i.e: '*' has a priority over '+' so
* if it has a priority then we push it into the queue otherwise we push it into the stack*/
else if(isOperator(expr[i])){
/*this case i take is whether the following number is negative such as -4
* this also takes care of the priority*/
if(expr[i] == '+' || expr[i] == '-' ){
if((expr[i]=='-'&& doubleOperator) || (expr[i]=='-' && i==0)){
// myStack.push('(');
myQueue.push("0");
if((expr[i]=='-'&& doubleOperator)){
oper_toSave=myStack.top();
myStack.pop();
}
}
while( !myStack.empty() && (myStack.top() == '*' || myStack.top() == '/')){
string opr_toPush;
opr_toPush.push_back(myStack.top());
myQueue.push(opr_toPush);
myStack.pop();
}
if(doubleOperator){
myStack.push(oper_toSave);
}
myStack.push(expr[i]);
}
else if(expr[i] == '*' || expr[i] == '/'){
myStack.push(expr[i]);
}
doubleOperator=true;
}
else if(expr[i] == '('){
myStack.push(expr[i]);
doubleOperator=false;
}
/* this case is if expr[i]==')' */
else if(expr[i]==')'){
while(!myStack.empty() && myStack.top()!= '(' && myStack.top() != ')'){
string opr_toPush;
opr_toPush.push_back(myStack.top());
myQueue.push(opr_toPush);
myStack.pop();
}
myStack.pop();
//isNegative=false;
doubleOperator=false;
}
//I assumed that " " spaces splits the expressions
else if(expr[i]==' '){
expr.substr(1,expr.length());
//isNegative=false;
}
}
/*after all iterations we want to empty the stack and insert the operators into the our queue*/
while(!myStack.empty()){
string opr_toPush;
opr_toPush.push_back(myStack.top());
myStack.pop();
myQueue.push(opr_toPush);
}
return postfix_calc(myQueue);
}
/*this functions recieves a string and int as parameter and as long is a valid number then loop
* keeps on running and in the end returns to caller the valid number as string*/
string ShuntingYard::prepareNumber(string expr, int i) {
string toReturn;
int num_dot= 0;
while(isdigit(expr[i])||(expr[i]=='.' && num_dot<2)){
if(expr[i]=='.'){
num_dot++;
}
if(num_dot==2)
throw ("illeagal expression");
toReturn+= expr[i];
i++;
}
return toReturn;
}
/* i need to finish this function-- this is according to Reverse Polish notation algoritm*/
Expression *ShuntingYard::postfix_calc(queue<string>& myQueue) {
stack <Expression*> exprStack;
while(!myQueue.empty()){
//case if the next bind in our postfix queue is a number
if(isValid_number(myQueue.front())){
Number* number= new Number(myQueue.front());
myQueue.pop();
//caution for not popping out bind
if(!myQueue.empty() && myQueue.front()==","){
//pop the delimeter bind ","
myQueue.pop();
}
exprStack.push(number);
}
//this case is if the next bind is an operator
else{
Expression* right_num = exprStack.top();
exprStack.pop();
//caution for not popping out bind
if(!myQueue.empty() && myQueue.front()==","){
//pop the delimeter bind ","
myQueue.pop();
}
Expression* left_num= exprStack.top();
exprStack.pop();
//caution for not popping out bind
if(!myQueue.empty() && myQueue.front()==","){
//pop the delimeter bind ","
myQueue.pop();
}
char oper_toCalc= myQueue.front()[0];
switch(oper_toCalc){
case '+':{
Plus* plus= new Plus(left_num,right_num);
//i added this on 24.12 to check if 5+5=10 and is inserted into the stack
Number* number= new Number(plus->calculate());
exprStack.push(number);
myQueue.pop();
//caution for not popping out bind
if(!myQueue.empty() && myQueue.front()==","){
//pop the delimeter bind ","
myQueue.pop();
}
break;
}
case '-':{
Minus* minus= new Minus(left_num,right_num);
Number* number= new Number(minus->calculate());
exprStack.push(number);
myQueue.pop();
//caution for not popping out bind
if(!myQueue.empty() && myQueue.front()==","){
//pop the delimeter bind ","
myQueue.pop();
}
break;
}
case '*':{
Multiply* multiply= new Multiply(left_num,right_num);
Number* number= new Number(multiply->calculate());
exprStack.push(number);
myQueue.pop();
//caution for not popping out bind
if(!myQueue.empty() && myQueue.front()==","){
//pop the delimeter bind ","
myQueue.pop();
}
break;
}
case '/':{
Divide* divide= new Divide(left_num,right_num);
Number* number= new Number(divide->calculate());
exprStack.push(number);
myQueue.pop();
//caution for not popping out bind
if(!myQueue.empty() && myQueue.front()==","){
//pop the delimeter bind ","
myQueue.pop();
}
break;
}
default:
break;
}
}
}
return exprStack.top();
}
bool ShuntingYard::isValid_number(string number) {
bool ans=false;
for(unsigned int i = 0; i < number.length(); i++){
if(isNumber(number[i])){
ans=true;
}
}
return ans;
}
double ShuntingYard::evaluate(string &mathematical_exp, SymbolsTable *currTable) {
Expression *calcExp= algorithm(mathematical_exp, currTable);
return calcExp->calculate();
}
/*checks if the char is a letter*/
bool ShuntingYard::isChar(char a) {
bool ans=false;
if(a>96 && a<123)
ans=true;
return ans;
}
/*this function prepares a var from the given string and returns the string of it
* for example "roll/5" will return the string "roll"*/
string ShuntingYard::prepareVariable(string expr, int i) {
string toReturn;
while(isChar(expr[i]) || isNumber(expr[i])){
toReturn+= expr[i];
i++;
}
return toReturn;
}