-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfixToPrefix.c
411 lines (381 loc) · 11.9 KB
/
infixToPrefix.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//structure of a Stack.
struct Stack{
int* int_array;
char* char_array;
int size;
int top;
};
typedef struct Stack Stack;
//function-definitions;
//clarify refers to adding '$'(separator) after each unit of equation.
int validateAndClarifyEquation(char equation[]);
void printError(char error_msg[], char equation[], int n);
void addSeparator(char equation[], int n);
int isOperator(char character);
int isVariable(const char string[]);
int isDigit(const char string[]);
int checkPrecedence(char operator1, char operator2);
void convertToPrefix(const char equation[]);
int evaluate(int operand1, int operand2, char operator);
void evaluateFromPrefix(const char* equation);
int main(){
char *equation = (char*)calloc(100, sizeof(int));
printf("\nInput INFIX equation: ");
fgets(equation, 100, stdin);
equation[(strlen(equation))-1] = '\0';
int validation_result = validateAndClarifyEquation(equation);
if (validation_result == 1) {
convertToPrefix(equation);
}
return 0;
}
//is-true or false----------------------------------------------------------------//
int isOperator(char character){
if((character == '+') || (character == '-') ||
(character == '*') || (character == '/') ||
(character == '^'))
{
return 1;
}
return 0;
}
int isVariable(const char string[]){
int i=0;
if(((string[i] >= 65) && (string[i] <= 90)) ||
((string[i] >= 97) && (string[i] <= 122)))
{
while(((string[i] >= 65) && (string[i] <= 90)) ||
((string[i] >= 97) && (string[i] <= 122)) ||
((string[i] >= 48) && (string[i] <= 57)))
{
i++;
}
if(string[i] == '\0'){
return 1;
}
else{
return 0;
}
}
return 0;
}
int isDigit(const char string[]){
int i=0;
while((string[i] >= 48) && (string[i] <= 57))
{
i++;
}
if(string[i] == '\0'){
return 1;
}
return 0;
}
//stack operations----------------------------------------------------------------//
void pushChar(Stack* s, char c){
if(s->top >= (s->size)-1){
return;
}
s->char_array[++(s->top)] = c;
}
char popChar(Stack* s){
if(s->top == -1){
return '\0';
}
return s->char_array[(s->top)--];
}
char getCharElement(Stack* s){
if(s->top == -1){
return '\0';
}
return s->char_array[s->top];
}
void pushInt(Stack* s, int n){
if(s->top >= (s->size)-1){
return;
}
s->int_array[++(s->top)] = n;
}
int popInt(Stack* s){
if(s->top == -1){
return '\0';
}
return s->int_array[(s->top)--];
}
//Equation validation and clarification-------------------------------------------//
void printError(char error_msg[], char equation[], int n){
printf("\n%s", error_msg);
printf("\n");
int count = 0;
for(int i = 0 ; equation[i]!='\0' ; i++){
if(equation[i] == '$'){
count++;
continue;
}
printf("%c", equation[i]);
}
printf("\n");
for(int i = 0 ; i<n-count ; i++){
printf(" ");
}
printf("^\n");
}
void addSeparator(char equation[], int n){
for (int i = 99; i > n; i--) {
equation[i] = equation[i - 1];
}
equation[n] = '$';
}
int validateAndClarifyEquation(char equation[]){
Stack* validation_stack = (Stack*)malloc(sizeof(Stack));
validation_stack->char_array = (char*)calloc(100, sizeof(int));
validation_stack->size = 100;
validation_stack->top = -1;
int i=0;
while(equation[i] != '\0'){
if (equation[i] == '('){
pushChar(validation_stack, '(');
addSeparator(equation, ++i);
}
else if((equation[i] >= 48) && (equation[i] <= 57)){
while((equation[i] >= 48) && (equation[i] <= 57)){
i++;
}
if(isOperator(equation[i]) || (equation[i] == ')') ||
(equation[i] == '(') || (equation[i] == '\0'))
{
addSeparator(equation, i);
}
else if(((equation[i] >= 65) && (equation[i] <= 90)) ||
((equation[i] >= 97) && (equation[i] <= 122)))
{
char error_msg[] = "Unexpected Data Value Encountered.";
printError(error_msg, equation,i);
return 0;
}
}
else if(((equation[i] >= 65) && (equation[i] <= 90)) ||
((equation[i] >= 97) && (equation[i] <= 122)))
{
while(((equation[i] >= 65) && (equation[i] <= 90)) ||
((equation[i] >= 97) && (equation[i] <= 122)) ||
((equation[i] >= 48) && (equation[i] <= 57)))
{
i++;
}
if(isOperator(equation[i]) || (equation[i] == ')') ||
(equation[i] == '(') || (equation[i] == '\0'))
{
addSeparator(equation, i);
}
else if(((equation[i] >= 65) && (equation[i] <= 90)) ||
((equation[i] >= 97) && (equation[i] <= 122)))
{
char error_msg[] = "Unexpected Variable Name Encountered.";
printError(error_msg, equation, i);
return 0;
}
}
else if(isOperator(equation[i])){
if(isOperator(equation[i+1])){
char error_msg[] = "Unexpected Operator Encountered.";
printError(error_msg, equation, i+1);
return 0;
}
else if(((equation[i+1] >= 65) && (equation[i+1] <= 90)) ||
((equation[i+1] >= 97) && (equation[i+1] <= 122)) ||
((equation[i+1] >= 48) && (equation[i+1] <= 57)) ||
(equation[i+1] == '(') || (equation[i+1] == ')'))
{
addSeparator(equation, ++i);
}
else {
char error_msg[] = "Unexpected variable or value given after operator.";
printError(error_msg, equation, i);
return 0;
}
}
else if(equation[i] == ' '){
char error_msg[] = "Spaces are not allowed.\n";
printError(error_msg, equation, i);
return 0;
}
else if(equation[i] == ')'){
char result = popChar(validation_stack);
if(result == '\0'){
char error_msg[] = "No opening bracket for ')' found.";
printError(error_msg, equation, i);
return 0;
}
else{
addSeparator(equation, ++i);
}
}
else{
char error_msg[] = "Unknown Character Encountered.";
printError(error_msg, equation, i);
return 0;
}
i++;
}
if(getCharElement(validation_stack)){
char error_msg[] = "No closing bracket for '(' found.";
printError(error_msg, equation,i-1);
return 0;
}
free(validation_stack->char_array);
free(validation_stack);
//reversing the equation.
for(int j=0 ; j<(i/2) ; j++){
char temp = equation[j];
equation[j] = equation[i-j-1];
equation[i-j-1] = temp;
}
equation++;
equation[i-1] = '$';
return 1;
}
//Convert to prefix---------------------------------------------------------------//
int checkPrecedence(char operator1, char operator2){
int op1;
if(operator1 == '^'){op1 = 3;}
else if((operator1 == '*') || (operator1 == '/')){op1 = 2;}
else if((operator1 == '+') || (operator1 == '-')){op1 = 1;}
else{return 0;}
int op2;
if(operator2 == '^'){op2 = 3;}
else if((operator2 == '*') || (operator2 == '/')){op2 = 2;}
else if((operator2 == '+') || (operator2 == '-')){op2 = 1;}
else{return 0;}
if(op1 > op2){return 1;}
return 0;
}
void convertToPrefix(const char equation[]){
Stack* prefix_stack = (Stack*)malloc(sizeof(Stack));
prefix_stack->char_array = (char*)calloc(100, sizeof(char));
prefix_stack->size = 100;
prefix_stack->top = -1;
char* prefix_array = (char*)calloc(100, sizeof(char));
int prefix_array_index = 0;
int i = 0;
while(equation[i] != '\0'){
char ch[10];
int ch_counter = 0;
while((equation[i] != '$') && (equation[i] != '\0')){
ch[ch_counter++] = equation[i];
i++;
}
ch[ch_counter++] = '\0';
ch_counter = 0;
if(isDigit(ch) || isVariable(ch)){
int j = 0;
while(ch[j]){
prefix_array[prefix_array_index++] = ch[j];
j++;
}
prefix_array[prefix_array_index++] = '$';
}
else{
while((isOperator(getCharElement(prefix_stack))) &&
(checkPrecedence(getCharElement(prefix_stack), ch[0])))
{
char operator = popChar(prefix_stack);
prefix_array[prefix_array_index++] = operator;
prefix_array[prefix_array_index++] = '$';
}
if(ch[0] != '('){
pushChar(prefix_stack, ch[0]);
}
else{
while(getCharElement(prefix_stack) != ')'){
prefix_array[prefix_array_index++] = popChar(prefix_stack);
prefix_array[prefix_array_index++] = '$';
}
popChar(prefix_stack);
}
}
i++;
}
while(getCharElement(prefix_stack)){
char operator = popChar(prefix_stack);
prefix_array[prefix_array_index++] = operator;
prefix_array[prefix_array_index++] = '$';
}
printf("\nPREFIX FORM:-\n");
for(int j = strlen(prefix_array)-1 ;j>=0 ; j--){
if(prefix_array[j] != '$'){
printf("%c", prefix_array[j]);
}
}
printf("\n");
free(prefix_stack->char_array);
free(prefix_stack);
evaluateFromPrefix(prefix_array);
free(prefix_array);
}
//evaluating from prefix array----------------------------------------------------//
int evaluate(int operand1, int operand2, char operator){
if(operator == '+'){
return (operand1 + operand2);
}
else if(operator == '-'){
return (operand1 - operand2);
}
else if(operator == '*'){
return (operand1 * operand2);
}
else if(operator == '/'){
printf("\nWarning: Dividing two operands will cause");
printf("\ndigit loss after the decimal point.\n");
return (operand1/operand2);
}
else if(operator == '^'){
int result = 1;
for(int i = 0 ; i < operand2 ; i++){
result = result*operand1;
}
return result;
}
else{
return 0;
}
}
void evaluateFromPrefix(const char* equation){
Stack* prefix_stack = (Stack*)malloc(sizeof(Stack));
prefix_stack->int_array = (int*)malloc(sizeof(equation));
prefix_stack->size = 10;
prefix_stack->top = -1;
for(int i = 0 ; equation[i] ; i++){
char ch[10];
int ch_counter = 0;
while((equation[i] != '$') && (equation[i] != '\0')){
ch[ch_counter++] = equation[i];
i++;
}
ch[ch_counter++] = '\0';
ch_counter = 0;
int num = 0;
if(isDigit(ch)){
for(int j = 0 ; ch[j]; j++){
num = num * 10 + (ch[j] - 48);
}
pushInt(prefix_stack, num);
}
else if(isVariable(ch)){
printf("\nEnter Value of '%s' in the equation: ", ch);
scanf("%d", &num);
pushInt(prefix_stack, num);
}
else if (isOperator(ch[0])){
int operand1 = popInt(prefix_stack);
int operand2 = popInt(prefix_stack);
pushInt(prefix_stack, evaluate(operand1, operand2, ch[0]));
}
}
printf("\nResult after evaluation is: %d\n", popInt(prefix_stack));
free(prefix_stack->int_array);
free(prefix_stack);
}
//Author - Shubham, github-url: "github.com/shubhamistic";