-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5a. Postfix-Evaluation.c
71 lines (63 loc) · 1.3 KB
/
5a. 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
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
float stack[20];
int top=-1;
int isEmpty(){
if(top<0)
return 1;
else
return 0;
}
void push(char symbol){
stack[++top]=symbol;
return;
}
int pop(){
int temp=stack[top];
--top;
return temp;
}
float compute(char symbol,int op1,int op2)
{
switch(symbol)
{
case '+':return op1+op2;
break;
case '-':return op1-op2;
break;
case '*':return op1*op2;
break;
case '/':return op1/op2;
break;
case '%':return op1%op2;
break;
case '^':return pow(op1, op2);
break;
default :return 0;
}
}
int main(){
float res,op1,op2;
char postfix[20],symbol;
printf("Enter Postfix expression:");
scanf("%s",postfix);
for(int i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
{
push(symbol-'0');
}
else
{
op2=pop();
op1=pop();
res=compute(symbol,op1,op2);
push(res);
}
}
res=stack[top];
printf("Evaluated Postfix Expression is :%f",res);
}