-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostfixEvaluation.c
77 lines (72 loc) · 1.38 KB
/
PostfixEvaluation.c
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define m 100
int s[m];
int t=-1;
void push(int);
int pop();
int is_operator(char);
int ev(char);
void push(int i){
if(t>=m-1)
printf("Stack overflow.");
t++;
s[t]=i;
}
int pop(){
if(t<0){
printf("Stack underflow.");
return -1;
}
int i=s[t];
t--;
return i;
}
int is_operator(char c){
if(c=='+' || c=='-' || c=='*' || c=='/')
return 1;
return 0;
}
int ev(char *e){
int i=0;
char c=e[i];
int op1,op2,r;
while(c!='\0'){
if(c>='0' && c<='9'){
int n=c-'0';
push(n);
}
else if(is_operator(c)){
op2=pop();
op1=pop();
switch(c){
case '+':
r=op1+op2;
break;
case '-':
r=op1+op2;
break;
case '*':
r=op1*op2;
break;
case '/':
r=op1/op2;
break;
}
push(r);
}
i++;
c=e[i];
}
r=pop();
return r;
}
int main(){
char p;
printf("Enter the expression: ");
scanf("%s",&p);
int r=ev(p);
printf("%d",r);
return 0;
}