-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp9.c
More file actions
84 lines (83 loc) · 1.25 KB
/
p9.c
File metadata and controls
84 lines (83 loc) · 1.25 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MS 50
char postfix[50];
struct stack
{
int top;
int item[MS];
}s;
void push(int value)
{
if(s.top==(MS-1))
{
printf("full\n");
}
else
{
s.item[++s.top]=value;
}
}
int pop()
{
if(s.top==-1)
{
printf("empty\n");
exit(0);
}
return(s.item[s.top--]);
}
int empty()
{
if(s.top==-1)
{
return 1;
}
else
{
return 0;
}
}
int operation(int a,int b,char c)
{
switch(c)
{
case '^':return(pow(a,b));
case '*':return(a*b);
case '%':return(a%b);
case '/':return(a/b);
case '+':return(a+b);
case '-':return(a-b);
}
}
int evaluate()
{
int a,b,i,ans ,value;
char symb;
for(i=0;postfix[i]!='\0';i++)
{
symb=postfix[i];
if((symb>='0')&&(symb<='9'))
{
push((int)(symb-'0'));
}
else{
a=pop();
b=pop();;
value=operation(b,a,symb);
push(value);
}
}
ans=pop();
return ans;
}
void main()
{
s.top=-1;
int ans;
printf("enter the postfix\n");
gets(postfix);
ans= evaluate();
printf("the ans is%d\n",ans);
}