Advertisement
kasougi

Untitled

Dec 22nd, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 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& istr);
  42.    
  43.     static const char sep {'/'};
  44.  
  45.    
  46. private:
  47.    
  48.     int _num {0};
  49.     int _denum {1};
  50.    
  51.     void normalize();
  52.    
  53.     int gcd(int a, int b);
  54. };
  55.  
  56. std::istream operator>>(std::istream istream, Rational& rhs);
  57. std::ostream operator<<(std::ostream ostream, const Rational& rhs);
  58.  
  59.  
  60. Rational operator+(Rational& lhs, Rational& rhs);
  61. Rational operator-(Rational& lhs, Rational& rhs);
  62. Rational operator*(Rational& lhs, Rational& rhs);
  63. Rational operator/(Rational& lhs, Rational& rhs);
  64.  
  65. Rational operator+(Rational& lhs, int& rhs);
  66. Rational operator-(Rational& lhs, int& rhs);
  67. Rational operator*(Rational& lhs, int& rhs);
  68. Rational operator/(Rational& lhs, int& rhs);
  69.  
  70. Rational operator+(int& rhs, Rational& lhs);
  71. Rational operator-(int& rhs, Rational& lhs);
  72. Rational operator*(int& rhs, Rational& lhs);
  73. Rational operator/(int& rhs, Rational& lhs);
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement