-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastEval.cpp
More file actions
117 lines (106 loc) · 3.14 KB
/
LastEval.cpp
File metadata and controls
117 lines (106 loc) · 3.14 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
/*
Note: all the adge cases are not tested
*/
#include <iostream>
#include <stack>
using namespace std;
//Helper Function 1:
//pop 2 values from vStack and one from op.
//push the result to val stack
void applyOp(stack<double> &vStack, stack<char> &opStack){
double rightOperand = vStack.top();
vStack.pop();
double leftOperand = vStack.top();
vStack.pop();
char op = opStack.top();
opStack.pop();
double result = 0;
switch (op){
case '+':
result = leftOperand + rightOperand;
break;
case '-':
result = leftOperand - rightOperand;
break;
case '*':
result = leftOperand * rightOperand;
break;
case '/':
if(rightOperand==0)
throw invalid_argument("Cannot divide by zero");
result = leftOperand / rightOperand;
break;
}
vStack.push(result);
}
//helper function 2:
//see if the op in hand and the op. on of opStack has precedence
bool hasPrecedence(char op, char prevOp){
bool evaluate = false;
switch (op)
{
case '+':
case '-':
evaluate = (prevOp != '(');
break;
case '*':
case '/':
evaluate = (prevOp == '*' || prevOp == '/');
break;
case ')':
evaluate = true;
break;
}
return evaluate;
}
//Helper 3: apply operation
void ProcessInputOperator(char op, stack<double> &vStack, stack<char> &opStack){
while (!opStack.empty() && hasPrecedence(op, opStack.top()))
applyOp(vStack, opStack);
opStack.push(op);
}
//Helper 4: get the all digits of the number
int ProcessInputNumber(string exp, int pos, stack<double> &vStack){
string value = "";
while (pos < exp.size() && exp[pos] >= '0' && exp[pos] <= '9' || exp[pos]=='.' ){
//value = 10 * value + (int)(exp[pos++] - '0');//works for int
value = value + exp[pos++] ;
}
vStack.push(stod(value));
return pos;
}
//Helper 5: if ) eval. until (
void ProcessClosingParenthesis(stack<double> &vStack, stack<char> &opStack){
while (opStack.top() != '(')
applyOp(vStack, opStack);
opStack.pop(); // Remove the opening parenthesis
}
//Bringing it all together
double EvaluateExpression(string exp){
stack<double> vStack ; //store values
stack<char> opStack ; //store Opertations
opStack.push('('); // Implicit opening parenthesis
int pos = 0;
while (pos <= exp.size()){
if (pos == exp.size() || exp[pos] == ')'){ //if )
ProcessClosingParenthesis(vStack, opStack); //evalueate what is inside ()
pos++;
}
else if (exp[pos] >= '0' && exp[pos] <= '9'){ // if number
pos = ProcessInputNumber(exp, pos, vStack); //Get the all the digits of that number
}
else {
ProcessInputOperator(exp[pos], vStack, opStack); //apply operation
pos++;
}
}
return vStack.top(); // Result
}
int main(){
string s;
cout<<"Write the Expression whiteout space: ";
cin>>s;
double l = EvaluateExpression(s);
cout<<"The string evaluates to: "<<l;
return 0;
}