-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostfix_evaluation.cpp
More file actions
63 lines (63 loc) · 1.08 KB
/
postfix_evaluation.cpp
File metadata and controls
63 lines (63 loc) · 1.08 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<iostream>
#include<string.h>
#define size 30
#include<math.h>
using namespace std;
struct stack
{
int top;
char items[size];
};
void push(char x,stack *s)
{
s->top+=1;
s->items[s->top]=x;
}
char pop(stack *s)
{
char temp=s->items[s->top];
s->top--;
return temp;
}
int OP(char symbol,int op1,int op2)
{
switch(symbol)
{
case '+':
return (op1+op2);
case '-':
return (op1-op2);
case '*':
return (op1*op2);
case '/':
return (op1/op2);
case '$':
case '^':
return (pow(op1,op2));
}
}
int main()
{
int i,op1,op2,result;
stack s;
s.top=-1;
char symbol,postfix[size];
cout<<"Enter Postfix Expression : ";
cin>>postfix;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
push(symbol-'0',&s);
else
{
op2=pop(&s);
op1=pop(&s);
result=OP(symbol,op1,op2);
push(result,&s);
}
}
result=pop(&s);
cout<<"\nAnswer = "<<result;
return 0;
}