-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path09.source.cpp
103 lines (81 loc) · 2.19 KB
/
09.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
#include <cmath>
#include <iostream>
const float E = 2.7;
// Global @TODO: Add const wherever applicable
// @TODO Convert to struct
float exponent(float x) { return std::pow(E, x); }
struct Set
{
bool (*check)(float);
};
// @TODO Define the sets of positive, nonnegative numbers and sets of intervals
// of the form (a, b)
struct RealFunc
{
// @TODO Declare `virtual RealFunc differentiate() = 0;` and implement in
// all subclasses
virtual float evalAt(float) = 0;
// @NOTE for me about next time:
// * make `evalAt()` protected
// * add a protected `Set domain` member
// * create a public non-virtual `float operator()(float)` that:
// * checks if x \in domain
// * throws an error if not
// * calls the virtual evalAt(x) otherwise
};
struct Log : RealFunc
{
float evalAt(float x) override
{
if (x <= 0) {
std::cerr << "Log expects positive numbers!\n"
<< x << " was given.";
throw;
}
return std::log(x);
}
// @TODO Define a const member `name`; initialize properly in all subclasses
// @TODO Add a non-virtual (why?) method `string print(string)` which
// returns the name of the functions and the passed string in braces.
// An exmaple print result could be `sin(log(x))` or `e^(x)`
};
struct BoyanFunction : RealFunc
{
int n;
float evalAt(float x) override
{
n++;
if (n >= 10) return 0;
return x;
}
};
struct Compose : RealFunc
{
RealFunc *f, *g;
// Set domain;
Compose(RealFunc* f, RealFunc* g) : f(f), g(g) {}
float evalAt(float x) override
{
// @TODO check if in domain: if (g->domain(x) && f->domain(g(x)))
return f->evalAt(g->evalAt(x));
}
};
struct Sin : RealFunc
{
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.
int main()
{
Log log;
Sin sin;
Compose f{&sin, &log};
Compose g{&log, &f};
std::cout << g.evalAt(4) << std::endl;
}