-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.cpp
More file actions
297 lines (253 loc) · 7.39 KB
/
expression.cpp
File metadata and controls
297 lines (253 loc) · 7.39 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
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
/**
* CS 106B/X Stanford 1-2-3
* This file implements the expression.h interface.
*
* Please do not modify this provided file. Your turned-in files should work
* with an unmodified version of all provided code files.
*
* @author Marty Stepp, Julie Zelenski, Jerry Cain
* @version 2016/12/02
* - made value default to 0.0 on Expression construction
* @version 2016/11/29
* - 16au 106x version (stepp)
*/
#include "expression.h"
#include "error.h"
#include "strlib.h"
#include "spreadsheet.h"
/**
* Implementation notes: Expression
* --------------------------------
* The Expression class itself implements only those methods that
* are not designated as pure virtual.
*/
Expression::Expression()
: rawText(""),
value(0.0) {
/* Empty */
}
Expression::~Expression() {
/* Empty */
}
std::string Expression::getRawText() const {
return rawText;
}
double Expression::getValue() const {
return value;
}
bool Expression::isFormula() const {
ExpressionType type = getType();
return type == IDENTIFIER || type == COMPOUND || type == RANGE;
}
void Expression::setRawText(const std::string& rawText) {
this->rawText = rawText;
}
void Expression::setValue(double value) {
this->value = value;
}
std::string Expression::getFunction() const {
error("Expression::getFunction: called on a non-Range expression object");
return "";
}
const Expression* Expression::getLeft() const {
error("Expression::getLeft: called on a non-Compound expression object");
return nullptr;
}
std::string Expression::getOperator() const {
error("Expression::getOperator: called on a non-Compound expression object");
return "";
}
Range Expression::getRange() const {
error("Expression::getRange: called on a non-Range expression object");
return Range();
}
const Expression* Expression::getRight() const {
error("Expression::getRight: called on a non-Compound expression object");
return nullptr;
}
/**
* Implementation notes: CompoundExp
* ---------------------------------
* The implementation of eval for CompoundExp evaluates the left and right
* subexpressions recursively and then applies the operator. Assignment is
* treated as a special case because it does not evaluate the left operand.
* Invariant: lhs != nullptr && rhs != nullptr
*/
Set<std::string> CompoundExp::KNOWN_OPERATORS {"+", "-", "*", "/"};
CompoundExp::CompoundExp(const std::string& op, Expression* lhs, Expression* rhs) {
this->op = op;
if (!lhs) {
error("CompoundExp::constructor: null left sub-expression");
}
if (!rhs) {
error("CompoundExp::constructor: null right sub-expression");
}
this->lhs = lhs;
this->rhs = rhs;
}
CompoundExp::~CompoundExp() {
delete lhs;
delete rhs;
}
double CompoundExp::eval(Spreadsheet& model) {
if (!KNOWN_OPERATORS.contains(op)) {
error("Illegal operator in expression: " + op);
}
double right = rhs->eval(model);
double left = lhs->eval(model);
double result = 0.0;
if (op == "+") {
result = left + right;
} else if (op == "-") {
result = left - right;
} else if (op == "*") {
result = left * right;
} else if (op == "/") {
result = left / right; // divide by 0.0 gives +/- INF
}
setValue(result);
return result;
}
const Expression* CompoundExp::getLeft() const {
return lhs;
}
std::string CompoundExp::getOperator() const {
return op;
}
const Expression* CompoundExp::getRight() const {
return rhs;
}
ExpressionType CompoundExp::getType() const {
return COMPOUND;
}
std::string CompoundExp::toString() const {
return '(' + lhs->toString() + ' ' + op + ' ' + rhs->toString() + ')';
}
/**
* Implementation notes: DoubleExp
* -------------------------------
* The DoubleExp subclass represents a numeric constant.
* The eval method simply returns that value.
*/
DoubleExp::DoubleExp(double value) {
setValue(value);
}
double DoubleExp::eval(Spreadsheet& /* model */) {
return getValue();
}
ExpressionType DoubleExp::getType() const {
return DOUBLE;
}
std::string DoubleExp::toString() const {
return realToString(value);
}
/**
* Implementation notes: IdentifierExp
* -----------------------------------
* The IdentifierExp subclass represents a variable name.
* The implementation of eval looks up that name in the evaluation context
* using your spreadsheet to get its value.
*/
IdentifierExp::IdentifierExp(const std::string& name) {
this->name = name;
}
double IdentifierExp::eval(Spreadsheet& model) {
if (!Range::isValidName(name)) {
error(name + " is not valid cell name.");
}
double result = model.getCellCalculatedValue(name);
setValue(result);
return result;
}
ExpressionType IdentifierExp::getType() const {
return IDENTIFIER;
}
std::string IdentifierExp::toString() const {
return name;
}
/**
* Implementation notes: RangeExp
* ------------------------------
* None.
*/
RangeExp::RangeExp(const std::string& function, Range cells) {
this->function = toUpperCase(trim(function));
this->cells = cells;
}
double RangeExp::eval(Spreadsheet& model) {
if (!Range::isKnownFunctionName(this->function)) {
error("Unknown function name: " + function);
}
Vector<double> valuesInRange;
model.fillFromRange(cells, valuesInRange);
double result = 0.0;
if (function == "AVERAGE" || function == "MEAN") {
result = average(valuesInRange);
} else if (function == "SUM") {
result = sum(valuesInRange);
} else if (function == "PRODUCT") {
result = product(valuesInRange);
} else if (function == "MAX") {
result = max(valuesInRange);
} else if (function == "MIN") {
result = min(valuesInRange);
} else if (function == "MEDIAN") {
result = median(valuesInRange);
} else if (function == "STDEV") {
result = stdev(valuesInRange);
} else {
error("Unknown function name: " + function);
}
setValue(result);
return result;
}
std::string RangeExp::getFunction() const{
return function;
}
Range RangeExp::getRange() const{
return cells;
}
ExpressionType RangeExp::getType() const {
return RANGE;
}
std::string RangeExp::toString() const {
return function + "(" + cells.toString() + ")";
}
/**
* Implementation notes: TextStringExp
* -----------------------------------
* The IdentifierExp subclass represents a text string constant. The
* implementation of eval simply returns 0.0.
*/
TextStringExp::TextStringExp(const std::string& str) {
this->str = str;
setValue(0.0);
}
double TextStringExp::eval(Spreadsheet& /* model */) {
return 0.0;
}
std::string TextStringExp::toString() const {
return str;
}
ExpressionType TextStringExp::getType() const {
return TEXTSTRING;
}
/**
* Implementation notes: EvaluationContext
* ---------------------------------------
* The methods in the EvaluationContext class simply call the appropriate
* method on the map used to represent the symbol table.
*/
void EvaluationContext::setValue(const std::string& var, double value) {
symbolTable.put(var, value);
}
double EvaluationContext::getValue(const std::string& var) const {
return symbolTable.get(var);
}
bool EvaluationContext::isDefined(const std::string& var) const {
return symbolTable.containsKey(var);
}
std::ostream& operator <<(std::ostream& out, const Expression& expr) {
out << expr.toString();
return out;
}