Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Страница 63, номер 17:
- Создать базовый класс Раir(пара целых чисел)
- с операциями проверки на равенство и перемножения полей.
- Реализовать операцию вычитания пар по формуле
- (а, b) - (с, d) = (а - b, с - d).
- Создать производный класс Rational;
- определить новые операции сложения(а, b) + (с, d) = (аd + bс, bd) и деления
- (а, b) / (с, d) = (аd, bс);
- переопределить операцию вычитания(а, b) - (с, d) == (аd - bс, bd).
- */
- #include <iostream>
- #include <Windows.h>
- using namespace std;
- class Pair
- {
- protected:
- int a, b;
- public:
- Pair()
- {
- a = b = 0;
- }
- Pair(int a, int b)
- {
- this->a = a;
- this->b = b;
- }
- void set_a(int a)
- {
- this->a = a;
- }
- void set_b(int b)
- {
- this->b = b;
- }
- int get_a()
- {
- return a;
- }
- int get_b()
- {
- return b;
- }
- bool operator== (const Pair& other)
- {
- return (this->a == other.a && this->b == other.b);
- }
- Pair operator* (const Pair& other)
- {
- Pair tmp;
- tmp.a = this->a * other.a;
- tmp.b = this->b * other.b;
- return tmp;
- }
- int mult()
- {
- return this->a * this->b;
- }
- virtual Pair operator-(Pair& other)
- {
- Pair tmp;
- tmp.a = this->a - other.a;
- tmp.b = this->b - other.b;
- return tmp;
- }
- };
- class Rational : public Pair
- {
- public :
- Rational()
- {
- a = b = 0;
- }
- Rational(int a, int b)
- {
- this->a = a;
- this->b = b;
- }
- Rational operator+ (const Rational& other)
- {
- Rational tmp;
- tmp.a = this->a * other.b + this->b * other.a;
- tmp.b = this->b * other.b;
- return tmp;
- }
- Rational operator/ (const Rational& other)
- {
- Rational tmp;
- tmp.a = this->a * other.b;
- tmp.b = this->b * other.a;
- return tmp;
- }
- Pair operator-(Pair& other) override
- {
- Pair tmp;
- tmp.set_a(this->a * other.get_b() - this->b * other.get_a());
- tmp.set_b(this->b * other.get_b());
- return tmp;
- }
- };
- int main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- int q, q1;
- do
- {
- Rational b;
- int a1, b1;
- cout << "Введите первое число:" << endl;
- cin >> a1;
- cin >> b1;
- Rational a(a1, b1);
- cout << "Введите второе число:" << endl;
- cin >> a1;
- cin >> b1;
- b.set_a(a1);
- b.set_b(b1);
- cout << "Введены числа:" << endl;
- cout << a.get_a()<<'\t' << " \t" << b.get_a() << '\t' << endl;
- cout << "---\t" << " и \t" << "---\t" << endl;
- cout << a.get_b() << '\t' << " \t" << b.get_b() << '\t' << endl;
- do
- {
- Pair result;
- cout << "Выберите действие:" << endl;
- cout << "1. Сложение" << endl;
- cout << "2. Вычитание" << endl;
- cout << "3. Умножения" << endl;
- cout << "4. Деление" << endl;
- cout << "0. Ввести новые числа" << endl;
- cin >> q;
- if (q == 1)
- {
- result = a + b;
- cout << result.get_a() << '\t' << endl;
- cout << "---\t" << endl;
- cout << result.get_b() << '\t' << endl;
- }
- else if (q == 2)
- {
- result = a - b;
- cout << result.get_a() << '\t' << endl;
- cout << "---\t" << endl;
- cout << result.get_b() << '\t' << endl;
- }
- else if (q == 3)
- {
- result = a * b;
- cout << result.get_a() << '\t' << endl;
- cout << "---\t" << endl;
- cout << result.get_b() << '\t' << endl;
- }
- else if (q == 4)
- {
- result = a / b;
- cout << result.get_a() << '\t' << endl;
- cout << "---\t" << endl;
- cout << result.get_b() << '\t' << endl;
- }
- } while (q != 0);
- cout << "0. Выход" << endl;
- cin >> q1;
- } while (q1 != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement