-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequation.cpp
304 lines (277 loc) · 7.53 KB
/
equation.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
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
// Implemtation of equation class
// William Immendorf - 2016
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <locale>
#include <stdexcept>
#include <utility>
#include "equation.hpp"
#include "utility.hpp"
namespace EquParser
{
Equation::Equation()
{
// This space intentionally left blank
}
Equation::Equation(const std::string & equation) : infix_equation(equation)
{
convert_to_rpn();
}
Equation::~Equation()
{
// This makes the linker happy.
}
bool Equation::handle_input(const char c, std::deque<std::string> & output_queue, std::stack<char> & operator_stack, LastAdded last_added) const
{
return false;
}
bool Equation::handle_term(const std::string term, std::stack<double> & result_stack) const
{
return false;
}
// Simply returns the raw infix equation string
std::string Equation::infix() const
{
return infix_equation;
}
// And set the raw infix equation, running the parse function
void Equation::infix(const char * equation)
{
std::string temp_string(equation);
infix_equation = temp_string;
convert_to_rpn();
}
void Equation::infix(const std::string & equation)
{
infix_equation = equation;
convert_to_rpn();
}
// Returns the RPN equation as its native queue format
std::queue<std::string> Equation::rpn() const
{
return rpn_equation;
}
// Create a string representation of the RPN formatted equation
std::string Equation::rpn_tostring() const
{
std::queue<std::string> clone_queue(rpn_equation);
std::string result;
while (!clone_queue.empty())
{
result.append(clone_queue.front());
result.push_back(' ');
clone_queue.pop();
}
return result.substr(0, result.length() - 1);;
}
// Evaluates the equation from stored queue and returns the result.
double Equation::evaluate() const
{
using std::cerr;
using std::endl;
using std::string;
std::stack<double> result_stack;
std::queue<string> clone_queue(rpn_equation);
std::locale loc;
// If the actual RPN equation is empty, toss an exception. Not supposed to happen.
if (clone_queue.empty())
{
throw std::runtime_error("Error: No expression present, are you sure you entered a valid infix expression?");
}
// Parse through the queue, evaluting each term within (as per RPN status)
while (!clone_queue.empty())
{
string term = clone_queue.front();
clone_queue.pop();
// First process any special rules:
if (handle_term(term, result_stack))
continue;
// Ignore remaining whitespace
else if (isspace(term[0], loc))
continue;
// Numbers are parsed and go on stack
else if (isdigit(term[0], loc))
result_stack.push(std::stod(term));
else
{
// It's a operator, attempt to parse it
try
{
switch (term[0])
{
case '+':
process_operator(result_stack, [] (double val1, double val2) { return val1 + val2; });
break;
case '-':
process_operator(result_stack, [] (double val1, double val2) { return val2 - val1; });
break;
case '*':
process_operator(result_stack, [] (double val1, double val2) { return val1 * val2; });
break;
case '/':
process_operator(result_stack, [] (double val1, double val2) { return val2 / val1; });
break;
case '^':
process_operator(result_stack, [] (double val1, double val2) { return pow(val2, val1); });
break;
default:
string error = "Unrecognized symbol ";
error.append(term);
throw std::runtime_error(error);
}
}
catch (std::runtime_error& e)
{
cerr << "Exception: " << e.what() << endl;
exit(EXIT_FAILURE);
}
}
}
// All RPN equations end with one value on the result stack - if not, error out.
if (result_stack.size() == 1)
{
return result_stack.top();
}
else
{
cerr << "Error: Too many values in result stack" << endl;
exit(EXIT_FAILURE);
}
}
// Internal method - make a RPN representation of the inputted infix equation.
void Equation::convert_to_rpn()
{
using std::string;
std::deque<string> output_queue;
std::stack<char> operator_stack;
std::locale loc;
LastAdded last_added = None;
bool decimal = false;
// Loop through equation and use shunting yard algorithm to handle it
for (char c : infix_equation)
{
// Reset decimal flag if not a decimal pointer or digit
if (c != '.' && !isdigit(c, loc))
{
decimal = false;
}
// First process special rules:
if (handle_input(c, output_queue, operator_stack, last_added))
continue;
// Whitespace or alphabetical characters are ignored.
else if (isspace(c, loc) || isalpha(c, loc))
continue;
// Number characters are addded to output queue
else if (isdigit(c, loc))
{
// If still part of a decimal, excend current number
if (decimal)
{
string prev_string = output_queue.back();
output_queue.pop_back();
prev_string.push_back(c);
output_queue.push_back(prev_string);
}
else
// Make a new number on the queue
output_queue.push_back(string(1, c));
last_added = Queue;
}
// With a decimal point, start a decimal number (if one isn't already started)
else if (c == '.')
{
if (decimal)
continue;
else
{
string prev_string = output_queue.back();
if (isdigit(prev_string[0],loc))
{
output_queue.pop_back();
prev_string.push_back('.');
output_queue.push_back(prev_string);
decimal = true;
last_added = Queue;
}
else
continue;
}
}
// Parenthesis open, add to operator stack
else if (c == '(')
{
// If the preciding result was a number, add an additional multiply operator
string prev_string = output_queue.back();
if (isdigit(prev_string[0],loc))
operator_stack.push('*');
operator_stack.push(c);
last_added = Stack;
}
// Partnthesis close, push all operators after parenthesis open to output queue
else if (c == ')')
{
if (!operator_stack.empty())
{
char top_operator = operator_stack.top();
while (top_operator != '(')
{
output_queue.push_back(string(1, top_operator));
operator_stack.pop();
if (operator_stack.empty())
{
std::cerr << "Error: Unclosed parenthesis expression. Exiting." << std::endl;
exit(EXIT_FAILURE);
}
top_operator = operator_stack.top();
}
operator_stack.pop(); // get rid of used parenthesis
}
else
{
std::cerr << "Error: Cannot close a parenthesis with no operators. Exiting." << std::endl;
exit(EXIT_FAILURE);
}
}
// Operator - add to stack
else
{
// If the new operator has greater presidence over the other operators in stack, push last operator to queue
while (!operator_stack.empty())
{
char top_operator = operator_stack.top();
if (precendence_less_than(c, top_operator))
{
output_queue.push_back(string(1, top_operator));
operator_stack.pop();
}
else
break;
}
operator_stack.push(c);
last_added = Stack;
}
}
// Clear RPN equation:
std::queue<string> empty_temp;
std::swap(rpn_equation, empty_temp);
// Empty output queue into RPN equation
while (!output_queue.empty())
{
rpn_equation.push(output_queue.front());
output_queue.pop_front();
}
// Empty operator stack into RPN equation
while (!operator_stack.empty())
{
rpn_equation.push(string(1, operator_stack.top()));
operator_stack.pop();
}
}
// Allow direct printing of equation in RPN form
std::ostream & operator<<(std::ostream & os, const Equation & equation)
{
os << equation.rpn_tostring();
return os;
}
}