-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix to postfix
78 lines (73 loc) · 1.22 KB
/
infix to postfix
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
#include <stdio.h>
char stack[50];
int top=-1;
int pop()
{
return stack[top--];
}
void push(char x)
{
stack[++top]=x;
}
int prio(char ch)
{ int p;
switch(ch)
{
case '*':
case '/':
p=2;break;
case '+':
case '-':
p=1;
break;
case '(':
p=-1;
break;
}
return p;
}
void conversion(char infix[],char postfix[])
{int k=0;
int i=0;
char p;
while(infix[i]!='\0')
{char p=infix[i];
switch(p)
{
default:
postfix[k++]=p;
break;
case '(':
push(p);
break;
case ')':
while(top!=-1&&stack[top]!='(')
postfix[k++]=pop();
pop();
break;
case '+':
case '-':
case '*':
case '/':
while(top!=-1&&prio(stack[top])>=prio(p))
{
postfix[k++]=pop();
}
push(p);
break;
}
i++;}
while(top!=-1)
{
postfix[k++]=pop();
}
postfix[k]='\0';
}
int main()
{
char infix[50],postfix[50];
printf("enter the expression\n");
scanf("%s",infix);
conversion(infix,postfix);
printf("postfix %s",postfix);
}