This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomb0708.c
467 lines (420 loc) · 7.96 KB
/
comb0708.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg Combined 0708
// Prg 07/1409 + Prg 08/1409
// Infix to Postfix Conversion and Evaluvation
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
#define FLAG 1
// INFIX -> PREFIX FUNCTIONS
void convert(char inf[100]);
int rank(char c, int type);
void reverse(char pf[]);
void display(char STACK[], int *TOP, char pf[], char c);
void push(char STACK[], int *TOP, char ITEM);
char pop(char STACK[], int *TOP);
// END
// PREFIX -> EVALUVATION
struct var
{
char letter;
float value;
};
void evaluvate(char pf[100]);
void analyse(struct var alpha[26], char pf[100]);
float result(float x, float y, char op);
int type(char ITEM);
void reverse_e(char arr[]);
void push_e(float STACK_E[], int *TOP_E, float ITEM);
float pop_e(float STACK_E[], int *TOP_E);
void display_e(float STACK_E[], int *TOP_E, char c);
// END
void main()
{
char inf[100];
printf("\nInfix -> Prefix.\n");
printf("Enter Infix Expression: ");
scanf("%s", inf);
convert(inf);
}
// convert() does infix -> prefix
void convert(char inf[100])
{
// STACK
// Dynamic allocation since size depends on MAXSIZE
char *STACK = malloc(sizeof(char) * MAXSIZE);
int TOP = -1;
char x;
push(STACK, &TOP, ')');
// Prefix character array
char pf[100];
pf[0] = '\0';
// Counters
int i, j = 0;
reverse(inf);
// Appending '(' at the end of infix expression
for(i = 0; inf[i] != '\0'; i++);
inf[i] = '(';
i++;
inf[i] = '\0';
// Table Head Row
if(FLAG == 1)
{
printf("\nSr. no.\t\tCHAR\t\tSTACK\t\t\tEXPR\n");
display(STACK, &TOP, pf, ' ');
}
// Loop iterates through each character in infix char array.
for(i = 0; inf[i] != '\0'; i++)
{
x = pop(STACK, &TOP);
if(inf[i] == '(')
{
//Closing bracket found
//Pop out all elements until ')' is encountered
while(x != ')')
{
pf[j] = x;
j++;
pf[j] = '\0';
x = pop(STACK, &TOP);
}
}
else if(rank(inf[i], 1) == -1)
{
// Current character is not operator
pf[j] = inf[i];
j++;
pf[j] = '\0';
push(STACK, &TOP, x);
}
else if(rank(inf[i], 2) >= rank(x, 1))
{
// When incoming character has higher priority than instack.
push(STACK, &TOP, x);
push(STACK, &TOP, inf[i]);
}
else
{
// Incase incoming character has lesser priority than instack.
while(rank(inf[i], 2) < rank(x, 1))
{
// Popping out until priority settles
pf[j] = x;
j++;
pf[j] = '\0';
x = pop(STACK, &TOP);
}
// Pushing since no more priority problems
push(STACK, &TOP, x);
push(STACK, &TOP, inf[i]);
}
display(STACK, &TOP, pf, inf[i]);
}
//printf("\nreverse(%s) = ", pf);
reverse(pf);
//printf("%s\n", pf);
printf("\nPrefix Expression: %s\n\n", pf);
free(STACK);
evaluvate(pf);
}
// rank() returns rank of operator.
// Incase of non operator -1 is returned
int rank(char c, int type)
{
//PRIORITY -> ^ * / + - )
int ISP[6] = {4, 2, 2, 1, 1, 0}; //In-Stack Priority // type = 1
int ICP[6] = {3, 2, 2, 1, 1, 4}; //In-Coming Priority // type = 2
if(type == 1)
{
switch(c)
{
case '^': return ISP[0];
case '*': return ISP[1];
case '/': return ISP[2];
case '+': return ISP[3];
case '-': return ISP[4];
case ')': return ISP[5];
default: return -1;
}
}
else
{
switch(c)
{
case '^': return ICP[0];
case '*': return ICP[1];
case '/': return ICP[2];
case '+': return ICP[3];
case '-': return ICP[4];
case ')': return ICP[5];
default: return -1;
}
}
}
// Reverse() reverses the array passed
// Call by reference method
void reverse(char arr[])
{
int i, j;
char rev[100];
/* Storing reverse string to res[] */
for(i = 0; arr[i] != '\0'; i++);
i--;
for(j = 0; i > -1; i--, j++)
{
rev[j] = arr[i];
}
rev[j] = '\0';
/* res[] now has reverse string */
// Copying back reverse string to pf
for(i = 0; i <= j; i++)
{
arr[i] = rev[i];
}
}
// **STACK OPERATIONS**
// PUSH()
void push(char STACK[], int *TOP, char ITEM)
{
if(*TOP >= MAXSIZE - 1)
{
printf("\nSTACK IS FULL. OVERFLOW\n");
return;
}
*TOP = *TOP + 1;
STACK[*TOP] = ITEM;
}
// POP()
char pop(char STACK[], int *TOP)
{
if(*TOP == -1)
{
printf("\nSTACK IS EMPTY. UNDERFLOW\n");
return '!';
}
char ITEM;
ITEM = STACK[*TOP];
*TOP = *TOP - 1;
return ITEM;
}
// DISPLAY()
void display(char STACK[], int *TOP, char pf[], char c)
{
if(FLAG == 0)
{
return;
}
static int count = 0;
printf("%d\t\t'%c'\t\t", count++, c);
if(*TOP == -1)
{
printf("EMPTY");
}
else
{
for(int i = 0; i <= *TOP; i++)
{
printf("%c", STACK[i]);
}
}
printf("\t\t\t'%s'\n", pf);
}
void evaluvate(char pf[100])
{
// STACK_E - FLOAT
// Dynamic allocation since size depends on MAXSIZE
float *STACK_E = malloc(sizeof(float) * MAXSIZE);
int TOP_E = -1;
float x, y, res;
char ITEM;
// Counter
int i;
struct var alpha[26];
for(i = 0; i < 25; i++)
{
// Initialising letter in structure
alpha[i].letter = '!';
}
analyse(alpha, pf);
reverse_e(pf);
// Appending '#' at the end of infix expression
for(i = 0; pf[i] != '\0'; i++);
pf[i] = '#';
i++;
pf[i] = '\0';
// Table Head Row
printf("\nSr no\t| SYMBOL\t|\tPOP() [X, Y]\t|\tPUSH()\t\t|\tSTACK_E[0 -> TOP_E]\n\n");
// Loop over postfix epression
i = 0;
ITEM = pf[0];
while(ITEM != '#')
{
printf("%d\t|\t%c\t|\t", i+1, ITEM);
if(type(ITEM) == 1)
{
printf("\t\t|\t");
push_e(STACK_E, &TOP_E, (float)(ITEM - '0'));
}
else if(type(ITEM) == 2)
{
printf("\t\t|\t");
push_e(STACK_E, &TOP_E, alpha[ITEM - 'a'].value);
}
else if(type(ITEM) == 3)
{
x = pop_e(STACK_E, &TOP_E);
y = pop_e(STACK_E, &TOP_E);
printf("\t|\t");
// operand swap!!
res = result(x, y, ITEM);
push_e(STACK_E, &TOP_E, res);
}
else
{
printf("\n\nInvalid Expression! End Evaluavtion.\n\n");
return;
}
display_e(STACK_E,&TOP_E,ITEM);
i++;
ITEM = pf[i];
}
printf("%d\t|\t#\t|\t", i + 1);
res = pop_e(STACK_E, &TOP_E);
printf("\t\t|\t\t\t|\tEMPTY\n\n");
printf("RESULT = %f\n\n", res);
free(STACK_E);
}
void analyse(struct var alpha[26], char pf[100])
{
int i = 0;
char ch = pf[i];
float temp;
while(ch != '\0')
{
if(ch >= 'a' && ch <= 'z')
{
if(alpha[ch - 'a'].letter != ch)
{
alpha[ch - 'a'].letter = ch;
printf("Input '%c': ", ch);
scanf("%f", &temp);
alpha[ch - 'a'].value = temp;
}
}
i++;
ch = pf[i];
}
}
float result(float x, float y, char op)
{
switch(op)
{
case '^':
{
if(x == 0)
{
return 0;
}
if(y == 0)
{
return 1;
}
float res = x;
for(int i = 2; i <= y; i++)
{
res *= x;
}
return res;
}
case '/': return x / y;
case '*': return x * y;
case '+': return x + y;
case '-': return x - y;
}
}
int type(char ITEM)
{
if(ITEM >= '0' && ITEM <= '9')
{
return 1;
}
else if(ITEM >= 'a' && ITEM <= 'z')
{
return 2;
}
else if(ITEM == '^' || ITEM == '*' || ITEM == '/' || ITEM == '+' || ITEM == '-')
{
return 3;
}
else
{
return 0;
}
}
// Reverse() reverses the array passed
// Call by reference method
void reverse_e(char arr[])
{
int i, j;
char rev[100];
/* Storing reverse string to res[] */
for(i = 0; arr[i] != '\0'; i++);
i--;
for(j = 0; i > -1; i--, j++)
{
rev[j] = arr[i];
}
rev[j] = '\0';
/* res[] now has reverse string */
// Copying back reverse string to pf
for(i = 0; i <= j; i++)
{
arr[i] = rev[i];
}
}
// **STACK_E OPERATIONS**
// push_e()
void push_e(float STACK_E[], int *TOP_E, float ITEM)
{
if(*TOP_E >= MAXSIZE - 1)
{
printf("\nSTACK_E IS FULL. OVERFLOW\n");
return;
}
*TOP_E = *TOP_E + 1;
STACK_E[*TOP_E] = ITEM;
printf("%-12.2f", ITEM);
}
// pop_e()
float pop_e(float STACK_E[], int *TOP_E)
{
if(*TOP_E == -1)
{
printf("\nSTACK_E IS EMPTY. UNDERFLOW\n");
return '0';
}
float ITEM;
ITEM = STACK_E[*TOP_E];
*TOP_E = *TOP_E - 1;
printf("%-6.2f ", ITEM);
return ITEM;
}
// display_e_I()
void display_e(float STACK_E[], int *TOP_E, char c)
{
printf("\t|\t");
if(*TOP_E == -1)
{
printf("EMPTY");
}
else
{
for(int i = 0; i <= *TOP_E; i++)
{
printf("%.2f ", STACK_E[i]);
}
}
printf("\n");
}