-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix evaluation new
90 lines (88 loc) · 1.51 KB
/
postfix evaluation new
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
#include<stdio.h>
void sum();
void diff();
void mult();
void div();
void power();
int s[100],top=-1;
void main(){
char st[30];
int i;
printf("enter postfix");
scanf("%[^\n]s",st);
for(i=0;st[i]!='\0';i++){
if(st[i]!=' '){
switch(st[i]){
case '+':
sum();
break;
case '-':
diff();
break;
case '*':
mult();
break;
case '/':
div();
break;
case '^':
power();
break;
default:
top++;
s[top]=st[i]-48;
}
}
}
printf("result is %d",s[top]);
}
void sum(){
int res,op1,op2;
op1=s[top];
top--;
op2=s[top];
top--;
res=op2+op1;
top++;
s[top]=res;
}
void diff(){
int res,op1,op2;
op1=s[top];
top--;
op2=s[top];
top--;
res=op2-op1;
top++;
s[top]=res;
}
void mult(){
int res,op1,op2;
op1=s[top];
top--;
op2=s[top];
top--;
res=op2*op1;
top++;
s[top]=res;
}
void div(){
int res,op1,op2;
op1=s[top];
top--;
op2=s[top];
top--;
res=op2/op1;
top++;
s[top]=res;
}
void power(){
int res,op1,op2,i;
op1=s[top];
top--;
op2=s[top];
top--;
for(i=0;i<op1;i++){
res=res*op2;
}
}