SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | #include <numeric> | |
| 3 | ||
| 4 | using namespace std; | |
| 5 | ||
| 6 | class Rational {
| |
| 7 | public: | |
| 8 | Rational() = default; | |
| 9 | ||
| 10 | Rational(int value) | |
| 11 | : numerator_(value) | |
| 12 | , denominator_(1) {
| |
| 13 | } | |
| 14 | ||
| 15 | Rational(int numerator, int denominator) | |
| 16 | : numerator_(numerator) | |
| 17 | , denominator_(denominator) | |
| 18 | {
| |
| 19 | Normalize(); | |
| 20 | } | |
| 21 | ||
| 22 | int Numerator() const {
| |
| 23 | return numerator_; | |
| 24 | } | |
| 25 | ||
| 26 | int Denominator() const {
| |
| 27 | return denominator_; | |
| 28 | } | |
| 29 | ||
| 30 | Rational& operator+=(Rational right) {
| |
| 31 | numerator_ = numerator_ * right.denominator_ + right.numerator_ * denominator_; | |
| 32 | denominator_ *= right.denominator_; | |
| 33 | Normalize(); | |
| 34 | return *this; | |
| 35 | } | |
| 36 | ||
| 37 | Rational& operator-=(Rational right) {
| |
| 38 | numerator_ = numerator_ * right.denominator_ - right.numerator_ * denominator_; | |
| 39 | denominator_ *= right.denominator_; | |
| 40 | Normalize(); | |
| 41 | return *this; | |
| 42 | } | |
| 43 | ||
| 44 | Rational& operator*=(Rational right) {
| |
| 45 | numerator_ *= right.numerator_; | |
| 46 | denominator_ *= right.denominator_; | |
| 47 | Normalize(); | |
| 48 | return *this; | |
| 49 | } | |
| 50 | ||
| 51 | Rational& operator/=(Rational right) {
| |
| 52 | numerator_ *= right.denominator_; | |
| 53 | denominator_ *= right.numerator_; | |
| 54 | Normalize(); | |
| 55 | return *this; | |
| 56 | } | |
| 57 | ||
| 58 | ||
| 59 | private: | |
| 60 | void Normalize() {
| |
| 61 | if (denominator_ < 0) {
| |
| 62 | numerator_ = -numerator_; | |
| 63 | denominator_ = -denominator_; | |
| 64 | } | |
| 65 | int n = gcd(numerator_, denominator_); | |
| 66 | numerator_ /= n; | |
| 67 | denominator_ /= n; | |
| 68 | } | |
| 69 | ||
| 70 | int numerator_ = 0; | |
| 71 | int denominator_ = 1; | |
| 72 | }; | |
| 73 | ||
| 74 | ostream& operator<<(ostream& output, Rational rational) {
| |
| 75 | return output << rational.Numerator() << '/' << rational.Denominator(); | |
| 76 | } | |
| 77 | ||
| 78 | istream& operator>>(istream& input, Rational& rational) {
| |
| 79 | int numerator; | |
| 80 | int denominator; | |
| 81 | char slash; | |
| 82 | if ((input >> numerator) && (input >> slash) && (slash == '/') && (input >> denominator)) {
| |
| 83 | rational = Rational{numerator, denominator};
| |
| 84 | } | |
| 85 | return input; | |
| 86 | } | |
| 87 | ||
| 88 | Rational operator+(Rational value) {
| |
| 89 | return value; | |
| 90 | } | |
| 91 | ||
| 92 | Rational operator-(Rational value) {
| |
| 93 | return {-value.Numerator(), value.Denominator()};
| |
| 94 | } | |
| 95 | ||
| 96 | Rational operator+(Rational left, Rational right) {
| |
| 97 | return left += right; | |
| 98 | } | |
| 99 | ||
| 100 | Rational operator-(Rational left, Rational right) {
| |
| 101 | return left -= right; | |
| 102 | } | |
| 103 | ||
| 104 | Rational operator*(Rational left, Rational right) {
| |
| 105 | return left *= right; | |
| 106 | } | |
| 107 | ||
| 108 | Rational operator/(Rational left, Rational right) {
| |
| 109 | return left /= right; | |
| 110 | } | |
| 111 | ||
| 112 | bool operator==(Rational left, Rational right) {
| |
| 113 | return left.Numerator() == right.Numerator() && left.Denominator() == right.Denominator(); | |
| 114 | } | |
| 115 | ||
| 116 | bool operator!=(Rational left, Rational right) {
| |
| 117 | return !(left == right); | |
| 118 | } | |
| 119 | ||
| 120 | bool operator<(Rational left, Rational right) {
| |
| 121 | return left.Numerator() * right.Denominator() < left.Denominator() * right.Numerator(); | |
| 122 | } | |
| 123 | ||
| 124 | bool operator>(Rational left, Rational right) {
| |
| 125 | return !(left < right) && left!=right; | |
| 126 | } | |
| 127 | ||
| 128 | bool operator<=(Rational left, Rational right) {
| |
| 129 | return left<right || left == right; | |
| 130 | } | |
| 131 | ||
| 132 | bool operator>=(Rational left, Rational right) {
| |
| 133 | return left>right || left == right; | |
| 134 | } |