-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevaluate_postfix.c
57 lines (48 loc) · 982 Bytes
/
evaluate_postfix.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
#include <stdio.h>
int top = -1, stack [100];
void push (int n){
top++;
stack [top] = n;
}
int pop (){
int res ;
res = stack [top];
top--;
return res;
}
int eval (char ch, int op1, int op2){
switch (ch){
case '+' : return (op1+op2);
case '-' : return (op1-op2);
case '*' : return (op1*op2);
case '/' : return (op1/op2);
default: return 0;
}
}
int is_num(char ch){
if(ch>='0' && ch<='9') return 1;
else return 0;
}
int main (){
char a[50];
int i,op1,op2,res,x;
printf("enter a postfix expression:");
scanf("%s",a);
//char a[] = {'3',' ', '5', ' ', '2', '*', ' ', '+', '\0'};
for(i=0; a[i]!='\0'; i++)
{
char ch = a[i];
if(is_num(ch))
push(ch - '0');
else if (ch != 32)
{
op2 = pop ( );
op1 = pop ( );
res = eval (ch, op1, op2);
push (res);
}
}
x = pop ( );
printf("evaluated value = %d", x);
return 0;
}