-
Notifications
You must be signed in to change notification settings - Fork 0
/
Infix2Postfix.c
168 lines (135 loc) · 3.1 KB
/
Infix2Postfix.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
typedef struct
{
char *data;
int top;
int size;
} Stack;
Stack* initStack(int size)
{
Stack *s = (Stack*)malloc(sizeof(Stack));
s->data = (char*)malloc(sizeof(char) * size);
s->top = -1;
s->size = size;
return s;
}
bool isEmpty(Stack *s)
{
return s->top == -1;
}
bool isFull(Stack *s)
{
return s->top == s->size - 1;
}
void push(Stack *s, char c)
{
if (isFull(s)) {
printf("Stack is full!\n");
return;
}
s->data[++s->top] = c;
}
char pop(Stack *s)
{
if (isEmpty(s)) {
printf("Stack is empty!\n");
return '\0';
}
return s->data[s->top--];
}
char peek(Stack *s)
{
if (isEmpty(s)) {
printf("Stack is empty!\n");
return '\0';
}
return s->data[s->top];
}
int isOperator(char c)
{
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
return 1;
}
return 0;
}
int precedence(char c)
{
if (c == '+' || c == '-') {
return 1;
} else if (c == '*' || c == '/') {
return 2;
} else if (c == '^') {
return 3;
}
return -1;
}
char* StringReverse(char *str)
{
int len = strlen(str);
char *rev = (char*) malloc(sizeof(char) * len);
int i = 0;
int j = len - 1;
while (j >= 0) {
rev[i++] = str[j--];
}
rev[i] = '\0';
return rev;
}
char* infixToPostfix(char *infix)
{
infix = StringReverse(infix);
int len = strlen(infix);
char *postfix = (char*)malloc(sizeof(char) * (len + 1));
Stack *s = initStack(len);
for (int i = 0; i < len; i++) {
if (infix[i] == '(') {
infix[i] = ')';
} else if (infix[i] == ')') {
infix[i] = '(';
}
}
int j = 0;
for (int i = 0; i < len; i++) {
if (infix[i] == '(') {
push(s, infix[i]);
} else if (isalpha(infix[i]) || isdigit(infix[i])) {
postfix[j++] = infix[i];
} else if (isOperator(infix[i])) {
while (!isEmpty(s) && precedence(infix[i]) <= precedence(peek(s))) {
postfix[j++] = pop(s);
}
push(s, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(s) && peek(s) != '(') {
postfix[j++] = pop(s);
}
pop(s);
}
}
while (!isEmpty(s)) {
postfix[j++] = pop(s);
}
postfix[j] = '\0';
return postfix;
}
int main(int argc, char** argv)
{
char* infix = "5+6*7";
char* postfix = infixToPostfix(infix);
printf("Infix: %s\n", infix);
printf("Postfix: %s\n", postfix);
if (strcmp(postfix, "76*5+") == 0) printf("TEST PASS\n");
else printf("TEST FAIL\n");
printf("=======================\n");
char* new_infix = malloc(sizeof(char) * 100);
printf("Enter an infix expression: ");
scanf("%s", new_infix);
printf("The infix expression is: %s\n", new_infix);
char* new_postfix = infixToPostfix(new_infix);
printf("The postfix expression is: %s\n", new_postfix);
return 0;
}