fraction.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <iostream> | ||
| 4 | #include <numeric> | ||
| 5 | |||
| 6 | template <typename T, typename MulT=T> struct fraction_t { | ||
| 7 | T numer = 0, denom = 1; | ||
| 8 | |||
| 9 | ✗ | fraction_t() : numer(0), denom(1) {} | |
| 10 | ✗ | fraction_t(T v) : numer(v), denom(1) {} | |
| 11 | ✗ | fraction_t(T n, T d) : numer(n), denom(d) { | |
| 12 | ✗ | if (denom < 0 || (denom == 0 && numer < 0)) { | |
| 13 | ✗ | numer = -numer; | |
| 14 | ✗ | denom = -denom; | |
| 15 | } | ||
| 16 | } | ||
| 17 | ✗ | template <typename U, typename V> explicit fraction_t(const fraction_t<U, V> o) : numer(T(o.numer)), denom(T(o.denom)) {} | |
| 18 | |||
| 19 | ✗ | friend std::ostream& operator << (std::ostream& o, const fraction_t& f) { | |
| 20 | ✗ | return o << f.numer << '/' << f.denom; | |
| 21 | } | ||
| 22 | ✗ | friend std::istream& operator >> (std::istream& i, const fraction_t& f) { | |
| 23 | ✗ | return i >> f.numer >> f.denom; | |
| 24 | } | ||
| 25 | |||
| 26 | ✗ | friend MulT cross(const fraction_t& a, const fraction_t& b) { | |
| 27 | ✗ | return MulT(a.numer) * MulT(b.denom) - MulT(b.numer) * MulT(a.denom); | |
| 28 | } | ||
| 29 | |||
| 30 | ✗ | friend bool operator == (const fraction_t& a, const fraction_t& b) { | |
| 31 | ✗ | return cross(a, b) == 0; | |
| 32 | } | ||
| 33 | ✗ | friend std::strong_ordering operator <=> (const fraction_t& a, const fraction_t& b) { | |
| 34 | ✗ | return cross(a, b) <=> 0; | |
| 35 | } | ||
| 36 | |||
| 37 | ✗ | fraction_t operator + () const { return fraction_t(+numer, denom); } | |
| 38 | ✗ | fraction_t operator - () const { return fraction_t(-numer, denom); } | |
| 39 | |||
| 40 | ✗ | fraction_t& operator *= (const fraction_t& o) { | |
| 41 | ✗ | numer *= o.numer; | |
| 42 | ✗ | denom *= o.denom; | |
| 43 | ✗ | return *this; | |
| 44 | } | ||
| 45 | ✗ | fraction_t& operator /= (const fraction_t& o) { | |
| 46 | ✗ | numer *= o.denom; | |
| 47 | ✗ | denom *= o.numer; | |
| 48 | ✗ | return *this; | |
| 49 | } | ||
| 50 | ✗ | friend fraction_t operator * (const fraction_t& a, const fraction_t& b) { | |
| 51 | ✗ | return fraction_t(a.numer * b.numer, a.denom * b.denom); | |
| 52 | } | ||
| 53 | ✗ | friend fraction_t operator / (const fraction_t& a, const fraction_t& b) { | |
| 54 | ✗ | return fraction_t(a.numer * b.denom, a.denom * b.numer); | |
| 55 | } | ||
| 56 | |||
| 57 | ✗ | friend fraction_t operator + (const fraction_t& a, const fraction_t& b) { | |
| 58 | ✗ | return {a.numer * b.denom + b.numer * a.denom, a.denom * b.denom}; | |
| 59 | } | ||
| 60 | ✗ | friend fraction_t operator - (const fraction_t& a, const fraction_t& b) { | |
| 61 | ✗ | return {a.numer * b.denom - b.numer * a.denom, a.denom * b.denom}; | |
| 62 | } | ||
| 63 | ✗ | fraction_t& operator += (const fraction_t& o) { return *this = *this + o; } | |
| 64 | ✗ | fraction_t& operator -= (const fraction_t& o) { return *this = *this - o; } | |
| 65 | |||
| 66 | ✗ | fraction_t& reduce() { | |
| 67 | using std::gcd; | ||
| 68 | ✗ | T g = gcd(numer, denom); | |
| 69 | ✗ | numer /= g; | |
| 70 | ✗ | denom /= g; | |
| 71 | ✗ | return *this; | |
| 72 | } | ||
| 73 | }; | ||
| 74 |