| 1 | template <class T> struct fraction { |
| 2 | T gcd(T a, T b) { return b == T(0) ? a : gcd(b, a % b); } |
| 3 | T n, d; |
| 4 | fraction(T n_=T(0), T d_=T(1)) { |
| 5 | assert(d_ != 0); |
| 6 | n = n_, d = d_; |
| 7 | if (d < T(0)) n = -n, d = -d; |
| 8 | T g = gcd(abs(n), abs(d)); |
| 9 | n /= g, d /= g; } |
| 10 | fraction(const fraction<T>& other) |
| 11 | : n(other.n), d(other.d) { } |
| 12 | fraction<T> operator +(const fraction<T>& other) const { |
| 13 | return fraction<T>(n * other.d + other.n * d, |
| 14 | d * other.d);} |
| 15 | fraction<T> operator -(const fraction<T>& other) const { |
| 16 | return fraction<T>(n * other.d - other.n * d, |
| 17 | d * other.d); } |
| 18 | fraction<T> operator *(const fraction<T>& other) const { |
| 19 | return fraction<T>(n * other.n, d * other.d); } |
| 20 | fraction<T> operator /(const fraction<T>& other) const { |
| 21 | return fraction<T>(n * other.d, d * other.n); } |
| 22 | bool operator <(const fraction<T>& other) const { |
| 23 | return n * other.d < other.n * d; } |
| 24 | bool operator <=(const fraction<T>& other) const { |
| 25 | return !(other < *this); } |
| 26 | bool operator >(const fraction<T>& other) const { |
| 27 | return other < *this; } |
| 28 | bool operator >=(const fraction<T>& other) const { |
| 29 | return !(*this < other); } |
| 30 | bool operator ==(const fraction<T>& other) const { |
| 31 | return n == other.n && d == other.d; } |
| 32 | bool operator !=(const fraction<T>& other) const { |
| 33 | return !(*this == other); } }; |
| 34 | // vim: cc=60 ts=2 sts=2 sw=2: |
nothing calls this directly
no outgoing calls
no test coverage detected