-
Notifications
You must be signed in to change notification settings - Fork 1
/
10.source.cpp
163 lines (120 loc) · 3.09 KB
/
10.source.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
#include <cmath>
#include <iostream>
const float E = 2.7f;
struct Set
{
virtual bool check(float) const = 0;
virtual Set* clone() const = 0;
virtual ~Set() {}
};
struct Intersect : Set
{
const Set *a, *b;
Intersect(const Set* a, const Set* b) : a(a), b(b) {}
bool check(float x) const override { return a->check(x) && b->check(x); }
Intersect* clone() const override { return new Intersect(*this); }
virtual ~Intersect() {}
};
struct AllReals : Set
{
bool check(float) const override { return true; }
AllReals* clone() const override { return new AllReals(); }
};
struct Positives : Set
{
bool check(float x) const override { return x > 0; }
Positives* clone() const override { return new Positives(); }
};
// @TODO Define the sets of positive, nonnegative numbers and sets of intervals
// of the form (a, b)
class RealFunc
{
protected:
std::string name;
virtual float evalAt(float) = 0;
public:
const Set* const domain;
RealFunc(Set const& domain, std::string name)
: name(name), domain(domain.clone())
{
}
// staticly bound
float safeEval(float x)
{
if (!domain->check(x)) {
std::cerr << "Invalid argument " << x << " was given to " << name
<< "." << std::endl;
throw;
}
// dynamic dispatch
return evalAt(x);
}
virtual ~RealFunc() { delete domain; };
};
struct Exponent : RealFunc
{
Exponent() : RealFunc{AllReals{}, "exp"} {}
virtual float evalAt(float x) override { return std::pow(E, x); }
};
struct Log : RealFunc
{
Log() : RealFunc{Positives{}, "log"} {}
float evalAt(float x) override { return std::log(x); }
};
struct BoyanFunction : RealFunc
{
int n;
int* myBigArr = new int[100]; // Illustration of the need of virtual d-tors
float evalAt(float x) override
{
n++;
if (n >= 10) return 0;
return x;
}
// Unless virtual, will not be called when delete-ing through a RealFunc ptr
virtual ~BoyanFunction() override { delete[] myBigArr; }
};
struct Compose : RealFunc
{
RealFunc *f, *g;
Compose(RealFunc* f, RealFunc* g)
: RealFunc{*g->domain, "Compose"}, f(f), g(g)
{
}
float evalAt(float x) override
{
return f->safeEval(g->safeEval(x));
}
};
struct Sin : RealFunc
{
Sin() : RealFunc{AllReals{}, "sin"} {}
float evalAt(float x) override { return std::sin(x); }
};
// @TODO Define `struct Tan` and carefully construct its domain Set.
// @TODO Define `struct Pow` which raises `x` to a power, specified as a
// constructor argument.
// @TODO Define `struct Sum` which represents the sum of all functions in a
// given array.
// @TODO Define global operators `*`, `+`, and `>>` which respectively act as
// multiplication, addition and composition of functions.
struct Sum : public RealFunc
{
RealFunc *f, *g;
Sum(RealFunc* f, RealFunc* g)
: RealFunc{Intersect{f->domain, g->domain}, "Sum"}, f(f), g(g)
{
}
float evalAt(float x) override { return f->safeEval(x) + g->safeEval(x); }
};
Sum operator+(RealFunc& a, RealFunc& b) { return {&a, &b}; }
int main()
{
Log log;
Sin sin;
Sum sum = log + sin;
Exponent exp;
Compose comp{&exp, &sum};
RealFunc* base = ∁
std::cout << base->safeEval(3) << std::endl;
}