-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.hpp
91 lines (84 loc) · 2.04 KB
/
expr.hpp
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
#ifndef __EXPR_HPP___
#define __EXPR_HPP___
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <string>
class Expression {
public:
Expression() {}
virtual std::string toString() const = 0;
virtual ~Expression() {}
};
class NumExpression : public Expression {
long number;
public:
NumExpression(long n) : number(n) {}
virtual std::string toString() const {
std::stringstream answer;
answer << number;
return answer.str();
}
virtual ~NumExpression() {}
};
class PlusExpression : public Expression {
Expression * l;
Expression * r;
public:
PlusExpression(Expression * lhs, Expression * rhs) : l(lhs), r(rhs) {}
virtual std::string toString() const {
std::stringstream answer;
answer << "(" << l->toString() << "+" << r->toString() << ")";
return answer.str();
}
virtual ~PlusExpression() {
delete l;
delete r;
}
};
class MinusExpression : public Expression {
Expression * l;
Expression * r;
public:
MinusExpression(Expression * lhs, Expression * rhs) : l(lhs), r(rhs) {}
virtual std::string toString() const {
std::stringstream answer;
answer << "(" << l->toString() << "-" << r->toString() << ")";
return answer.str();
}
virtual ~MinusExpression() {
delete l;
delete r;
}
};
class TimesExpression : public Expression {
Expression * l;
Expression * r;
public:
TimesExpression(Expression * lhs, Expression rhs) : l(lhs), r(rhs) {}
virtual std::string toString() const {
std::stringstream answer;
answer << "(" << l->toString() << "*" << r->toString() << ")";
return answer.str();
}
virtual ~TimesExpression() {
delete l;
delete r;
}
};
class DivExpression : public Expression {
Expression * l;
Expression * r;
public:
DivExpression(Expression * lhs, Expression * rhs) : l(lhs), r(rhs) {}
virtual std::string toString() const {
std::stringstream answer;
answer << "(" << l->toString() << " / " << r->toString() << ")";
return answer.str();
}
virtual ~DivExpression() {
delete l;
delete r;
}
};
#endif