-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotationConverter.cpp
207 lines (174 loc) · 5.54 KB
/
NotationConverter.cpp
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
#include "NotationConverter.hpp"
#include "Deque.hpp"
#include <stdexcept>
#include <sstream>
#include <algorithm>
// Helper function definitions
bool NotationConverter::isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
int NotationConverter::getPrecedence(char ch) {
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
Deque NotationConverter::decodeInput(std::string& inStr) {
inStr = preprocess(inStr);
Deque dq;
stringstream ss(inStr);
string token;
while (ss >> token) {
for (char c: token) {
if (!isalnum(c) && !isOperator(c) && c != '(' && c != ')') {
throw runtime_error("Invalid character encountered");
}
}
dq.pushRear(token);
}
return dq;
}
string NotationConverter::formatExpression(const string & str) {
string result;
for (size_t i = 0; i < str.size(); ++i) {
if (str[i] == ' ' && i > 0 && (str[i - 1] == '(' || str[i - 1] == ')')) {
continue;
}
if (str[i] == ' ' && i < str.size() - 1 && (str[i + 1] == '(' || str[i + 1] == ')')) {
continue;
}
result += str[i];
}
string finalResult;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] == '*' || result[i] == '/' || result[i] == '+' || result[i] == '-') {
if (i > 0 && result[i - 1] != ' ') {
finalResult += ' ';
}
finalResult += result[i];
if (i < result.size() - 1 && result[i + 1] != ' ') {
finalResult += ' ';
}
} else {
finalResult += result[i];
}
}
return finalResult;
}
string NotationConverter::preprocess(const string & str) {
string result;
for (char ch: str) {
if (ch == '(' || ch == ')' || isOperator(ch)) {
result += " ";
result += ch;
result += " ";
} else {
result += ch;
}
}
return result;
}
string NotationConverter::infixToPostfix(string inStr) {
Deque stack;
Deque input = decodeInput(inStr);
string output = "";
while (!input.isEmpty()) {
string token = input.popFront();
if (!isOperator(token[0]) && token != "(" && token != ")") {
output += token + " ";
} else if (token == "(") {
stack.pushRear(token);
} else if (token == ")") {
while (stack.getRear() != "(") {
output += stack.popRear() + " ";
}
stack.popRear();
} else {
while (!stack.isEmpty() && getPrecedence(token[0]) <= getPrecedence(stack.getRear()[0])) {
output += stack.popRear() + " ";
}
stack.pushRear(token);
}
}
while (!stack.isEmpty()) {
output += stack.popRear() + " ";
}
if (!output.empty()) {
output.pop_back();
}
return output;
}
string NotationConverter::postfixToInfix(string inStr) {
Deque stack;
Deque input = decodeInput(inStr);
while (!input.isEmpty()) {
string token = input.popFront();
if (!isOperator(token[0])) {
stack.pushRear(token);
} else {
string operand2 = stack.popRear();
string operand1 = stack.popRear();
string infixExp = "(" + operand1 + " " + token + " " + operand2 + ")";
stack.pushRear(infixExp);
}
}
return formatExpression(stack.popRear());
}
string NotationConverter::postfixToPrefix(string inStr) {
string infixStr = postfixToInfix(inStr);
return infixToPrefix(infixStr);
}
string NotationConverter::infixToPrefix(string inStr) {
Deque stack;
reverse(inStr.begin(), inStr.end());
Deque input = decodeInput(inStr);
string output = "";
while (!input.isEmpty()) {
string token = input.popFront();
if (!isOperator(token[0]) && token != "(" && token != ")") {
output = token + " " + output;
} else if (token == ")") {
stack.pushRear(token);
} else if (token == "(") {
while (stack.getRear() != ")") {
output = stack.popRear() + " " + output;
}
stack.popRear();
} else {
while (!stack.isEmpty() && getPrecedence(token[0]) <= getPrecedence(stack.getRear()[0])) {
output = stack.popRear() + " " + output;
}
stack.pushRear(token);
}
}
while (!stack.isEmpty()) {
output = stack.popRear() + " " + output;
}
return output;
}
string NotationConverter::prefixToInfix(string inStr) {
Deque stack;
Deque input = decodeInput(inStr);
while (!input.isEmpty()) {
string token = input.popRear();
if (!isOperator(token[0])) {
stack.pushRear(token);
} else {
string operand1 = stack.popRear();
string operand2 = stack.popRear();
string infixExp = "(" + operand1 + " " + token + " " + operand2 + ")";
stack.pushRear(infixExp);
}
}
return formatExpression(stack.popRear());
}
string NotationConverter::prefixToPostfix(string inStr) {
string infixStr = prefixToInfix(inStr);
return infixToPostfix(infixStr);
}