-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix evalution.cpp
71 lines (64 loc) · 1.29 KB
/
postfix evalution.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
#include <iostream>
using namespace std;
#define SIZE 10
char postfix[SIZE];
int top=-1;
int stack[SIZE];
void push (int n)
{
if (top<SIZE-1)
stack[++top]=n;
else
cout<<"Stack is full."<<endl;
}
int pop()
{
int n;
if (top>-1)
{
n=stack[top];
stack[top--]=-1;
return n;
}
else
cout<<"Stack is Empty."<<endl;
}
int main ()
{
int i,a,b,result,Eval;
char ch;
for (i=0;i<SIZE;i++) //initialize by -1
stack[i]=-1;
cout<<"Enter postfix expression: ";
cin>>postfix;
for (i=0;postfix[i]!='\0';i++)
{
ch=postfix[i];
if (isdigit(ch))
push(ch-'0');
else if (ch=='+' || ch=='-' || ch=='*' || ch=='/')
{
b=pop();
a=pop();
switch(ch)
{
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
}
push(result);
}
}
Eval=pop();
cout<<"Result= "<<Eval<<endl;
return 0;
}