-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8 - Postfix_Evaluation.c
113 lines (99 loc) · 1.52 KB
/
8 - Postfix_Evaluation.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Implement Postfix Evaluation using Stack.
#include<stdio.h>
#define max 20
void push(int[],int);
int pop (int[]);
int top=-1;
void display(int[],int);
void main()
{
int s[max],x1,x2,result,i=0,ans,len;
char p[50];
printf("Enter Postfix Expression : ");
gets(p);
len=strlen(p);
p[len]=')';
puts(p);
while(p[i]!=')')
{
switch(p[i])
{
case '+':
x1=pop(s);
x2=pop(s);
result=x2+x1;
push(s,result);
break;
case '-':
x1=pop(s);
x2=pop(s);
result=x2-x1;
push(s,result);
break;
case '*':
x1=pop(s);
x2=pop(s);
result=x2*x1;
push(s,result);
break;
case '/':
x1=pop(s);
x2=pop(s);
result=x2/x1;
push(s,result);
break;
case '$':
x1=pop(s);
x2=pop(s);
result=pow(x2,x1);
push(s,result);
break;
default:
push(s,p[i]-48);
break;
}
display(s,top);
i++;
}
ans=pop(s);
printf("Postfix Evaulation is : %d",ans);
}
void push(int s[],int y)
{
if(top>=max)
{
printf("\nStack is Overflow...!!");
printf("\n\n");
}
else
{
top++;
s[top]=y;
}
}
int pop(int s[])
{
int x;
if(top<0)
{
printf("\nStack is Underflow...!!");
printf("\n\n");
}
else
{
x=s[top];
top--;
return x;
}
}
void display(int s[],int top)
{
int i;
printf("\nStack has Following Elements : \n");
for(i=0;i<=top;i++)
{
printf("\t%d",s[i]);
printf("\n");
}
printf("\n");
}