Загрузка данных


1. Составить описание класса для представления типа «обыкновенная
дробь» вида
,
A
C
B

где A и B – целые числа.
а) обеспечить выполнение 4-х арифметических действий над этими
объектами (+=, +, –=, –, *=, *, /=, /);
б) предусмотреть операцию автоматического «сокращения дроби»
– удаления общих множителей числителя и знаменателя;
в) обеспечить операцию декомпозиции неправильной дроби на целую часть и правильную дробь, используя операцию деления с остатком.
2. Составить описание класса для представления типа «полином» вида
 
2
0 1 2 ... ,
n P x a a x a x a x     
n
где аi – вещественные коэффициенты.
а) обеспечить выполнение арифметических действий над этими
объектами (+=, +, –=, –, *=, *);
б) предусмотреть операцию деления полиномов с остатком, используя «деление углом» (алгоритм Евклида);
в) предусмотреть функцию обновления, отбрасывающую старшие
члены полинома с нулевыми коэффициентами;
г) перегрузить операцию () для вычисления значения полинома.
18
3. Для всех типов организовать необходимые конструкторы и деструкторы.
4. Также при необходимости реализовать перегруженные операторы
присваивания и перемещения.

#include <iostream>
#include <stdexcept>

using namespace std;

class Polynomial {
private:
    int n;
    double* a;
public:
    

};

class Fraction {
private:
    int A;
    int B;

    static int nod(int x, int y) {
        if (x < 0) x = -x;
        if (y < 0) y = -y;
        while (y != 0) {
            int t = x % y;
            x = y;
            y = t;
        }
        return x == 0 ? 1 : x;
    }

    void normalize() {
        if (B == 0) {
            throw invalid_argument("Знаменатель не может быть равен 0");
        }
        if (B < 0) {
            A = -A;
            B = -B;
        }
        int n = nod(A, B);
        A /= n;
        B /= n;
    }

public:
    Fraction() : A(1), B(2) { }
    Fraction(int a, int b) : A(a), B(b) { normalize(); }

    int integerPart() const {
        return A / B;
    }

    // правильная дробь
    Fraction properPart() const {
        Fraction r;
        r.A = A % B;
        r.B = B;
        r.normalize();
        return r;
    }

    // декомпозиция whole + proper
    void decompose(int& whole, Fraction& proper) const {
        whole = A / B;
        proper.A = A % B;
        proper.B = B;
        proper.normalize();
    }

    // +
    Fraction operator+(const Fraction& other) const {
        Fraction result;
        result.A = A * other.B + other.A * B;
        result.B = B * other.B;
        result.normalize();
        return result;
    }

    // -
    Fraction operator-(const Fraction& other) const {
        Fraction result;
        result.A = A * other.B - other.A * B;
        result.B = B * other.B;
        result.normalize();
        return result;
    }

    // *
    Fraction operator*(const Fraction& other) const {
        Fraction result;
        result.A = A * other.A;
        result.B = B * other.B;
        result.normalize();
        return result;
    }

    // /
    Fraction operator/(const Fraction& other) const {
        if (other.A == 0) {
            throw invalid_argument("Деление на ноль");
        }
        Fraction result;
        result.A = A * other.B;
        result.B = B * other.A;
        result.normalize();
        return result;
    }

    // +=
    Fraction& operator+=(const Fraction& other) {
        A = A * other.B + other.A * B;
        B = B * other.B;
        normalize();
        return *this;
    }

    // -=
    Fraction& operator-=(const Fraction& other) {
        A = A * other.B - other.A * B;
        B = B * other.B;
        normalize();
        return *this;
    }

    // *=
    Fraction& operator*=(const Fraction& other) {
        A *= other.A;
        B *= other.B;
        normalize();
        return *this;
    }

    // /=
    Fraction& operator/=(const Fraction& other) {
        if (other.A == 0) {
            throw invalid_argument("Деление на ноль");
        }
        A *= other.B;
        B *= other.A;
        normalize();
        return *this;
    }

    friend ostream& operator<<(ostream& os, const Fraction& f) {
        if (f.B == 1)
            os << f.A;
        else
            os << f.A << "/" << f.B;
        return os;
    }
};

int main() {
    Fraction f(7, 3);  // 7/3

    cout << "fract: " << f << endl;                        // 7/3
    cout << "integer part: " << f.integerPart() << endl;    // 2
    cout << "proper part: " << f.properPart() << endl;// 1/3

    int whole;
    Fraction proper;
    f.decompose(whole, proper);
    cout << "decompose: " << whole << " + " << proper << endl; // 2 + 1/3

    Fraction g(-7, 3);
    int w; Fraction p;
    g.decompose(w, p);
    cout << "decompose -7/3: " << w << " + " << p << endl;     // -2 + -1/3
}