Advertisement
Alaricy

исключение при инициализации

Dec 14th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <numeric>
  3. #include <stdexcept>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Rational {
  9. public:
  10.     Rational() = default;
  11.  
  12.     Rational(int value)
  13.         : numerator_(value)
  14.         , denominator_(1) {
  15.     }
  16.  
  17.     Rational(int numerator, int denominator)
  18.         : numerator_(numerator)
  19.         , denominator_(denominator) {
  20.         if (denominator==0) {
  21.             throw runtime_error("Denominator is zero");
  22.         }
  23.         Normalize();
  24.     }
  25.  
  26.     int Numerator() const {
  27.         return numerator_;
  28.     }
  29.  
  30.     int Denominator() const {
  31.         return denominator_;
  32.     }
  33.  
  34.     Rational& operator+=(Rational right) {
  35.         numerator_ = numerator_ * right.denominator_ + right.numerator_ * denominator_;
  36.         denominator_ *= right.denominator_;
  37.         Normalize();
  38.         return *this;
  39.     }
  40.  
  41.     Rational& operator-=(Rational right) {
  42.         numerator_ = numerator_ * right.denominator_ - right.numerator_ * denominator_;
  43.         denominator_ *= right.denominator_;
  44.         Normalize();
  45.         return *this;
  46.     }
  47.  
  48.     Rational& operator*=(Rational right) {
  49.         numerator_ *= right.numerator_;
  50.         denominator_ *= right.denominator_;
  51.         Normalize();
  52.         return *this;
  53.     }
  54.  
  55.     Rational& operator/=(Rational right) {
  56.         if (right.numerator_ == 0) {
  57.             throw invalid_argument("Division by zero");
  58.         }
  59.         numerator_ *= right.denominator_;
  60.         denominator_ *= right.numerator_;
  61.         Normalize();
  62.         return *this;
  63.     }
  64.  
  65. private:
  66.     void Normalize() {
  67.         if (denominator_ < 0) {
  68.             numerator_ = -numerator_;
  69.             denominator_ = -denominator_;
  70.         }
  71.         int n = gcd(numerator_, denominator_);
  72.         numerator_ /= n;
  73.         denominator_ /= n;
  74.     }
  75.  
  76.     int numerator_ = 0;
  77.     int denominator_ = 1;
  78. };
  79.  
  80. ostream& operator<<(ostream& output, Rational rational) {
  81.     return output << rational.Numerator() << '/' << rational.Denominator();
  82. }
  83.  
  84. istream& operator>>(istream& input, Rational& rational) {
  85.     int numerator;
  86.     int denominator;
  87.     char slash;
  88.     if ((input >> numerator) && (input >> slash) && (slash == '/') && (input >> denominator)) {
  89.         rational = Rational{numerator, denominator};
  90.     }
  91.     return input;
  92. }
  93.  
  94. Rational operator+(Rational value) {
  95.     return value;
  96. }
  97.  
  98. Rational operator-(Rational value) {
  99.     return {-value.Numerator(), value.Denominator()};
  100. }
  101.  
  102. Rational operator+(Rational left, Rational right) {
  103.     return left += right;
  104. }
  105.  
  106. Rational operator-(Rational left, Rational right) {
  107.     return left -= right;
  108. }
  109.  
  110. Rational operator*(Rational left, Rational right) {
  111.     return left *= right;
  112. }
  113.  
  114. Rational operator/(Rational left, Rational right) {
  115.     return left /= right;
  116. }
  117.  
  118. bool operator==(Rational left, Rational right) {
  119.     return left.Numerator() == right.Numerator() && left.Denominator() == right.Denominator();
  120. }
  121.  
  122. bool operator!=(Rational left, Rational right) {
  123.     return !(left == right);
  124. }
  125.  
  126. bool operator<(Rational left, Rational right) {
  127.     return left.Numerator() * right.Denominator() < left.Denominator() * right.Numerator();
  128. }
  129.  
  130. bool operator>(Rational left, Rational right) {
  131.     return left.Numerator() * right.Denominator() > left.Denominator() * right.Numerator();
  132. }
  133.  
  134. bool operator>=(Rational left, Rational right) {
  135.     return !(left < right);
  136. }
  137.  
  138. bool operator<=(Rational left, Rational right) {
  139.     return !(left > right);
  140. }
  141.  
  142. // ========== для примера ========
  143.  
  144. int main() {
  145.     try {
  146.         // При попытке сконструировать дробь с нулевым знаменателем
  147.         // должно выброситься исключение domain_error
  148.         const Rational invalid_value{1, 0};
  149.         // Следующая строка не должна выполниться
  150.         cout << invalid_value << endl;
  151.     } catch (const domain_error& e) {
  152.         cout << "Ошибка: "s << e.what() << endl;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement