forked from zapellass123/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalpre.cpp
More file actions
63 lines (53 loc) · 1.39 KB
/
evalpre.cpp
File metadata and controls
63 lines (53 loc) · 1.39 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
#include <bits/stdc++.h>
using namespace std;
bool isOperand(char c)
{
// If the character is a digit then it must
// be an operand
return isdigit(c);
}
double evaluatePrefix(string exprsn)
{
stack<double> Stack;
for (int j = exprsn.size() - 1; j >= 0; j--) {
// Push operand to Stack
// To convert exprsn[j] to digit subtract
// '0' from exprsn[j].
if (isOperand(exprsn[j]))
Stack.push(exprsn[j] - '0');
else {
// Operator encountered
// Pop two elements from Stack
double o1 = Stack.top();
Stack.pop();
double o2 = Stack.top();
Stack.pop();
// Use switch case to operate on o1
// and o2 and perform o1 O o2.
switch (exprsn[j]) {
case '+':
Stack.push(o1 + o2);
break;
case '-':
Stack.push(o1 - o2);
break;
case '*':
Stack.push(o1 * o2);
break;
case '/':
Stack.push(o1 / o2);
break;
}
}
}
return Stack.top();
}
// Driver code
int main()
{
string exprsn;
cout<<"Enter expression:";
cin>>exprsn;
cout << evaluatePrefix(exprsn) << endl;
return 0;
}