-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix_evaluation2.cpp
75 lines (56 loc) · 1.03 KB
/
postfix_evaluation2.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
#include <iostream>
using namespace std;
int stack[15];
int top=-1;
void push(int value)
{
top++;
stack[top]=value;
}
char pop()
{
float result=stack[top];
top--;
return result;
}
int main()
{
string postfix;
float op1, op2, result;
cout<<"\n Enter a valid postfix expression : ";
cin>>postfix;
for(int i=0; i<postfix.length(); i++)
{
if(postfix[i] >= '0' && postfix[i] <='9')
{
float operand = (int)(postfix[i]-48);
push(operand);
}else if(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/')
{
op2=pop();
op1=pop();
switch(postfix[i])
{
case '+':
result=op1+op2;
push(result);
break;
case '-':
result=op1-op2;
push(result);
break;
case '*':
result=op1*op2;
push(result);
break;
case '/':
result=op1/op2;
push(result);
break;
}
}
}
push(result);
cout<<"Result: "<<pop();
return 0;
}