-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblatt05_entwurf_compat.cpp
309 lines (269 loc) · 12.7 KB
/
blatt05_entwurf_compat.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <iostream>
#include <iomanip>
#include <limits>
#include <stdexcept>
#include <vector>
namespace checked_int
{
template <typename T>
class checked
{
T value;
public:
checked(T value = T()) : value(value) { }
operator T() const { return value; }
checked operator+() const { return *this; }
checked operator-() const
{
if (std::numeric_limits<T>::is_signed && std::numeric_limits<T>::min() + std::numeric_limits<T>::max() < T(0))
if (value == std::numeric_limits<T>::min())
{
throw std::overflow_error("operator-: arithmetic overflow on negation");
}
return checked(-value);
}
template<typename S> checked& operator+=(S rhs) { value = *this + rhs; return *this; }
template<typename S> checked& operator-=(S rhs) { value = *this - rhs; return *this; }
template<typename S> checked& operator*=(S rhs) { value = *this * rhs; return *this; }
template<typename S> checked& operator/=(S rhs) { value = *this / rhs; return *this; }
template<typename S> checked& operator%=(S rhs) { value = *this % rhs; return *this; }
template<typename T1, typename T2>
friend auto operator+(checked<T1> lhs, checked<T2> rhs) -> checked<decltype(lhs.value + rhs.value)>
{
using Common = decltype(lhs.value + rhs.value);
if (rhs.value > T2(0) && std::numeric_limits<Common>::max() - rhs.value < lhs.value
|| rhs.value < T2(0) && std::numeric_limits<Common>::min() - rhs.value > lhs.value)
{
throw std::overflow_error("operator+: arithmetic overflow");
}
return checked<Common>(lhs.value + rhs.value);
}
template<typename T1, typename T2>
friend auto operator-(checked<T1> lhs, checked<T2> rhs) -> checked<decltype(lhs.value - rhs.value)>
{
using Common = decltype(lhs.value - rhs.value);
if (rhs.value < T2(0) && std::numeric_limits<Common>::max() + rhs.value < lhs.value
|| rhs.value > T2(0) && std::numeric_limits<Common>::min() + rhs.value > lhs.value)
{
throw std::overflow_error("operator-: arithmetic overflow");
}
return checked<Common>(lhs.value - rhs.value);
}
template<typename T1, typename T2>
friend auto operator*(checked<T1> lhs, checked<T2> rhs) -> checked<decltype(lhs.value * rhs.value)>
{
using Common = decltype(lhs.value * rhs.value);
if (std::numeric_limits<Common>::is_signed && std::numeric_limits<Common>::min() + std::numeric_limits<Common>::max() < Common(0) && (
lhs.value == T1(-1) && rhs.value == std::numeric_limits<Common>::min()
|| rhs.value == T2(-1) && lhs.value == std::numeric_limits<Common>::min()))
{
throw std::overflow_error("operator*: arithmetic overflow on negation");
}
if (rhs < 0 && lhs < 0)
{
rhs = -rhs;
lhs = -lhs;
}
// one of rhs and lhs >= 0; for == 0, nothing to check; for < 0, the other must be >= 0, so if > 0, divide max by it.
if (rhs > 0 && (
lhs.value > std::numeric_limits<Common>::max() / rhs.value
|| lhs.value < std::numeric_limits<Common>::min() / rhs.value)
|| lhs > 0 && (
rhs.value > std::numeric_limits<Common>::max() / lhs.value
|| rhs.value < std::numeric_limits<Common>::min() / lhs.value)
)
{
throw std::overflow_error("operator*: arithmetic overflow");
}
return checked<Common>(lhs.value * rhs.value);
}
template<typename T1, typename T2>
friend auto operator/(checked<T1> lhs, checked<T2> rhs) -> checked<decltype(lhs.value / rhs.value)>
{
if (rhs == T2(0)) throw std::domain_error("operator/: division by zero");
using Common = decltype(lhs.value / rhs.value);
if (std::numeric_limits<Common>::is_signed && std::numeric_limits<Common>::min() + std::numeric_limits<Common>::max() < Common(0) &&
std::numeric_limits<T2>::is_signed && rhs == T2(-1) && lhs.value == std::numeric_limits<Common>::min())
{
throw std::overflow_error("operator/: arithmetic overflow on negation");
}
return checked<Common>(lhs.value / rhs.value);
}
template<typename T1, typename T2>
friend auto operator%(checked<T1> lhs, checked<T2> rhs) -> checked<decltype(lhs.value % rhs.value)>
{
if (rhs == T2(0)) throw std::domain_error("operator%: division by zero");
using Common = decltype(lhs.value % rhs.value);
return checked<Common>(lhs.value % rhs.value);
}
friend std::ostream& operator<<(std::ostream& stream, checked c) { return stream << c.value; }
friend std::istream& operator>>(std::istream& stream, checked & c) { return stream >> c.value; }
};
template<typename T1, typename T2> bool operator==(checked<T1> lhs, checked<T2> rhs) { return static_cast<T1>(lhs) == static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator==(checked<T1> lhs, T2 rhs) { return static_cast<T1>(lhs) == rhs ; }
template<typename T1, typename T2> bool operator==( T1 lhs, checked<T2> rhs) { return lhs == static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator!=(checked<T1> lhs, checked<T2> rhs) { return static_cast<T1>(lhs) != static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator!=(checked<T1> lhs, T2 rhs) { return static_cast<T1>(lhs) != rhs ; }
template<typename T1, typename T2> bool operator!=( T1 lhs, checked<T2> rhs) { return lhs != static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator< (checked<T1> lhs, checked<T2> rhs) { return static_cast<T1>(lhs) < static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator< (checked<T1> lhs, T2 rhs) { return static_cast<T1>(lhs) < rhs ; }
template<typename T1, typename T2> bool operator< ( T1 lhs, checked<T2> rhs) { return lhs < static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator> (checked<T1> lhs, checked<T2> rhs) { return static_cast<T1>(lhs) > static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator> (checked<T1> lhs, T2 rhs) { return static_cast<T1>(lhs) > rhs ; }
template<typename T1, typename T2> bool operator> ( T1 lhs, checked<T2> rhs) { return lhs > static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator<=(checked<T1> lhs, checked<T2> rhs) { return static_cast<T1>(lhs) <= static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator<=(checked<T1> lhs, T2 rhs) { return static_cast<T1>(lhs) <= rhs ; }
template<typename T1, typename T2> bool operator<=( T1 lhs, checked<T2> rhs) { return lhs <= static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator>=(checked<T1> lhs, checked<T2> rhs) { return static_cast<T1>(lhs) >= static_cast<T2>(rhs); }
template<typename T1, typename T2> bool operator>=(checked<T1> lhs, T2 rhs) { return static_cast<T1>(lhs) >= rhs ; }
template<typename T1, typename T2> bool operator>=( T1 lhs, checked<T2> rhs) { return lhs >= static_cast<T2>(rhs); }
template<typename T1, typename T2> auto operator+( T1 lhs, checked<T2> rhs) { return checked<T1>(lhs) + rhs ; }
template<typename T1, typename T2> auto operator+(checked<T1> lhs, T2 rhs) { return lhs + checked<T2>(rhs); }
template<typename T1, typename T2> auto operator-( T1 lhs, checked<T2> rhs) { return checked<T1>(lhs) - rhs ; }
template<typename T1, typename T2> auto operator-(checked<T1> lhs, T2 rhs) { return lhs - checked<T2>(rhs); }
template<typename T1, typename T2> auto operator*( T1 lhs, checked<T2> rhs) { return checked<T1>(lhs) * rhs ; }
template<typename T1, typename T2> auto operator*(checked<T1> lhs, T2 rhs) { return lhs * checked<T2>(rhs); }
template<typename T1, typename T2> auto operator/( T1 lhs, checked<T2> rhs) { return checked<T1>(lhs) / rhs ; }
template<typename T1, typename T2> auto operator/(checked<T1> lhs, T2 rhs) { return lhs / checked<T2>(rhs); }
template<typename T1, typename T2> auto operator%( T1 lhs, checked<T2> rhs) { return checked<T1>(lhs) % rhs ; }
template<typename T1, typename T2> auto operator%(checked<T1> lhs, T2 rhs) { return lhs % checked<T2>(rhs); }
template<typename T>
checked<T> check(T value) { return checked<T>(value); }
}
#define STRINGIZE_DETAIL(x) #x
#define STRINGIZE(x) STRINGIZE_DETAIL(x)
struct todo_missing : public std::logic_error { todo_missing(const char * p) : std::logic_error(p) { } };
#define TODO() throw todo_missing("something not implemented in line " STRINGIZE(__LINE__) ".")
// ***** BEGIN HERE *****
using namespace std;
using namespace checked_int;
class rational
{
private:
checked<long> p, q;
// greatest common divisor
static long gcd(long a, long b)
{
if (a < 0) TODO();
if (b < 0) TODO();
if (b == 0) TODO();
TODO(); // the main case
}
public:
rational(long z = 0, long n = 1)
: p(z), q(n)
{
if (q == 0) throw domain_error("denominator must not be zero");
if (q < 0)
{
TODO();
}
TODO();
}
explicit operator long double() const { TODO(); }
friend rational operator+(rational r) { TODO(); }
friend rational operator-(rational r) { TODO(); }
rational& operator+=(rational r) { return *this = *this + r; }
rational& operator-=(rational r) { return *this = *this - r; }
rational& operator*=(rational r) { return *this = *this * r; }
rational& operator/=(rational r) { return *this = *this / r; }
friend rational operator+(rational s, rational t)
{
TODO();
}
friend rational operator-(rational s, rational t)
{
TODO();
}
friend rational operator*(rational s, rational t)
{
TODO();
}
friend rational operator/(rational s, rational t)
{
if (t == 0) throw invalid_argument("operator/: division by zero");
TODO();
}
friend rational pow(rational b, int n)
{
if (n == 0) return 1;
if (b == 0) return b;
if (n < 0)
{
TODO();
}
TODO();
}
friend bool operator==(rational s, rational t) { return s.p == t.p && s.q == t.q; }
friend bool operator!=(rational s, rational t) { return !(s == t); }
friend bool operator> (rational s, rational t) { return t < s; }
friend bool operator<=(rational s, rational t) { return s == t || s < t; }
friend bool operator>=(rational s, rational t) { return s == t || s > t; }
friend bool operator< (rational s, rational t)
{
// trivial criteria
if (s.p <= 0 && 0 <= t.p) return s != t; // makes comparisons to 0 fast.
if (s.p <= t.p && s.q >= t.q) return s != t;
TODO();
}
friend ostream& operator<<(ostream& stream, rational const & r)
{
TODO();
}
friend istream& operator>>(istream& stream, rational & r)
{
TODO();
}
};
rational power_series(rational const & r, int n)
{
TODO();
}
rational continued_fraction(vector<long> const & a, vector<long> const & b)
{
if (b.size() == 0u) throw invalid_argument("continued_fraction: vector of coefficients must not be empty");
if (a.size() != b.size() - 1u) throw invalid_argument("continued_fraction: vector sizes mismatch");
TODO();
}
// version where the a[i] default to ones.
rational continued_fraction(vector<long> const & b)
{
if (b.size() == 0u) throw invalid_argument("continued_fraction: vector of coefficients must not be empty");
return continued_fraction(vector<long>(b.size() - 1, 1), b);
}
// for debugging vectors of int; prints [v0, v1, .., vn]
// comment in if you need it.
/*
ostream& operator<<(ostream& stream, vector<int> const & v)
{
stream << '[';
if (!v.empty())
{
stream << v[0];
for (vector<int>::size_type i = 1; i < v.size(); ++i)
stream << ", " << v[i];
}
stream << ']';
return stream;
}
*/
int main()
{
try
{
cout << "\n-- (a) and (b) ----------" << endl;
for (rational r : { rational(2, 3), rational(-10, 7) })
{
const int n = 8;
// TODO
}
cout << "\n-- (c) ------------------" << endl;
// TODO
cout << "\n-- (d) ------------------" << endl;
// TODO
}
catch (todo_missing const & e)
{
cerr << e.what() << endl;
}
}