#include <vector> #include <cmath> #include <iostream> class Polynomial { public:…
- User
#include #include #include
class Polynomial { public: Polynomial(const std::vector& 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; - Assistant
Ошибка:
В методе operator/ в классе Polynomial используется оператор & вместо оператора *, при умножении многочлена divisor на term.
Исправление:
Заменить оператор & на оператор *.
Измененный метод operator/:
Polynomial operator/(const Polynomial& divisor) const { if (divisor.coefficients.empty() || divisor.coefficients.back() == 0) { throw std::invalid_argument("Division by zero"); } std::vector 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(i - divisor.degree(), 0)); term.coefficients.push_back(resultCoefficients[i - divisor.degree()]); *this -= divisor * term; } removeLeadingZeros(); return Polynomial(resultCoefficients); }
- User
- Assistant