-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposteval.c
59 lines (56 loc) · 935 Bytes
/
posteval.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
/*write a program to evaluate the postfix expression using stack*/
#include<stdio.h>
#include<math.h>
int stack[30],top=-1;
int x,i,j=0;
char postfix[30],ch;
int opd1,opd2,x;
void main()
{
printf("\nEnter postfix:");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
if(isalpha(postfix[i]))
{
printf("\nEnter value for %c:",postfix[i]);
scanf("%d",&x);
push(x);
}
else
{
switch(postfix[i])
{
case '+':opd1=pop();
opd2=pop();
x=opd2+opd1;
push(x);break;
case '-':opd1=pop();
opd2=pop();
x=opd2-opd1;
push(x);break;
case '*':opd1=pop();
opd2=pop();
x=opd2*opd1;
push(x);break;
case '/':opd1=pop();
opd2=pop();
x=opd2/opd1;
push(x);break;
case '^':opd1=pop();
opd2=pop();
x=pow(opd2,opd1);
push(x);break;
}
}
}
printf("Result is %d\n",stack[top]);
}
push(int n)
{
stack[++top]=n;
}
pop()
{
return(stack[top--]);
}