-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rational.cc
64 lines (51 loc) · 1.36 KB
/
Rational.cc
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
#ifndef RATIONAL_CC
#define RATIONAL_CC
#include "Algebra.cc"
// BEGIN
// Rational struct. Uses lcm to keep in simplified form.
// Simply replace "double" (or "int") with "rat" and use
// contructor to initialize constants. Rat class handles
// everything else.
//
// Written in such a way as to avoid overflow if possible.
struct rat {
T n, d;
rat(T n, T d) {
T k = gcd(n, d);
this->n = n/k;
this->d = d/k;
}
rat(T n) : n(n), d(1) {}
};
rat operator + (const rat &a, const rat &b) {
T new_d = lcm(a.d, b.d); // if overflow occurs, this may be 0
// causing floating point exception
T a_scale = new_d / a.d;
T b_scale = new_d / b.d;
return rat(a.n*a_scale + b.n*b_scale, new_d);
}
rat operator * (const T s, const rat &a) {
return rat(a.n * s, a.d);
}
rat operator * (const rat &a, const T s) {
return s*a;
}
rat operator - (const rat &a, const rat &b) {
return a + (-1 * b);
}
rat operator * (const rat &a, const rat &b) {
return rat(a.n*b.n, a.d*b.d);
}
rat operator / (const rat &a, const rat &b) {
return a * rat(b.d, b.n);
}
// END
#ifdef BUILD_TEST_RATIONAL
int main() {
//TODO: implement tests
//This code has successfully been used and accepted by UVa Online Judge
//Dice Throwing
return 0;
}
#endif // BUILD_TEST_RATIONAL
#endif // RATIONAL_CC