Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int NOD(int a, int b) {
  6.     if (a < b) {
  7.         swap(a, b);
  8.     }
  9.     while (a % b != 0) {
  10.         a = a % b;
  11.         swap(a, b);
  12.     }
  13.     return b;
  14. }
  15.  
  16. int nok(int a, int b)
  17. {
  18.     int max = b;
  19.     for (int i = max; i > 0; i++) {
  20.  
  21.         if ((i % a == 0) && (i % b == 0)) {
  22.             return i;
  23.         }
  24.     }
  25. }
  26.  
  27. class Rational {
  28. public:
  29.     Rational(){
  30.         p = 0;
  31.         q = 1;
  32.     }
  33.     Rational(int numerator, int denominator):p(numerator), q(denominator){
  34.         if ((p < 0 && q < 0) || q < 0) {
  35.             p *= -1;
  36.             q *= -1;
  37.         }
  38.         if (p == 0) {
  39.             q = 1;
  40.         }
  41.         else {
  42.             int t = NOD(p, q);
  43.             if (t != 1) {
  44.                 p /= t;
  45.                 q /= t;
  46.             }
  47.         }
  48.     }
  49.  
  50.     int Numerator() const {
  51.         return p;
  52.     }
  53.     int Denominator() const {
  54.         return q;
  55.     }
  56.     bool operator == (Rational& b) {
  57.         return (Denominator() == b.Denominator() && Numerator() == b.Numerator());
  58.     }
  59.  
  60.     Rational operator + (Rational& b) {
  61.  
  62.         int t = nok(Denominator(), b.Denominator());
  63.         int q = t;
  64.         int p = t / Denominator() * Numerator() + t / b.Denominator() * b.Numerator();
  65.         Rational R(p, q);
  66.         return R;
  67.     }
  68.  
  69.  
  70.     Rational operator - (Rational& b) {
  71.  
  72.         int t = nok(Denominator(), b.Denominator());
  73.         int q = t;
  74.         int p = t / Denominator() * Numerator() - t / b.Denominator() * b.Numerator();
  75.         Rational R(p, q);
  76.         return R;
  77.     }
  78. private:
  79.     int p;
  80.     int q;
  81. };
  82.  
  83.  
  84.  
  85. int main() {
  86.     {
  87.         Rational r1(4, 6);
  88.         Rational r2(2, 3);
  89.         bool equal = r1 == r2;
  90.         if (!equal) {
  91.             cout << "4/6 != 2/3" << endl;
  92.             return 1;
  93.         }
  94.     }
  95.  
  96.     {
  97.         Rational a(2, 3);
  98.         Rational b(4, 3);
  99.         Rational c = a + b;
  100.         bool equal = c == Rational(2, 1);
  101.         if (!equal) {
  102.             cout << "2/3 + 4/3 != 2" << endl;
  103.             return 2;
  104.         }
  105.     }
  106.  
  107.     {
  108.         Rational a(5, 7);
  109.         Rational b(2, 9);
  110.         Rational c = a - b;
  111.         bool equal = c == Rational(31, 63);
  112.         if (!equal) {
  113.             cout << "5/7 - 2/9 != 31/63" << endl;
  114.             return 3;
  115.         }
  116.     }
  117.  
  118.     cout << "OK" << endl;
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement