-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluation_of_Postfix_Expression.c
59 lines (56 loc) · 1.31 KB
/
Evaluation_of_Postfix_Expression.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
// Give an input Postfix Expression, write the program to evaluate the given postfix expression using Stack Operations.
// Input : Postfix Expression and List of values of the variables
// Output : Result of the expression
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int s[20],top=-1;
void push(int e)
{
top++;
s[top]=e;
}
int pop()
{
int temp=top;
top--;
return s[temp];
}
int main()
{
char str[20];
int a,n,b,c;
scanf("%s",str);
for(int i=0;i<strlen(str);i++)
{
if(str[i]>='a' && str[i]<='z')
{
scanf("%d",&n);
push(n);
}
else
{
switch(str[i])
{
case '+': a=pop()+pop();
push(a);
break;
case '-': b=pop();
c=pop();
a=c-b;
push(a);
break;
case '*': a=pop()*pop();
push(a);
break;
case '/': b=pop();
c=pop();
a=c/b;
push(a);
break;
}
}
}
printf("%d",pop());
return 0;
}