-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPostfixEvaluation.c
60 lines (60 loc) · 1.38 KB
/
PostfixEvaluation.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
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define Msize 100
//declaring size and setting top
int top=-1;
int S[Msize];
//to push a number into stack
void push(int x){
if(top!=Msize-1){
top=top+1;
S[top]=x;
}
}
//to pop number from stack during incoming operator
int pop(){
if(top!=-1){
top=top-1;
return S[top+1];
}
}
//to check for operator
bool isOperator(char ch){
return (ch=='+' || ch == '-' || ch =='*' || ch=='/' || ch =='^');
}
int main(){
char C[30];
int i=0,o1,o2,r;
printf("Enter expression : ");
scanf("%s",&C);
int l= strlen(C);
while(i!=l){
if (!isOperator(C[i])){//incoming is number
int n = C[i]-'0';
push(n);
}
else{
//obtaining operants from stack
o2=pop();
o1=pop();
switch (C[i]){
case '+':r=o1+o2;break;
case '-':r=o1-o2;break;
case '*':r=o1*o2;break;
case '/':r=o1/o2;break;
case '^':
r=1;
for (int k=1;k<=o2;k++){
r=r*o1;
}
break;
default:printf("Invalid operators\n");return 0;
}
//pushing result back to stack
push(r);
}
i=i+1;
}
printf("Result is : %d\n",pop());
}