-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvaluator.cpp
More file actions
267 lines (249 loc) · 8.16 KB
/
Evaluator.cpp
File metadata and controls
267 lines (249 loc) · 8.16 KB
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
// Originally written by Luke Simmons and Megan Sword
// Edited by Alfred Ledgin
// 11/4/2015
// CS 303
// Project 2
//testing this out
#include "Evaluator.h"
using namespace std;
Evaluator::Evaluator() {
eqString = "";
}
Evaluator::Evaluator(string line) {
eqString = line;
}
const double Evaluator::evaluate(string equation) {
if (equation.length() == 0)
throw invalid_argument("expression cannot be empty");
bool hitNumber = false;
bool hitOperator = false;
stack<Operator> opStack;
stack<double> numbers;
int index = 0;
while (index < equation.size()) {
if (equation[index] == ' ') {
index++;
}
else if (equation[index] == '(') { //If the item is an '(', evaluate the substring.
int subIdx = index;
int parens = 1;
while (parens > 0)
{
subIdx++;
if (subIdx > equation.length() - 1)
{
string error = "missing closing parenthesis @ char " + to_string(equation.length() - 1);
throw invalid_argument(error);
}
if (equation[subIdx] == '(')
parens++;
if (equation[subIdx] == ')')
parens--;
}
string substring = equation.substr(index + 1, subIdx - (index + 1));
// Reference (for using substr function):
// "std::string::substr." _cplusplus.com_. cplusplus.com, 2015.
// Web. 4 Nov. 2015.
// <http://www.cplusplus.com/reference/string/string/substr/>.
numbers.push(evaluate(substring));
index = subIdx;
index++;
}
else if (isdigit(equation[index])) { // If the item is a digit, build the number until the item is no longer a digit.
if (hitOperator == false && !numbers.empty()) {
string error = "expression cannot have two operands in a row @ char " + to_string(index);
throw std::invalid_argument(error);
}
hitNumber = true;
double newOperand = 0;
string newOperandString;
while (isdigit(equation[index])) {
newOperandString += equation[index];
index++;
}
hitOperator = false;
numbers.push(stoi(newOperandString));
}
else { // The item must be an operator.
hitOperator = true;
string temp;
temp += equation[index];
if (index == 0 && temp == "-")
{
if (equation.length() > 1 && equation[1] != '-')
// If the first operator is a -, then it is the negation operator.
temp += "#";
}
else if (equation.length() > index + 1)
{
if (temp == "-" && !isdigit(equation[index - 1])
&& equation[index + 1] != '-' && equation[index + 1] != ' ')
// If the operator is a -, and the preceding character is not a digit,
// then the operator is the negation operator.
temp += "#";
}
index++;
if (!isdigit(equation[index]) && equation[index] != '('
&& equation[index] != ')' && equation[index] != '!') { // Check whether the operator contains 2 characters.
if (equation[index - 1] == equation[index] || equation[index - 1] == '!'
|| equation[index - 1] == '<' || equation[index - 1] == '>') // Check for valid two-char operator.
{
if (temp.length() == 1) // Cannot have operator with more than 2 characters.
{
if (equation[index - 1] != '-')
{
temp += equation[index];
index++;
}
else // -- is decrement operator only in special cases.
{
if (index >= 2)
{
if (!isdigit(equation[index - 2]))
{
{
temp += equation[index];
index++;
}
}
}
else
{
temp += equation[index];
index++;
}
}
}
}
}
Operator tempO; // Create new operator and test it for errors.
try
{
tempO = Operator(temp);
}
catch(exception)
{
string error = "improper operator combination @ char " + to_string(index - 1);
throw invalid_argument(error);
}
if (tempO.hasType() == ")") // Misplaced closing parenthesis.
{
string error = "misplaced closing parenthesis @ char " + to_string(index - 1);
throw invalid_argument(error);
}
if (opStack.empty()) {
if (hitNumber == false && tempO.hasCategory() == 'b' && numbers.empty()) {
string message = "cannot start expression with binary operator @ char " + to_string(index - 1);
throw invalid_argument(message);
}
else if (hitNumber == false && temp == ")" && numbers.empty()) {
string message = "cannot start expression with closing parenthesis @ char " + to_string(index - 1);
throw invalid_argument(message);
}
opStack.push(tempO);
hitNumber = false;
}
else if (tempO < opStack.top()) {
while (!opStack.empty() && tempO < opStack.top()) {
if (opStack.top().hasCategory() == 'u') {
double tempNum1 = numbers.top();
numbers.pop();
try
{
numbers.push(opStack.top().execute(tempNum1));
}
catch(exception)
{
string message = "division by zero @ char " + to_string(index);
throw invalid_argument(message);
}
opStack.pop();
}
else if (opStack.top().hasCategory() == 'b') {
double tempNum1 = numbers.top();
numbers.pop();
if (numbers.empty())
{
string error = "improper operator combination @ char " + to_string(index - 1);
throw invalid_argument(error);
}
double tempNum2 = numbers.top();
numbers.pop();
try
{
numbers.push(opStack.top().execute(tempNum2, tempNum1));
}
catch (exception)
{
string message = "division by zero @ char " + to_string(index);
throw invalid_argument(message);
}
opStack.pop();
}
}
opStack.push(tempO);
}
else {
if (!opStack.empty()) {
if (tempO.hasCategory() == 'b' && opStack.top().hasCategory() == 'b' && hitNumber == false) {
string message = "cannot have two binary operators in a row @ char " + to_string(index);
throw invalid_argument(message);
}
else if (tempO.hasCategory() == 'b' && opStack.top().hasCategory() == 'u' && hitNumber == false) {
string message = "cannot have a unary operator followed by a binary operator @ char " + to_string(index);
throw invalid_argument(message);
}
else if (temp == ")") {
string message = "closing brace before opening @ char " + to_string(index);
throw invalid_argument(message);
}
}
opStack.push(tempO);
hitNumber = false;
}
}
}
while (index = equation.size() && !opStack.empty()) { // After all of the numbers are read, we need to finish evaluating.
if (opStack.top().hasCategory() == 'u') {
double tempNum1 = numbers.top();
numbers.pop();
try
{
numbers.push(opStack.top().execute(tempNum1));
}
catch (exception)
{
string message = "division by zero @ char " + to_string(index);
throw invalid_argument(message);
}
opStack.pop();
}
if (opStack.empty())
break;
if (opStack.top().hasCategory() == 'b') {
double tempNum1 = numbers.top();
numbers.pop();
if (numbers.empty())
{
string error = "improper operator combination @ char " + to_string(index - 1);
throw invalid_argument(error);
}
double tempNum2 = numbers.top();
numbers.pop();
try
{
numbers.push(opStack.top().execute(tempNum2, tempNum1));
}
catch (exception)
{
string message = "division by zero @ char " + to_string(index);
throw invalid_argument(message);
}
opStack.pop();
}
}
if (numbers.size() == 1)
return numbers.top();
else
throw invalid_argument("too many operands, improper grouping");
}