-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathinfix_to_postfix.c
194 lines (164 loc) · 4.35 KB
/
infix_to_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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Date: 2020-10-21
*
* Description:
* Convert Infix expression into postfix.
*
* Approach:
* INFIX -> Input String.
* S -> Stack of characters.
* R_POLISH -> output String.
* next -> iterator for INFIX
*
* 1. PUSH "(" into the S and concat ")" into INFIX
* 2. Until precedence of next is less then top of S POP elements from the stack
* and concat in R_POLISH.
* 3. If next and top of the S are not same then PUSH next into S.
*
*
* Complexity:
* Time - O(n^2), space: O(n)
*
* Precedence Table:
* | Symbol | Input precedence | Stack precedence | Rank |
* | +,- | 1 | 2 | -1 |
* | *,/ | 3 | 4 | -1 |
* | ^ | 6 | 5 | -1 |
* | Alphabets | 7 | 8 | 1 |
* | ( | 9 | 0 | - |
* | ) | 0 | - | - |
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// precedence of alphabets in input String and stack
#define INPUTALPHA 7
#define STACKALPHA 8
#define RANKALPHA 1
typedef struct PrecedenceTable
{
char symbol;
int precedence;
int Rank;
} PrecedenceTable;
// IF operators are in input string then we are using this TABLE
PrecedenceTable INFIXOperators[] = {{'+' , 1, -1},
{'-' , 1, -1},
{'*' , 3, -1},
{'/' , 3, -1},
{'^' , 5, -1},
{'(' , 9}, // Prenthesis doesn't have Rank
{')' , 0}};
// IF operators are in STACK then we are using this TABLE
PrecedenceTable STACKOperators[] = {{'+' , 2, -1},
{'-' , 2, -1},
{'*' , 4, -1},
{'/' , 4, -1},
{'^' , 6, -1},
{'(' , 0}, // Prenthesis doesn't have Rank
{')' , 0}};
int findrank(char a)
{
for (int i = 0; i < 6; i++)
{
if (a == INFIXOperators[i].symbol)
return INFIXOperators[i].Rank;
}
return RANKALPHA;
}
int isHigher(char input, char stack)
{
int inputPrec=0, stackPrec=0;
if(isalpha(input))
inputPrec = INPUTALPHA;
if(isalpha(stack))
stackPrec = STACKALPHA;
if(inputPrec != INPUTALPHA)
{
for (int i = 0; i < 7; i++)
{
if (input == INFIXOperators[i].symbol)
inputPrec = INFIXOperators[i].precedence;
}
}
if(stackPrec != STACKALPHA)
{
for (int i = 0; i < 7; i++)
{
if (stack == STACKOperators[i].symbol)
stackPrec = STACKOperators[i].precedence;
}
}
if (inputPrec < stackPrec)
{
return 1;
}
else if (inputPrec == stackPrec)
return 0;
else
return -1;
}
typedef struct STACK
{
char *data;
int top;
} STACK;
void PUSH(STACK *s, char symbol)
{
s->data = (char *)realloc(s->data, sizeof(char) * (s->top + 1));
s->data[s->top] = symbol;
s->top++;
}
char POP(STACK *s)
{
char temp = s->data[s->top - 1];
s->data[s->top - 1] = '\0';
s->top--;
return temp;
}
int main()
{
char *INFIX, *R_POLISH, next;
int length = 0, RANK = 0, i = 0;
STACK S;
INFIX = (char *)malloc(sizeof(char) * 100);
R_POLISH = (char *)malloc(sizeof(char) * 100);
R_POLISH[length] = '\0';
S.top = 0;
S.data = NULL;
PUSH(&S, '(');
scanf("%s", INFIX);
strcat(INFIX,")");
i = 0;
length = 0;
next = INFIX[i++];
while (next != '\0')
{
if (S.top < 1)
{
printf("INVALID INFIX EXPRESSION");
exit(0);
}
while (isHigher(next, S.data[S.top - 1]) == 1)
{
char temp = POP(&S);
R_POLISH[length++] = temp;
RANK += findrank(temp);
if (RANK < 1)
{
printf("INVALID INFIX EXPRESSION");
exit(0);
}
}
if (isHigher(next, S.data[S.top - 1]) != 0)
{
PUSH(&S, next);
}
else
POP(&S);
next = INFIX[i++];
}
R_POLISH[length] = '\0';
puts(R_POLISH);
}