SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <cmath> | |
3 | #include <type_traits> | |
4 | ||
5 | using namespace std; | |
6 | ||
7 | ||
8 | class X { | |
9 | public: | |
10 | double e( double v ) { | |
11 | return v; | |
12 | } | |
13 | ||
14 | double dx( double v ) { | |
15 | return 1; | |
16 | } | |
17 | }; | |
18 | ||
19 | X x; | |
20 | ||
21 | class Cte { | |
22 | public: | |
23 | Cte( double c ): c(c) {} | |
24 | ||
25 | double e( double v ) { | |
26 | return c; | |
27 | } | |
28 | ||
29 | double dx( double v ) { | |
30 | return 0; | |
31 | } | |
32 | ||
33 | private: | |
34 | double c; | |
35 | }; | |
36 | ||
37 | template <typename F1, typename F2> | |
38 | class Multiplica { | |
39 | public: | |
40 | Multiplica( F1 f1, F2 f2 ): f1(f1), f2(f2) {} | |
41 | ||
42 | double e( double v ) { | |
43 | return f1.e(v) * f2.e(v); | |
44 | } | |
45 | ||
46 | double dx( double v ) { | |
47 | return f1.e(v)*f2.dx(v) + f1.dx(v)*f2.e(v); | |
48 | } | |
49 | ||
50 | private: | |
51 | F1 f1; | |
52 | F2 f2; | |
53 | }; | |
54 | ||
55 | template <typename F1, typename F2> | |
56 | class Soma { | |
57 | public: | |
58 | Soma( F1 f1, F2 f2 ): f1(f1), f2(f2) {} | |
59 | ||
60 | double e( double v ) { | |
61 | return f1.e(v) + f2.e(v); | |
62 | } | |
63 | ||
64 | double dx( double v ) { | |
65 | return f1.dx(v)+f2.dx(v); | |
66 | } | |
67 | ||
68 | private: | |
69 | F1 f1; | |
70 | F2 f2; | |
71 | }; | |
72 | ||
73 | ||
74 | template <typename F1, typename F2> | |
75 | Multiplica<F1,F2> operator * ( F1 f1, F2 f2 ) { | |
76 | return Multiplica<F1,F2>( f1, f2 ); | |
77 | } | |
78 | ||
79 | template <typename F2> | |
80 | Multiplica<Cte,F2> operator * ( double n, F2 f2 ) { | |
81 | return Multiplica<Cte,F2>( n, f2 ); | |
82 | } | |
83 | ||
84 | template <typename F1> | |
85 | Multiplica<F1,Cte> operator * ( F1 f1, double n ) { | |
86 | return Multiplica<F1,Cte>( f1, n ); | |
87 | } | |
88 | ||
89 | ||
90 | template <typename F1, typename F2> | |
91 | Soma<F1,F2> operator + ( F1 f1, F2 f2 ) { | |
92 | return Soma<F1,F2>( f1, f2 ); | |
93 | } | |
94 | ||
95 | template <typename F2> | |
96 | Soma<Cte,F2> operator + ( double n, F2 f2 ) { | |
97 | return Soma<Cte,F2>( n, f2 ); | |
98 | } | |
99 | ||
100 | template <typename F1> | |
101 | Soma<F1,Cte> operator + ( F1 f1, double n ) { | |
102 | return Soma<F1,Cte>( f1, n ); | |
103 | } | |
104 | ||
105 | ||
106 | ||
107 | int main () { | |
108 | ||
109 | //auto f = sin(x); | |
110 | ||
111 | auto f = 3.0 * x * x; | |
112 | double v = 7; | |
113 | ||
114 | cout << "f(" << v << ")=" << f.e( v ) << ", f'(" << v << ")=" << f.dx( v ) << endl; | |
115 | ||
116 | return 0; | |
117 | } |