-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathinfix_postfix.c
52 lines (49 loc) · 970 Bytes
/
infix_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
#include<stdio.h>
#include<string.h>
int priority(char c)
{
if(c=='^')
return 4;
else if(c=='*' || c=='/' || c=='%')
return 3;
else if(c=='+' || c=='-')
return 2;
else
return 1;
}
void main()
{
char infix[100],post[100],stack[50];
int top=-1,i,j=0;
printf("\n Enter expression ");
scanf("%s",infix);
strupr(infix);
for(i=0;i<strlen(infix);i++)
{
if(infix[i]=='(')
stack[++top]=infix[i];
else if(infix[i]>='A' && infix[i]<='Z')
post[j++]=infix[i];
else if(infix[i]==')')
{
while(stack[top]!='(')
post[j++]=stack[top--];
top--; // remove '(' from stack
}
else//operator
{
if(priority(infix[i])>priority(stack[top]))
stack[++top]=infix[i];
else //same or low priority
{
while(priority(stack[top])>=priority(infix[i]))
post[j++]=stack[top--];
stack[++top]=infix[i];
}
}
stack[++top]='\0';
post[j]='\0';
printf("\n %c \t %s \t %s",infix[i],stack,post);
top--;
}
}