Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
01.11.2025
Размер:
3.36 Кб
Скачать
#include <iostream>
#include <windows.h>
using namespace std;

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

class Rational {
private:
    int numerator, denominator;
    
    void reduce() {
        int common = gcd(numerator, denominator);
        numerator /= common;
        denominator /= common;
        if (denominator < 0) {
            numerator = -numerator;
            denominator = -denominator;
        }
    }
    
public:
    Rational(int num = 0, int den = 1) : numerator(num), denominator(den) {
        if (denominator == 0) throw invalid_argument("Знаменатель не может быть нулем");
        reduce();
    }
    
    // Сложение
    Rational operator+(const Rational& other) const {
        return Rational(numerator * other.denominator + other.numerator * denominator,
                       denominator * other.denominator);
    }
    
    // Вычитание
    Rational operator-(const Rational& other) const {
        return Rational(numerator * other.denominator - other.numerator * denominator,
                       denominator * other.denominator);
    }
    
    // Умножение
    Rational operator*(const Rational& other) const {
        return Rational(numerator * other.numerator,
                       denominator * other.denominator);
    }
    
    // Деление
    Rational operator/(const Rational& other) const {
        if (other.numerator == 0) throw invalid_argument("Деление на ноль");
        return Rational(numerator * other.denominator,
                       denominator * other.numerator);
    }
    
    // Приведение к несократимому виду (!)
    Rational operator!() const {
        return *this; // Дробь всегда в несократимом виде
    }
    
    // Сравнение (==)
    bool operator==(const Rational& other) const {
        return numerator == other.numerator && denominator == other.denominator;
    }
    
    void print() const {
        cout << numerator;
        if (denominator != 1) cout << "/" << denominator;
        cout << endl;
    }
    
    friend ostream& operator<<(ostream& os, const Rational& r);
};

ostream& operator<<(ostream& os, const Rational& r) {
    os << r.numerator;
    if (r.denominator != 1) os << "/" << r.denominator;
    return os;
}

int main() {
    SetConsoleOutputCP(65001);
    
    cout << "=== Задача 3.10 - Рациональная дробь ===" << endl;
    
    Rational r1(2, 3);
    Rational r2(3, 4);
    Rational r3(6, 8); // Сократимая дробь
    
    cout << "Дробь r1: " << r1 << endl;
    cout << "Дробь r2: " << r2 << endl;
    cout << "Дробь r3: " << r3 << " (автоматически сокращена)" << endl;
    
    Rational sum = r1 + r2;
    cout << "r1 + r2 = " << sum << endl;
    
    Rational product = r1 * r2;
    cout << "r1 * r2 = " << product << endl;
    
    Rational quotient = r1 / r2;
    cout << "r1 / r2 = " << quotient << endl;
    
    cout << "r2 == r3: " << (r2 == r3 ? "true" : "false") << endl;
    
    Rational reduced = !r3;
    cout << "!r3 = " << reduced << " (уже несократима)" << endl;
    
    return 0;
}
Соседние файлы в папке Лаба3