-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix.c
64 lines (61 loc) · 827 Bytes
/
postfix.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
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
int pro(int x);
int push(char y);
char pop();
char stck[100];
int top=0;
int main()
{
char a[100],b[100];
int i,j=0;
printf("enter the exprssion: \n");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(isalnum(a[i])!=0)
b[j++]=a[i];
else if(pro(stck[top])<pro(a[i]))
{
push(a[i]);
}
else if(pro(stck[top])>pro(a[i]))
{ while(pro(stck[top])>pro(a[i]))
{ b[j++]=pop();
}
push(a[i]);
}
}
while(top!=-1)
{
b[j++]=pop();
}
for(i=0;i<strlen(b);i++)
{
printf("%c",b[i]);
}
return 0;
}
int pro(int x)
{
if(x=='$' || x=='^')
return 3;
else if(x=='*' || x=='/')
return 2;
else if(x=='+' || x=='-')
return 1;
else
return 0;
}
int push(char y)
{
stck[top++]=y;
}
char pop()
{
char m;
m=stck[--top];
return m;
}