-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequation conversion
200 lines (172 loc) · 4.59 KB
/
equation conversion
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
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
int priority(char alpha) {
if (alpha == '+' || alpha == '-')
return 1;
if (alpha == '*' || alpha == '/')
return 2;
if (alpha == '^')
return 3;
return 0;
}
string infixtopostfix(string infix) {
string postfix = "";
stack<char> stack;
for (char& c : infix) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
postfix += c;
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.empty() && stack.top() != '(') {
postfix += stack.top();
stack.pop();
}
stack.pop();
} else {
while (!stack.empty() && priority(c) <= priority(stack.top())) {
postfix += stack.top();
stack.pop();
}
stack.push(c);
}
}
while (!stack.empty()) {
postfix += stack.top();
stack.pop();
}
return postfix;
}
void reverseString(string& str) {
reverse(str.begin(), str.end());
}
void infixToPrefix(string& infix, string& prefix) {
reverseString(infix);
for (char& c : infix) {
if (c == '(') {
c = ')';
} else if (c == ')') {
c = '(';
}
}
reverseString(infix);
prefix = infix;
}
bool isOperator(char x) {
return (x == '+' || x == '-' || x == '*' || x == '/' || x == '^');
}
string prefixToInfix(string prefix, int& index) {
string infix = "";
if (isOperator(prefix[index])) {
infix += "(";
infix += prefixToInfix(prefix, ++index);
infix += prefix[index++];
infix += prefixToInfix(prefix, ++index);
infix += ")";
} else {
infix += prefix[index++];
}
return infix;
}
string convertPrefixToInfix(string prefix) {
int index = 0;
return prefixToInfix(prefix, index);
}
string getInfix(string exp) {
stack<string> s;
for (char c : exp) {
if (isOperator(c)) {
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
s.push("(" + op2 + c + op1 + ")");
} else {
s.push(string(1, c));
}
}
return s.top();
}
string postToPre(string post_exp) {
stack<string> s;
for (char c : post_exp) {
if (isOperator(c)) {
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
string temp = c + op2 + op1;
s.push(temp);
} else {
s.push(string(1, c));
}
}
string ans = "";
while (!s.empty()) {
ans += s.top();
s.pop();
}
return ans;
}
string preToPost(string pre_exp) {
stack<string> s;
for (int i = pre_exp.size() - 1; i >= 0; i--) {
if (isOperator(pre_exp[i])) {
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
string temp = op1 + op2 + pre_exp[i];
s.push(temp);
} else {
s.push(string(1, pre_exp[i]));
}
}
return s.top();
}
int main() {
string expression, result;
int choice;
cout << "Select one of the following:" << endl;
cout << "1) Infix to postfix" << endl;
cout << "2) Infix to prefix" << endl;
cout << "3) Prefix to infix" << endl;
cout << "4) Postfix to infix" << endl;
cout << "5) Postfix to prefix" << endl;
cout << "6) Prefix to postfix" << endl;
cin >> choice;
cout << "Enter an expression: ";
cin >> expression;
switch (choice) {
case 1:
result = infixtopostfix(expression);
cout << "Postfix expression: " << result << endl;
break;
case 2:
infixToPrefix(expression, result);
cout << "Prefix expression: " << result << endl;
break;
case 3:
result = convertPrefixToInfix(expression);
cout << "Infix expression: " << result << endl;
break;
case 4:
result = getInfix(expression);
cout << "Infix expression: " << result << endl;
break;
case 5:
result = postToPre(expression);
cout << "Prefix expression: " << result << endl;
break;
case 6:
result = preToPost(expression);
cout << "Postfix expression: " << result << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}