-
Notifications
You must be signed in to change notification settings - Fork 0
/
INtoPost.c
66 lines (61 loc) · 1.13 KB
/
INtoPost.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
#include<stdio.h>
#include<string.h>
#define MAX 100
int check(char ch)
{
if(ch=='^')
return 4;
else if(ch=='/' || ch=='*' || ch=='%')
return 3;
else if(ch=='+' || ch=='-')
return 2;
else //(
return 1;
}
void push(char stk[], char ch, int *top)
{
*top=*top+1;
stk[*top]=ch;
}
char pop(char stk[], int *top)
{
char t=stk[*top];
*top=*top-1;
return t;
}
int main()
{
int top=-1,i, ind=0;
char infix[MAX],postfix[MAX],stack[MAX];
printf("Enter the expression ");
scanf("%s",infix);
strupr(infix);
for(i=0;i<strlen(infix);i++)
{
if(infix[i]=='(')
push(stack,infix[i],&top);
else if(infix[i]>='A' && infix[i]<='Z')
postfix[ind++]=infix[i];
else if(infix[i]==')')
{
while(stack[top]!='(')
postfix[ind++]=pop(stack,&top);
top--;//remove ( from stack top
}
else//operator
{
if(check(infix[i])>check(stack[top]))
push(stack,infix[i],&top);
else
{
while(check(infix[i])<=check(stack[top]))
postfix[ind++]=pop(stack,&top);
push(stack,infix[i],&top);
}
}
postfix[ind]='\0';
stack[++top]='\0';
printf("\n %c\t %s \t%s",infix[i],stack,postfix) ;
top--;
}
}