-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrefixToInfix.c
60 lines (45 loc) · 1.17 KB
/
PrefixToInfix.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
char stack[MAX][MAX];
int top = -1;
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
void push(char *str) {
strcpy(stack[++top], str);
}
void pop(char *str) {
strcpy(str, stack[top--]);
}
void prefixtoinfix(char *prefix, char *infix) {
int len = strlen(prefix);
int i;
for ( i = len - 1; i >= 0; i--) {
if (isOperator(prefix[i])) {
char operand1[MAX], operand2[MAX];
char temp[MAX];
pop(operand1);
pop(operand2);
sprintf(temp, "(%s%c%s)", operand1, prefix[i], operand2);
push(temp);
} else {
char temp[MAX];
temp[0] = prefix[i];
temp[1] = '\0';
push(temp);
}
}
pop(infix);
}
int main() {
char prefix[MAX], infix[MAX];
printf("Enter the prefix expression: ");
fgets(prefix, sizeof(prefix), stdin);
prefix[strlen(prefix) - 1] = '\0';
// Remove the newline character from fgets
prefixtoinfix(prefix, infix);
printf("Infix expression is: %s\n", infix);
return 0;
}