-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_postfix.c
54 lines (50 loc) · 1.1 KB
/
eval_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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
int s[30],top=-1,op1,op2,res,i;
char symbol,postfix[20];
void push(int item){
top=top+1;
s[top]=item;
return;
}
int pop()
{
int item;
item=s[top];
top=top-1;
return item;
}
void main()
{
printf("enter valid postfix exp:");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++){
symbol=postfix[i];
if(isdigit(symbol)){
push(symbol - '0');
}
else{
op2=pop();
op1=pop();
switch(symbol){
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
case '^':
case '$':push(pow(op1,op2));
break;
default:printf("invalid operator\n");
}
}
}
res=pop();
printf("result:%d\n",res);
}