Advertisement
kasougi

Untitled

Dec 22nd, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Rational {
  4.    
  5. public:
  6.    
  7.     Rational() = default;
  8.     Rational(const Rational&) = default;
  9.     Rational(const int num, const int denum = 1);
  10.    
  11.     Rational& operator=(const Rational&) = default;
  12.     ~Rational() = default;
  13.    
  14.     int num() const;
  15.     int denum() const;
  16.    
  17.     bool operator==(Rational& rhs) const;
  18.     bool operator!=(Rational& rhs) const;
  19.    
  20.     bool operator>(Rational& rhs) const;
  21.     bool operator>=(Rational& rhs) const;
  22.    
  23.     bool operator<(Rational& rhs) const;
  24.     bool operator<=(Rational& rhs) const;
  25.    
  26.    
  27.    
  28.     Rational& operator+=(const Rational& rhs);
  29.     Rational& operator+=(const int& rhs);
  30.    
  31.     Rational& operator-=(const Rational& rhs);
  32.     Rational& operator-=(const int& rhs);
  33.    
  34.     Rational& operator/=(const Rational& rhs);
  35.     Rational& operator/=(const int& rhs);
  36.    
  37.     Rational& operator*=(const Rational& rhs);
  38.     Rational& operator*=(const int& rhs);
  39.    
  40.     std::ostream& writeTo(std::ostream& ostr) const;
  41.     std::istream& readFrom(std::istream& ostr);
  42.  
  43.    
  44. private:
  45.    
  46.     int _num {0};
  47.     int _denum {1};
  48.    
  49.     void normalize();
  50.    
  51.     int gcd(int a, int b);
  52. };
  53.  
  54. std::istream operator>>(std::istream istream, Rational& rhs);
  55. std::ostream operator<<(std::ostream istream, Rational& rhs);
  56.  
  57.  
  58. Rational operator+(Rational& lhs, Rational& rhs);
  59. Rational operator-(Rational& lhs, Rational& rhs);
  60. Rational operator*(Rational& lhs, Rational& rhs);
  61. Rational operator/(Rational& lhs, Rational& rhs);
  62.  
  63. Rational operator+(Rational& lhs, int& rhs);
  64. Rational operator-(Rational& lhs, int& rhs);
  65. Rational operator*(Rational& lhs, int& rhs);
  66. Rational operator/(Rational& lhs, int& rhs);
  67.  
  68. Rational operator+(int& rhs, Rational& lhs);
  69. Rational operator-(int& rhs, Rational& lhs);
  70. Rational operator*(int& rhs, Rational& lhs);
  71. Rational operator/(int& rhs, Rational& lhs);
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement