-
Notifications
You must be signed in to change notification settings - Fork 1
/
infix.c
105 lines (98 loc) · 1.36 KB
/
infix.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<stdio.h>
#include<string.h>
char stack[20],postfix[20];
int j=-1,top=0;
void pushtopostfix(char x);
void infixtopostfix(char x);
int priority(char x);
int main()
{
int i=0;
char a[20],x;
printf("Enter the infix expression :\n");
gets(a);
stack[top] = '(';
while(a[i] != '\0')
{
x=a[i];
switch (x)
{
case '+': infixtopostfix(x);
break;
case '-': infixtopostfix(x);
break;
case '*': infixtopostfix(x);
break;
case '/': infixtopostfix(x);
break;
case '^': infixtopostfix(x);
break;
case '(': {
top++;
stack[top] = '(';
}
break;
case ')': {
while(stack[top] != '(')
{
j++;
postfix[j] = stack[top];
top--;
}
top--;
}
break;
default : pushtopostfix(x);
break;
}
i++;
}
while(stack[top] != '(')
{
j++;
postfix[j] = stack[top];
top--;
}
top--;
printf("Postfix expression is :\n");
puts(postfix);
return 0;
}
//Functions
void pushtopostfix(char x)
{
j++;
postfix[j] = x;
}
void infixtopostfix(char x)
{
int a,b;
a = priority(x);
b = priority(stack[top]);
if(a>b)
{
top++;
stack[top] = x;
}
else
{
j++;
postfix[j] = stack[top];
stack[top] = x;
}
}
int priority(char x)
{
if(x == '+')
return 1;
else if(x == '-')
return 1;
else if(x == '*')
return 2;
else if(x == '/')
return 2;
else if(x == '^')
return 3;
else
return 0;
}