Advertisement
Alaricy

перегрузка присвоение +=. сравнение и т.д.

Dec 8th, 2022 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  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. private:
  59.     void Normalize() {
  60.         if (denominator_ < 0) {
  61.             numerator_ = -numerator_;
  62.             denominator_ = -denominator_;
  63.         }
  64.         int n = gcd(numerator_, denominator_);
  65.         numerator_ /= n;
  66.         denominator_ /= n;
  67.     }
  68.  
  69.     int numerator_ = 0;
  70.     int denominator_ = 1;
  71. };
  72.  
  73. ostream& operator<<(ostream& output, Rational rational) {
  74.     return output << rational.Numerator() << '/' << rational.Denominator();
  75. }
  76.  
  77. istream& operator>>(istream& input, Rational& rational) {
  78.     int numerator;
  79.     int denominator;
  80.     char slash;
  81.     if ((input >> numerator) && (input >> slash) && (slash == '/') && (input >> denominator)) {
  82.         rational = Rational{numerator, denominator};
  83.     }
  84.     return input;
  85. }
  86.  
  87. Rational operator+(Rational value) {
  88.     return value;
  89. }
  90.  
  91. Rational operator-(Rational value) {
  92.     return {-value.Numerator(), value.Denominator()};
  93. }
  94.  
  95. Rational operator+(Rational left, Rational right) {
  96.     return left += right;
  97. }
  98.  
  99. Rational operator-(Rational left, Rational right) {
  100.     return left -= right;
  101. }
  102.  
  103. Rational operator*(Rational left, Rational right) {
  104.     return left *= right;
  105. }
  106.  
  107. Rational operator/(Rational left, Rational right) {
  108.     return left /= right;
  109. }
  110. bool operator==(Rational left, Rational right) {
  111.     return left.Numerator() == right.Numerator() &&
  112.            left.Denominator() == right.Denominator();
  113. }
  114. bool operator!=(Rational left, Rational right) {
  115.     return !(left == right);
  116. }
  117. bool operator>(Rational left, Rational right) {
  118.     return left.Numerator()*right.Denominator() > right.Numerator()*left.Denominator();    
  119. }
  120. bool operator<(Rational left, Rational right) {
  121.     return left.Numerator()*right.Denominator() < right.Numerator()*left.Denominator();    
  122. }
  123. bool operator>=(Rational left, Rational right) {
  124.     return left.Numerator()*right.Denominator() >= right.Numerator()*left.Denominator();    
  125. }
  126. bool operator<=(Rational left, Rational right) {
  127.     return left.Numerator()*right.Denominator() <= right.Numerator()*left.Denominator();    
  128. }
  129.  
  130.  
  131. int main() {
  132. Rational r1,r2,r;
  133.     cin >> r1 >> r2;
  134.     //r1-=r2;
  135.     cout << (r1==r2);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement