Advertisement
Bibodui

ООП Лаба 4

Mar 21st, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. /*
  2. Страница 63, номер 17:
  3. Создать базовый класс Раir(пара целых чисел)
  4. с операциями проверки на равенство и перемножения полей.
  5. Реализовать операцию вычитания пар по формуле
  6. (а, b) - (с, d) = (а - b, с - d).
  7. Создать производный класс Rational;
  8. определить новые операции сложения(а, b) + (с, d) = (аd + bс, bd) и деления
  9. (а, b) / (с, d) = (аd, bс);
  10. переопределить операцию вычитания(а, b) - (с, d) == (аd - bс, bd).
  11. */
  12.  
  13. #include <iostream>
  14. #include <Windows.h>
  15.  
  16. using namespace std;
  17.  
  18. class Pair
  19. {
  20. protected:
  21.     int a, b;
  22. public:
  23.  
  24.     Pair()
  25.     {
  26.         a = b = 0;
  27.     }
  28.     Pair(int a, int b)
  29.     {
  30.         this->a = a;
  31.         this->b = b;
  32.     }
  33.     void set_a(int a)
  34.     {
  35.         this->a = a;
  36.     }
  37.    
  38.     void set_b(int b)
  39.     {
  40.         this->b = b;
  41.     }
  42.  
  43.     int get_a()
  44.     {
  45.         return a;
  46.     }
  47.  
  48.     int get_b()
  49.     {
  50.         return b;
  51.     }
  52.  
  53.     bool operator== (const Pair& other)
  54.     {
  55.         return (this->a == other.a && this->b == other.b);
  56.     }
  57.  
  58.     Pair operator* (const Pair& other)
  59.     {
  60.         Pair tmp;
  61.  
  62.         tmp.a = this->a * other.a;
  63.         tmp.b = this->b * other.b;
  64.  
  65.         return tmp;
  66.     }
  67.  
  68.     int mult()
  69.     {
  70.         return this->a * this->b;
  71.     }
  72.  
  73.     virtual Pair operator-(Pair& other)
  74.     {
  75.         Pair tmp;
  76.  
  77.         tmp.a = this->a - other.a;
  78.         tmp.b = this->b - other.b;
  79.  
  80.         return tmp;
  81.     }
  82. };
  83.  
  84. class Rational : public Pair
  85. {
  86. public :
  87.     Rational()
  88.     {
  89.         a = b = 0;
  90.     }
  91.  
  92.     Rational(int a, int b)
  93.     {
  94.         this->a = a;
  95.         this->b = b;
  96.     }
  97.  
  98.     Rational operator+ (const Rational& other)
  99.     {
  100.         Rational tmp;
  101.  
  102.         tmp.a = this->a * other.b + this->b * other.a;
  103.         tmp.b = this->b * other.b;
  104.  
  105.         return tmp;
  106.     }
  107.  
  108.     Rational operator/ (const Rational& other)
  109.     {
  110.         Rational tmp;
  111.        
  112.         tmp.a = this->a * other.b;
  113.         tmp.b = this->b * other.a;
  114.  
  115.         return tmp;
  116.     }
  117.  
  118.     Pair operator-(Pair& other) override
  119.     {
  120.         Pair tmp;
  121.  
  122.         tmp.set_a(this->a * other.get_b() - this->b * other.get_a());
  123.         tmp.set_b(this->b * other.get_b());
  124.  
  125.         return tmp;
  126.     }
  127. };
  128.  
  129. int main()
  130. {
  131.     SetConsoleOutputCP(1251);
  132.     SetConsoleCP(1251);
  133.  
  134.     int q, q1;
  135.  
  136.     do
  137.     {
  138.  
  139.         Rational b;
  140.         int a1, b1;
  141.         cout << "Введите первое число:" << endl;
  142.         cin >> a1;
  143.         cin >> b1;
  144.         Rational a(a1, b1);
  145.         cout << "Введите второе число:" << endl;
  146.         cin >> a1;
  147.         cin >> b1;
  148.         b.set_a(a1);
  149.         b.set_b(b1);
  150.         cout << "Введены числа:" << endl;
  151.         cout << a.get_a()<<'\t' << " \t" << b.get_a() << '\t' << endl;
  152.         cout << "---\t" << " и \t" << "---\t" << endl;
  153.         cout << a.get_b() << '\t' << " \t" << b.get_b() << '\t' << endl;
  154.  
  155.         do
  156.         {
  157.             Pair result;
  158.             cout << "Выберите действие:" << endl;
  159.             cout << "1. Сложение" << endl;
  160.             cout << "2. Вычитание" << endl;
  161.             cout << "3. Умножения" << endl;
  162.             cout << "4. Деление" << endl;
  163.             cout << "0. Ввести новые числа" << endl;
  164.             cin >> q;
  165.             if (q == 1)
  166.             {
  167.                 result = a + b;
  168.                 cout << result.get_a() << '\t' << endl;
  169.                 cout << "---\t" << endl;
  170.                 cout << result.get_b() << '\t' << endl;
  171.             }
  172.             else if (q == 2)
  173.             {
  174.                 result = a - b;
  175.                 cout << result.get_a() << '\t' << endl;
  176.                 cout << "---\t" << endl;
  177.                 cout << result.get_b() << '\t' << endl;
  178.             }
  179.             else if (q == 3)
  180.             {
  181.                 result = a * b;
  182.                 cout << result.get_a() << '\t' << endl;
  183.                 cout << "---\t" << endl;
  184.                 cout << result.get_b() << '\t' << endl;
  185.             }
  186.             else if (q == 4)
  187.             {
  188.                 result = a / b;
  189.                 cout << result.get_a() << '\t' << endl;
  190.                 cout << "---\t" << endl;
  191.                 cout << result.get_b() << '\t' << endl;
  192.             }
  193.  
  194.         } while (q != 0);
  195.         cout << "0. Выход" << endl;
  196.         cin >> q1;
  197.     } while (q1 != 0);
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement