#include <vector>
#include <cmath>
#include <iostream>

class Polynomial {
    public:
        Polynomial(const std::vector<double>& coefficients) : coefficients(coefficients) { }

        double operator()(double x) const {
            double result = 0;
            for (int i = 0; i < coefficients.size(); i++) {
                result += coefficients[i] * std::pow(x, i);
            }
            return result;
        }

        Polynomial operator+(const Polynomial& other) const {
            int size = std::max(coefficients.size(), other.coefficients.size());
            std::vector<double> result(size, 0);
            for (int i = 0; i < size; i++) {
                if (i < coefficients.size()) {
                    result[i] += coefficients[i];
                }
                if (i < other.coefficients.size()) {
                    result[i] += other.coefficients[i];
                }
            }
            return Polynomial(result);
        }

        Polynomial operator-(const Polynomial& other) const {
            int size = std::max(coefficients.size(), other.coefficients.size());
            std::vector<double> result(size, 0);
            for (int i = 0; i < size; i++) {
                if (i < coefficients.size()) {
                    result[i] += coefficients[i];
                }
                if (i < other.coefficients.size()) {
                    result[i] -= other.coefficients[i];
                }
            }
            return Polynomial(result);
        }

        Polynomial operator*(const Polynomial& other) const {
            int size = coefficients.size() + other.coefficients.size() - 1;
            std::vector<double> result(size, 0);
            for (int i = 0; i < coefficients.size(); i++) {
                for (int j = 0; j < other.coefficients.size(); j++) {
                    result[i + j] += coefficients[i] * other.coefficients[j];
                }
            }
            return Polynomial(result);
        }
        
        //Polynomial operator/(const Polynomial& other) const {
        //    int size = coefficients.size() + other.coefficients.size() - 1;
        //    std::vector<double> result(size, 0);
        //    for (int i = 0; i < coefficients.size(); i++) {
        //        for (int j = 0; j < other.coefficients.size(); j++) {
        //            result[i + j] += coefficients[i] / other.coefficients[j];
        //        }
        //    }
        //    return Polynomial(result);
        //}
        
        Polynomial operator/(const Polynomial& divisor) const {
            if (divisor.coefficients.empty() || divisor.coefficients.back() == 0) {
                throw std::invalid_argument("Division by zero");
            }
            std::vector<double> resultCoefficients(degree() - divisor.degree() + 1, 0);
            for (int i = degree(); i >= divisor.degree(); i--) {
                resultCoefficients[i - divisor.degree()] = coefficients[i] / divisor.coefficients[divisor.degree()];
                Polynomial term(std::vector<double>(i - divisor.degree(), 0));
                term,coefficients.push_back(resultCoefficients[i - divisor.degree()]);
                *this -= divisor & term;
            }
            removeLeadingZeros();
            return Polynomial(resultCoefficients);
        }
        
        Polynomial& operator+=(const Polynomial& other) {
            *this = *this + other;
            return *this;
        }

        Polynomial& operator-=(const Polynomial& other) {
            *this = *this - other;
            return *this;
        }

        Polynomial& operator*=(const Polynomial& other) {
            *this = *this * other;
            return *this;
        }

        friend std::ostream& operator<<(std::ostream& stream, const Polynomial& poly) {
            for (int i = poly.coefficients.size() - 1; i >= 0; i--) {
                if (poly.coefficients[i] == 0) continue;
                if (i != poly.coefficients.size() - 1)
                    if(poly.coefficients[i] >= 0) stream << " + " << poly.coefficients[i];
                    else stream << " - " << abs(poly.coefficients[i]);
                else stream << poly.coefficients[i];
                if (i > 0) stream << "x^" << i;            
            }
            return stream;
        }

    private:
        std::vector<double> coefficients;
};

int main() {
    int n;
    std::cout << "Введите степень первого многочлена: ";
    std::cin >> n;
    std::vector<double> coeffs_a(n+1);
    std::cout << "Введите коэффициенты первого многочлена, начиная со старшего:";
    for (int i = n; i >= 0; i--) {
        std::cin >> coeffs_a[i];
    }
    Polynomial a(coeffs_a);
    std::cout << "Введите степень второго многочлена: ";
    std::cin >> n;
    std::vector<double> coeffs_b(n+1);
    std::cout << "Введите коэффициенты второго многочлена, начиная со старшего:";
    for (int i = n; i >= 0; i--) {
        std::cin >> coeffs_b[i];
    }
    Polynomial b(coeffs_b);
    std::cout << "Многочлен a = " << a << std::endl;
    std::cout << "Многочлен b = " << b << std::endl;
    std::cout << "Сумма многочленов: " << a + b << std::endl;
    std::cout << "Разность многочленов: " << a - b << std::endl;
    std::cout << "Произведение многочленов: " << a * b << std::endl;
    std::cout << "Частное многочленов: " << a / b << std::endl;
    return 0;
} Исправь ошибку