Vserosbuybuy

Rational class, C++

Mar 19th, 2021 (edited)
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. class Rational {
  7. private:
  8.     int _n;
  9.     int _d;
  10.  
  11. public:
  12.     Rational(int n = 0, int d = 1) {
  13.         int tmp = _gcd(n, d) * d / abs(d);
  14.         _n = n / tmp;
  15.         _d = d / tmp;
  16.     }
  17.  
  18.     int numerator() const {
  19.         return _n;
  20.     }
  21.  
  22.     int denominator() const {
  23.         return _d;
  24.     }
  25.  
  26.     const Rational& operator+() const {
  27.         return *this;
  28.     }
  29.  
  30.     const Rational operator-() const {
  31.         return Rational(-_n, _d);
  32.     }
  33.  
  34.     Rational& operator++() {
  35.         *this += 1;
  36.         return *this;
  37.     }
  38.  
  39.     Rational operator++(int) {
  40.         Rational copy = *this;
  41.         *this += 1;
  42.         return copy;
  43.     }
  44.  
  45.     Rational& operator--() {
  46.         *this -= 1;
  47.         return *this;
  48.     }
  49.  
  50.     Rational operator--(int) {
  51.         Rational copy = *this;
  52.         *this -= 1;
  53.         return copy;
  54.     }
  55.  
  56.     friend Rational operator+(const Rational& a, const Rational& b);
  57.     friend Rational operator-(const Rational& a, const Rational& b);
  58.     friend Rational operator*(const Rational& a, const Rational& b);
  59.     friend Rational operator/(const Rational& a, const Rational& b);
  60.     friend Rational& operator+=(Rational& a, const Rational& b);
  61.     friend Rational& operator-=(Rational& a, const Rational& b);
  62.     friend Rational& operator*=(Rational& a, const Rational& b);
  63.     friend Rational& operator/=(Rational& a, const Rational& b);
  64.     friend bool operator==(const Rational& a, const Rational& b);
  65.     friend bool operator!=(const Rational& a, const Rational& b);
  66.  
  67. private:
  68.     int _gcd(int a, int b) {
  69.         a = abs(a);
  70.         b = abs(b);
  71.         return (a == 0) ? b : _gcd(b % a, a);
  72.     }
  73. };
  74.  
  75. Rational operator+(const Rational& a, const Rational& b) {
  76.     return Rational(a._n * b._d + b._n * a._d, a._d * b._d);
  77. }
  78.  
  79. Rational operator-(const Rational& a, const Rational& b) {
  80.     return Rational(a._n * b._d - b._n * a._d, a._d * b._d);
  81. }
  82.  
  83. Rational operator*(const Rational& a, const Rational& b) {
  84.     return Rational(a._n * b._n, a._d * b._d);
  85. }
  86.  
  87. Rational operator/(const Rational& a, const Rational& b) {
  88.     return Rational(a._n * b._d, a._d * b._n);
  89. }
  90.  
  91. Rational& operator+=(Rational& a, const Rational& b) {
  92.     a = a + b;
  93.     return a;
  94. }
  95.  
  96. Rational& operator-=(Rational& a, const Rational& b) {
  97.     a = a - b;
  98.     return a;
  99. }
  100.  
  101. Rational& operator*=(Rational& a, const Rational& b) {
  102.     a = a * b;
  103.     return a;
  104. }
  105.  
  106. Rational& operator/=(Rational& a, const Rational& b) {
  107.     a = a / b;
  108.     return a;
  109. }
  110.  
  111. bool operator==(const Rational& a, const Rational& b) {
  112.     return (a._n == b._n) & (a._d == b._d);
  113. }
  114.  
  115. bool operator!=(const Rational& a, const Rational& b) {
  116.     return !(a == b);
  117. }
Add Comment
Please, Sign In to add comment