-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfixToPrefix.c
146 lines (133 loc) · 3.56 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
char infix[100], prefix[100];
int priority(char symbol) {
switch (symbol) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
void infixtoprefix() {
int start, end, i = 0, j = 0, top = -1;
int stk[100];
start = 0;
end = strlen(infix) - 1;
while (start < end) {
char temp = infix[start];
infix[start] = infix[end];
infix[end] = temp;
start++;
end--;
}
for (i = 0; infix[i] != '\0'; i++) {
char symbol = infix[i];
if (isalnum(symbol)) {
prefix[j++] = symbol;
} else {
if (symbol == ')') {
stk[++top] = symbol;
} else if (symbol == '(') {
while (top != -1 && stk[top] != ')') {
prefix[j++] = stk[top--];
}
top--;
} else {
while (top != -1 && (priority(symbol) < priority(stk[top]))) {
prefix[j++] = stk[top--];
}
stk[++top] = symbol;
}
}
}
while (top != -1) {
prefix[j++] = stk[top--];
}
prefix[j] = '\0';
start = 0;
end = strlen(prefix) - 1;
while (start < end) {
char temp = prefix[start];
prefix[start] = prefix[end];
prefix[end] = temp;
start++;
end--;
}
printf("Prefix expression: %s\n", prefix);
}
void evaluation() {
float st[100];
int top = -1;
for (int i = strlen(prefix) - 1; i >= 0; i--) {
char symbol = prefix[i];
if (isalnum(symbol)) {
st[++top] = symbol - '0';
} else {
float op1 = st[top--];
float op2 = st[top--];
float res;
switch (symbol) {
case '+':
res = op1 + op2;
st[++top] = res;
break;
case '-':
res = op1 - op2;
st[++top] = res;
break;
case '*':
res = op1 * op2;
st[++top] = res;
break;
case '/':
res = op1 / op2;
st[++top] = res;
break;
case '^':
res = pow(op1, op2);
st[++top] = res;
break;
}
}
}
printf("Result: %.2f\n", st[top]);
}
int main() {
int choice;
while (1) {
printf("Enter the choice:\n");
printf("1. Enter the infix expression\n");
printf("2. Convert to prefix\n");
printf("3. Evaluate prefix expression\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the infix expression: ");
scanf("%s", infix);
break;
case 2:
infixtoprefix();
break;
case 3:
evaluation();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}