35657

Untitled

Nov 18th, 2023
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class Fraction {
  7.  
  8. public:
  9.  
  10.     Fraction() : Fraction(1, 1) {}
  11.  
  12.     Fraction(int num, int denom) : numerator(num), denominator(denom) {}
  13.  
  14.     Fraction(const Fraction& fract) : numerator(fract.numerator), denominator(fract.denominator) {}
  15.  
  16.     Fraction& operator+=(const Fraction& other) {
  17.         numerator = numerator * other.denominator + denominator * other.numerator;
  18.         denominator *= other.denominator;
  19.         Reduction();
  20.         return *this;
  21.     }
  22.  
  23.     Fraction& operator-=(const Fraction& other) {
  24.         numerator = numerator * other.denominator - denominator * other.numerator;
  25.         denominator *= other.denominator;
  26.         Reduction();
  27.         return *this;
  28.     }
  29.  
  30.     Fraction& operator*=(const Fraction& other) {
  31.         numerator *= other.numerator;
  32.         denominator *= other.denominator;
  33.         Reduction();
  34.         return *this;
  35.     }
  36.  
  37.     Fraction& operator/=(const Fraction& other) {
  38.         numerator *= other.denominator;
  39.         denominator *= other.numerator;
  40.         Reduction();
  41.         return *this;
  42.     }
  43.  
  44.     int Numerator() const {
  45.         return numerator;
  46.     }
  47.  
  48.     int Denominator() const {
  49.         return denominator;
  50.     }
  51.  
  52.     ~Fraction() {}
  53.  
  54.     void print() {
  55.         std::cout << numerator << " / " << denominator << std::endl;
  56.     }
  57.  
  58. private:
  59.     void Reduction() {
  60.  
  61.         if (denominator < 0) {
  62.             numerator = -numerator;
  63.             denominator = -denominator;
  64.         }
  65.  
  66.         int x = abs(numerator); // берем значение по модулю на случай отрицательных значений
  67.         int y = abs(denominator); // берем значение по модулю на случай отрицательных значений
  68.  
  69.         while (x != y) {
  70.             if (x > y) {
  71.                 x = x - y;
  72.             }
  73.             else {
  74.                 y = y - x;
  75.             }
  76.         }
  77.         numerator /= x;
  78.         denominator /= x;
  79.     }
  80.  
  81.     int numerator;
  82.     int denominator;
  83. };
  84.  
  85. Fraction operator+(const Fraction& left, const Fraction& right) {
  86.     Fraction temp = left;
  87.     temp += right;
  88.     return temp;
  89. }
  90.  
  91. Fraction operator-(const Fraction& left, const Fraction& right) {
  92.     Fraction temp = left;
  93.     temp -= right;
  94.     return temp;
  95. }
  96.  
  97. Fraction operator*(const Fraction& left, const Fraction& right) {
  98.     Fraction temp = left;
  99.     temp *= right;
  100.     return temp;
  101. }
  102.  
  103. Fraction operator/(const Fraction& left, const Fraction& right) {
  104.     Fraction temp = left;
  105.     temp /= right;
  106.     return temp;
  107. }
  108.  
  109. bool operator==(const Fraction& left, const Fraction& right) {
  110.     return left.Numerator() == right.Numerator() && left.Denominator() == right.Denominator();
  111. }
  112.  
  113. bool operator!=(const Fraction& left, const Fraction& right) {
  114.     return left.Numerator() != right.Numerator() || left.Denominator() != right.Denominator();
  115. }
  116.  
  117. bool operator>(const Fraction& left, const Fraction& right) {
  118.     return left.Numerator() * right.Denominator() > right.Numerator() * left.Denominator();
  119. }
  120.  
  121. bool operator<(const Fraction& left, const Fraction& right) {
  122.     return left.Numerator() * right.Denominator() < right.Numerator() * left.Denominator();
  123. }
  124.  
  125. bool operator>=(const Fraction& left, const Fraction& right) {
  126.     return left.Numerator() * right.Denominator() >= right.Numerator() * left.Denominator();
  127. }
  128.  
  129. bool operator<=(const Fraction& left, const Fraction& right) {
  130.     return left.Numerator() * right.Denominator() <= right.Numerator() * left.Denominator();
  131. }
  132.  
  133. ostream& operator<<(ostream& output, const Fraction& fraction) {
  134.     output << fraction.Numerator() << " / " << fraction.Denominator();
  135.     return output;
  136. }
  137.  
  138. istream& operator>>(istream& input, Fraction& fraction) { // по неконстантной ссылке!
  139.     int numerator, denominator;
  140.     char slash;
  141.     input >> numerator >> slash >> denominator;
  142.     fraction = Fraction(numerator, denominator);
  143.     return input;
  144. }
  145.  
  146. int main() {
  147.     Fraction a(1, 2);
  148.     Fraction b(3, 4);
  149.  
  150.     cout << (a += b) << endl;
  151.     cout << (a -= b) << endl;
  152.     cout << (a *= b) << endl;
  153.     cout << (a /= b) << endl;
  154.  
  155.     cout << a + b << endl;
  156.     cout << a - b << endl;
  157.     cout << a * b << endl;
  158.     cout << a / b << endl;
  159.  
  160.     cout << "a = " << a << endl;
  161.     cout << "b = " << b << endl;
  162.     cout << boolalpha;
  163.     cout << "a == b " << (a == b) << endl;
  164.     cout << "a != b " << (a != b) << endl;
  165.     cout << "a > b " << (a > b) << endl;
  166.     cout << "a < b " << (a < b) << endl;
  167.     cout << "a >= b " << (a >= b) << endl;
  168.     cout << "a <= b " << (a <= b) << endl;
  169.  
  170.     Fraction n;
  171.     cout << "Enter n: ";
  172.     cin >> n;
  173.     cout << n << endl;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment