Skip to content

Commit 4af6139

Browse files
Create Evaluation_of_Postfix_Expression.c
1 parent 3c1bff0 commit 4af6139

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Evaluation_of_Postfix_Expression.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Give an input Postfix Expression, write the program to evaluate the given postfix expression using Stack Operations.
2+
3+
// Input : Postfix Expression and List of values of the variables
4+
5+
// Output : Result of the expression
6+
7+
#include<stdio.h>
8+
#include<stdlib.h>
9+
#include<string.h>
10+
int s[20],top=-1;
11+
void push(int e)
12+
{
13+
top++;
14+
s[top]=e;
15+
}
16+
int pop()
17+
{
18+
int temp=top;
19+
top--;
20+
return s[temp];
21+
}
22+
int main()
23+
{
24+
char str[20];
25+
int a,n,b,c;
26+
scanf("%s",str);
27+
for(int i=0;i<strlen(str);i++)
28+
{
29+
if(str[i]>='a' && str[i]<='z')
30+
{
31+
scanf("%d",&n);
32+
push(n);
33+
}
34+
else
35+
{
36+
switch(str[i])
37+
{
38+
case '+': a=pop()+pop();
39+
push(a);
40+
break;
41+
case '-': b=pop();
42+
c=pop();
43+
a=c-b;
44+
push(a);
45+
break;
46+
case '*': a=pop()*pop();
47+
push(a);
48+
break;
49+
case '/': b=pop();
50+
c=pop();
51+
a=c/b;
52+
push(a);
53+
break;
54+
}
55+
}
56+
}
57+
printf("%d",pop());
58+
return 0;
59+
}

0 commit comments

Comments
 (0)