-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_eval.c
71 lines (61 loc) · 1.08 KB
/
post_eval.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 <ctype.h>
#define SIZE 500
int stack[SIZE];
int top=-1;
int isEmpty() {
if(top==-1) return 1;
else return 0;
}
int isFull() {
if(top==SIZE-1) return 1;
else return 0;
}
int onTop() {
return stack[top];
}
void push(int element) {
if(isFull())
printf("Stack is full\n");
else {
top++;
stack[top]=element;
}
}
void pop() {
if(isEmpty()==1)
printf("Stack is empty\n");
else
top--;
}
int calculate(char operator,int op1,int op2)
{
switch(operator)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': return op1/op2;
case '%': return op1%op2;
}
}
main()
{
char postfix[200],token;
int i,op1,op2;
printf("Enter postfix expr \n");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{ token=postfix[i];
if(isdigit(token))
push(token-48);//get the numer value
else {
op2=onTop();
pop();
op1=onTop();
pop();
push(calculate(token,op1,op2));
}
}
printf("Result=%d\n",onTop());
}