-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfixevaluation.c
62 lines (57 loc) · 1.37 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
#include<stdio.h>
#include<ctype.h>
#define SIZE 30
char postfix[SIZE],ch;
int s[SIZE],num,top=-1,res,op1,op2,i=0;
void push(int num)
{
s[++top]=num;
}
int pop()
{
return s[top--];
}
void main()
{
printf("enter postfix expression\n");
gets(postfix);
while(postfix[i]!='\0');
{
ch=postfix[i];
if(isalpha(ch))
{
printf("enter value for %c",ch);
scanf("%d",&num);
push(num);
}
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '*': res=op1*op2;
push(res);
break;
case '/': res=op1/op2;
push(res);
break;
case '+': res=op1+op2;
push(res);
break;
case '-': res=op1-op2;
push(res);
break;
case '^': res=op1^op2;
push(res);
break;
case '%': res=op1%op2;
push(res);
break;
}
}
i++;
}
res=pop();
printf("\nthe postfix evaluation is %d",res);
}