-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.cpp
More file actions
172 lines (136 loc) · 3.65 KB
/
Expression.cpp
File metadata and controls
172 lines (136 loc) · 3.65 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
#include <string>
#include "Expression.h"
#include <iostream>
// Create a new Expression
Expression::Expression(){
}
// Dynamic memory is dealt with in derived class destructors
Expression::~Expression(){
}
// Virtual function
int Expression::evaluate() const {
return this->evaluate();
}
// Virtual function
std::string Expression::print() const {
return this->print();
}
// Virtual function
std::string Expression::type() const {
return this->type();
}
// Initialise the expressions that make up this BinaryExpression
BinaryExpression::BinaryExpression(Expression* subexp1, Expression* subexp2){
exp1 = subexp1;
exp2 = subexp2;
}
// Free dynamically allocated memory
BinaryExpression::~BinaryExpression(){
delete exp1;
delete exp2;
}
// Initialise the expression of this UnaryExpression
UnaryExpression::UnaryExpression(Expression* subexp){
exp = subexp;
}
// Free dynamically allocated memory
UnaryExpression::~UnaryExpression(){
delete exp;
}
// Create a Number which is an integer n
Number::Number(int n){
num = n;
}
// Same as default destructor
Number::~Number(){
}
// Override virtual function
int Number::evaluate() const {
return this->num;
}
// Override virtual function
std::string Number::print() const {
return std::to_string(this->num);
}
// Override virtual function
std::string Number::type() const{
return "Number";
}
// Create a new Multiplication Expression
// The two expressions making up this expression are init in creation of Binary Expression
Multiply::Multiply(Expression* subexp1, Expression* subexp2):BinaryExpression(subexp1, subexp2){
}
// Freeing of dynamic memory is dealt with in ~BinaryExpression()
Multiply::~Multiply(){
}
// Override virtual function
int Multiply::evaluate() const{
int result = (this->exp1->evaluate()) * (this->exp2->evaluate());
return result;
}
std::string Multiply::print() const {
return "(" + exp1->print() + "*" + exp2->print() + ")";
}
std::string Multiply::type() const{
return "Multiply";
}
// Create a new Addition Expression
// The two expressions making up this expression are init in creation of Binary Expression
Add::Add(Expression* subexp1, Expression* subexp2):BinaryExpression(subexp1, subexp2){
}
// Freeing of dynamic memory is dealt with in ~BinaryExpression()
Add::~Add(){
}
// Override virtual function
int Add::evaluate() const {
int result = (this->exp1->evaluate()) + (this->exp2->evaluate());
return result;
}
// Override virtual function
std::string Add::print() const {
return "(" + exp1->print() + "+" + exp2->print() + ")";
}
// Override virtual function
std::string Add::type() const {
return "Add";
}
// Create a new Absolute Expression
// The expression is init in creation of Unary Expression
Abs::Abs(Expression* subexp):UnaryExpression(subexp){
}
// Freeing of dynamic memory is done in ~UnaryExpression()
Abs::~Abs(){
}
// Override virtual function
int Abs::evaluate() const {
int result = abs(this->exp->evaluate());
return result;
}
// Override virtual function
std::string Abs::print() const {
return "Abs(" + exp->print() + ")";
}
// Override virtual function
std::string Abs::type() const {
return "Abs";
}
// Create a new Square Expression
// The expression is init in creation of Unary Expression
Square::Square(Expression* subexp):UnaryExpression(subexp){
}
// Freeing of dynamic memory is done in ~UnaryExpression()
Square::~Square(){
}
// Override virtual function
int Square::evaluate() const {
int result = (this->exp->evaluate()) * (this->exp->evaluate());
return result;
}
// Override virtual function
std::string Square::print() const {
return "Square(" + exp->print() + ")";
}
// Override virtual function
std::string Square::type() const {
return "Square";
}