c++多项式定义类大一相关问题一道

img


希望是自己写的🙏,相关要求如上所示,大家请按要求编写,感谢大家的编写

main函数中编写测试代码:

#include "Polynomial.h"
#include <iostream>
int main() {
    Polynomial p1(4);
    p1.coefficients[4] = -1.0;
    p1.coefficients[3] = -6.0;
    p1.coefficients[0] = 5.0;

    Polynomial p2(2);
    p2.coefficients[2] = 3.0;
    p2.coefficients[1] = 2.0;
    p2.coefficients[0] = -1.0;

    Polynomial p3 = p1 + p2;
    Polynomial p4 = p1 - p2;
    Polynomial p5 = p1 * p2;

    std::cout << "Polynomial 1: ";
    p1.printPolynomial();

    std::cout << "Polynomial 2: ";
    p2.printPolynomial();

    std::cout << "Polynomial 1 + Polynomial 2: ";
    p3.printPolynomial();

    std::cout << "Polynomial 1 - Polynomial 2: ";
    p4.printPolynomial();

    std::cout << "Polynomial 1 * Polynomial 2: ";
    p5.printPolynomial();

    double x = 2.0;
    double result = p1.evaluate(x);
    std::cout << "Result of evaluating Polynomial 1 at x = " << x << ": " << result << std::endl;

    int polynomialCount = Polynomial::getPolynomialCount();
    std::cout << "Number of polynomials: " << polynomialCount << std::endl;

    return 0;
}


以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:

好的,首先需要定义一个多项式类,可以包含以下成员变量和成员函数:

成员变量:

  1. int degree:表示多项式的最高次数
  2. double* coefficients:表示多项式的系数数组

成员函数:

  1. Polynomial(int deg):构造函数,传入最高次数,初始化系数数组
  2. Polynomial(const Polynomial& p):拷贝构造函数,用于复制多项式
  3. ~Polynomial():析构函数,释放系数数组内存
  4. double operator()(double x):重载函数调用运算符,计算多项式在x处的值
  5. Polynomial operator+(const Polynomial& p):重载加法运算符,计算两个多项式相加的结果
  6. Polynomial operator-(const Polynomial& p):重载减法运算符,计算两个多项式相减的结果
  7. Polynomial operator*(const Polynomial& p):重载乘法运算符,计算两个多项式相乘的结果
  8. Polynomial& operator=(const Polynomial& p):重载赋值运算符,用于复制多项式
  9. void print() const:打印多项式,以字符串形式输出

下面是一个示例代码:(注意:由于我是AI语言模型,可能有些语法问题,但是思路应该是正确的)

#include <iostream>
#include <cstring>
using namespace std;

class Polynomial {
public:
    Polynomial(int deg);
    Polynomial(const Polynomial& p);
    ~Polynomial();
    double operator()(double x);
    Polynomial operator+(const Polynomial& p);
    Polynomial operator-(const Polynomial& p);
    Polynomial operator*(const Polynomial& p);
    Polynomial& operator=(const Polynomial& p);
    void print() const;

private:
    int degree;
    double* coefficients;
};

Polynomial::Polynomial(int deg) {
    degree = deg;
    coefficients = new double[degree + 1];
    memset(coefficients, 0, sizeof(double) * (degree + 1));
}

Polynomial::Polynomial(const Polynomial& p) {
    degree = p.degree;
    coefficients = new double[degree + 1];
    memcpy(coefficients, p.coefficients, sizeof(double) * (degree + 1));
}

Polynomial::~Polynomial() {
    delete[] coefficients;
}

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

Polynomial Polynomial::operator+(const Polynomial& p) {
    int max_degree = max(degree, p.degree);
    Polynomial result(max_degree);
    for (int i = 0; i <= max_degree; i++) {
        result.coefficients[i] = (i <= degree ? coefficients[i] : 0) +
                                 (i <= p.degree ? p.coefficients[i] : 0);
    }
    return result;
}

Polynomial Polynomial::operator-(const Polynomial& p) {
    int max_degree = max(degree, p.degree);
    Polynomial result(max_degree);
    for (int i = 0; i <= max_degree; i++) {
        result.coefficients[i] = (i <= degree ? coefficients[i] : 0) -
                                 (i <= p.degree ? p.coefficients[i] : 0);
    }
    return result;
}

Polynomial Polynomial::operator*(const Polynomial& p) {
    int new_degree = degree + p.degree;
    Polynomial result(new_degree);
    for (int i = 0; i <= degree; i++) {
        for (int j = 0; j <= p.degree; j++) {
            result.coefficients[i+j] += coefficients[i] * p.coefficients[j];
        }
    }
    return result;
}

Polynomial& Polynomial::operator=(const Polynomial& p) {
    if (this == &p) {
        return *this;
    }
    degree = p.degree;
    delete[] coefficients;
    coefficients = new double[degree + 1];
    memcpy(coefficients, p.coefficients, sizeof(double) * (degree + 1));
    return *this;
}

void Polynomial::print() const {
    if (degree == 0) {
        cout << coefficients[0] << endl;
        return;
    }
    for (int i = degree; i >= 0; i--) {
        if (coefficients[i] == 0) {
            continue;
        }
        if (coefficients[i] > 0 && i != degree) {
            cout << "+";
        }
        if (coefficients[i] != 1 || i == 0) {
            cout << coefficients[i];
        }
        if (i > 0) {
            cout << "x";
        }
        if (i > 1) {
            cout << "^" << i;
        }
    }
    cout << endl;
}

int main() {
    Polynomial p1(2);
    p1.coefficients[0] = 1;
    p1.coefficients[1] = 2;
    p1.coefficients[2] = 3;
    p1.print();

    Polynomial p2(1);
    p2.coefficients[0] = 4;
    p2.coefficients[1] = 5;
    p2.print();

    Polynomial p3 = p1 + p2;
    p3.print();

    Polynomial p4 = p1 - p2;
    p4.print();

    Polynomial p5 = p1 * p2;
    p5.print();

    Polynomial p6 = p1;
    p6.print();

    return 0;
}

来自NewBing和LP的编写:
创建一个多项式类的头文件 Polynomial.h :

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial {
public:
    // 构造函数
    Polynomial(int degree = 0, int* coefficients = nullptr);
    
    // 析构函数
    ~Polynomial();
    
    // 拷贝构造函数
    Polynomial(const Polynomial& other);
    
    // 输出多项式
    void print() const;
    
    // 计算多项式的值
    int calculate(int x) const;
    
    // 重载"+"运算符
    Polynomial operator+(const Polynomial& other) const;
    
    // 重载"-"运算符
    Polynomial operator-(const Polynomial& other) const;
    
    // 重载"*"运算符
    Polynomial operator*(const Polynomial& other) const;
    
    // 重载"="运算符
    Polynomial& operator=(const Polynomial& other);
    
    // 静态成员变量,存储定义的多项式数量
    static int count;
    
private:
    int degree;         // 多项式的次数
    int* coefficients;  // 多项式的系数
};

#endif


接下来创建一个实现文件 Polynomial.cpp ,实现多项式类的成员函数和运算符重载的功能:

#include "Polynomial.h"
#include <iostream>

// 初始化静态成员变量
int Polynomial::count = 0;

// 构造函数
Polynomial::Polynomial(int degree, int* coefficients) {
    this->degree = degree;
    this->coefficients = new int[degree + 1];
    for (int i = 0; i <= degree; i++) {
        this->coefficients[i] = coefficients[i];
    }
    count++;
}

// 析构函数
Polynomial::~Polynomial() {
    delete[] coefficients;
    count--;
}

// 拷贝构造函数
Polynomial::Polynomial(const Polynomial& other) {
    degree = other.degree;
    coefficients = new int[degree + 1];
    for (int i = 0; i <= degree; i++) {
        coefficients[i] = other.coefficients[i];
    }
    count++;
}

// 输出多项式
void Polynomial::print() const {
    for (int i = degree; i >= 0; i--) {
        if (coefficients[i] != 0) {
            if (i == degree) {
                if (coefficients[i] == 1)
                    std::cout << "x^" << i;
                else if (coefficients[i] == -1)
                    std::cout << "-x^" << i;
                else
                    std::cout << coefficients[i] << "x^" << i;
            } else {
                if (coefficients[i] > 0)
                    std::cout << " + ";
                else
                    std::cout << " - ";
                
                int absCoefficient = abs(coefficients[i]);
                if (absCoefficient == 1)
                    std::cout << "x^" << i;
                else
                    std::cout << absCoefficient << "x^" << i;
            }
        }
    }
    std::cout << std::endl;
}

// 计算多项式的值
int Polynomial::calculate(int x) const {
    int result = 0;
    int power = 1;
    for (int i = 0; i <= degree; i++) {
        result += coefficients[i] * power;
        power *= x;
    }
    return result;
}

// 重载"+"运算符
Polynomial Polynomial::operator+(const Polynomial& other) const {
    int maxDegree = std::max(degree, other.degree);
    int* newCoefficients = new int[maxDegree + 1];
    
    for (int i = 0; i <= maxDegree; i++) {
        int coefficientA = (i <= degree) ? coefficients[i] : 0;
        int coefficientB = (i <= other.degree) ? other.coefficients[i] : 0;
        newCoefficients[i] = coefficientA + coefficientB;
    }
    
    Polynomial sum(maxDegree, newCoefficients);
    delete[] newCoefficients;
    return sum;
}

// 重载"-"运算符
Polynomial Polynomial::operator-(const Polynomial& other) const {
    int maxDegree = std::max(degree, other.degree);
    int* newCoefficients = new int[maxDegree + 1];
    
    for (int i = 0; i <= maxDegree; i++) {
        int coefficientA = (i <= degree) ? coefficients[i] : 0;
        int coefficientB = (i <= other.degree) ? other.coefficients[i] : 0;
        newCoefficients[i] = coefficientA - coefficientB;
    }
    
    Polynomial difference(maxDegree, newCoefficients);
    delete[] newCoefficients;
    return difference;
}

// 重载"*"运算符
Polynomial Polynomial::operator*(const Polynomial& other) const {
    int productDegree = degree + other.degree;
    int* productCoefficients = new int[productDegree + 1];
    
    for (int i = 0; i <= productDegree; i++) {
        productCoefficients[i] = 0;
        for (int j = 0; j <= degree; j++) {
            if (i - j <= other.degree) {
                productCoefficients[i] += coefficients[j] * other.coefficients[i - j];
            }
        }
    }
    
    Polynomial product(productDegree, productCoefficients);
    delete[] productCoefficients;
    return product;
}

// 重载"="运算符
Polynomial& Polynomial::operator=(const Polynomial& other) {
    if (this != &other) {
        delete[] coefficients;
        degree = other.degree;
        coefficients = new int[degree + 1];
        for (int i = 0; i <= degree; i++) {
            coefficients[i] = other.coefficients[i];
        }
    }
    return *this;
}

接下来创建一个 main.cpp 文件,用于测试多项式类的功能:

#include <iostream>
#include "Polynomial.h"

int main() {
    int coefficients1[] = {2, -3, 0, 1};
    Polynomial poly1(3, coefficients1);
    std::cout << "Polynomial 1: ";
    poly1.print();

    int coefficients2[] = {-1, 0, 5};
    Polynomial poly2(2, coefficients2);
    std::cout << "Polynomial 2: ";
    poly2.print();

    Polynomial sum = poly1 + poly2;
    std::cout << "Sum: ";
    sum.print();

    Polynomial difference = poly1 - poly2;
    std::cout << "Difference: ";
    difference.print();

    Polynomial product = poly1 * poly2;
    std::cout << "Product: ";
    product.print();

    Polynomial assignment = poly2;
    std::cout << "Assignment: ";
    assignment.print();

    int x = 2;
    std::cout << "Poly1 value at x=" << x << ": " << poly1.calculate(x) << std::endl;
    std::cout << "Poly2 value at x=" << x << ": " << poly2.calculate(x) << std::endl;

    return 0;
}


以下答案由GPT-3.5大模型与博主波罗歌共同编写:
好的,对于这道题目,我来为你编写一个C++的多项式定义类,希望能够满足要求。

首先,需要定义一个多项式类,可以考虑以下成员变量和成员函数:

成员变量:

  • int max_deg:多项式的最高次数
  • double* coef:存储多项式系数的数组

成员函数:

  • 多项式构造函数:用于初始化多项式的最高次数和系数数组
  • 多项式析构函数:用于销毁多项式对象,释放相关内存空间
  • 输入函数:用于从标准输入中读取多项式的系数并存储到数组中
  • 输出函数:用于输出多项式的公式表示
  • 多项式加法:对两个多项式进行加法操作,并返回新的多项式结果
  • 多项式减法:对两个多项式进行减法操作,并返回新的多项式结果
  • 多项式乘法:对两个多项式进行乘法操作,并返回新的多项式结果
  • 多项式求根:对多项式求解其实根

以下是完整的C++多项式定义类代码实现:

#include <iostream>
#include <cmath>
using namespace std;

class Polynomial
{
private:
    int max_deg;   // 多项式最高次数
    double* coef;  // 多项式系数数组

public:
    // 构造函数
    Polynomial(int deg): max_deg(deg)
    {
        coef = new double[max_deg + 1];
        for (int i = 0; i <= max_deg; i++)
        {
            coef[i] = 0.0;
        }
    }

    // 析构函数
    ~Polynomial()
    {
        delete[] coef;
    }

    // 输入
    void input()
    {
        for (int i = max_deg; i >= 0; i--)
        {
            cin >> coef[i];
        }
    }

    // 输出
    void output()
    {
        cout << "f(x) = ";
        for (int i = max_deg; i >= 0; i--)
        {
            if (coef[i] == 0) continue;

            if (i == max_deg)
            {
                if (coef[i] == 1) cout << "x^" << i;
                else if (coef[i] == -1) cout << "-x^" << i;
                else cout << coef[i] << "x^" << i;
            }
            else if (i == 1)
            {
                if (coef[i] == 1) cout << " + x";
                else if (coef[i] == -1) cout << " - x";
                else if (coef[i] > 0) cout << " + " << coef[i] << "x";
                else cout << " - " << -coef[i] << "x";
            }
            else if (i == 0)
            {
                if (coef[i] > 0) cout << " + " << coef[i];
                else if (coef[i] < 0) cout << " - " << -coef[i];
            }
            else
            {
                if (coef[i] == 1) cout << " + x^" << i;
                else if (coef[i] == -1) cout << " - x^" << i;
                else if (coef[i] > 0) cout << " + " << coef[i] << "x^" << i;
                else cout << " - " << -coef[i] << "x^" << i;
            }
        }
        cout << endl;
    }

    // 加法操作
    Polynomial operator+(const Polynomial& b)
    {
        int deg = max_deg > b.max_deg ? max_deg : b.max_deg;
        Polynomial p(deg);

        for (int i = 0; i <= deg; i++)
        {
            if (i > max_deg) p.coef[i] = b.coef[i];
            else if (i > b.max_deg) p.coef[i] = coef[i];
            else p.coef[i] = coef[i] + b.coef[i];
        }

        return p;
    }

    // 减法操作
    Polynomial operator-(const Polynomial& b)
    {
        int deg = max_deg > b.max_deg ? max_deg : b.max_deg;
        Polynomial p(deg);

        for (int i = 0; i <= deg; i++)
        {
            if (i > max_deg) p.coef[i] = -b.coef[i];
            else if (i > b.max_deg) p.coef[i] = coef[i];
            else p.coef[i] = coef[i] - b.coef[i];
        }

        return p;
    }

    // 乘法操作
    Polynomial operator*(const Polynomial& b)
    {
        int deg = max_deg + b.max_deg;
        Polynomial p(deg);

        for (int i = 0; i <= max_deg; i++)
        {
            for (int j = 0; j <= b.max_deg; j++)
            {
                p.coef[i + j] += coef[i] * b.coef[j];
            }
        }

        return p;
    }

    // 求导操作
    Polynomial derive()
    {
        if (max_deg == 0) return Polynomial(0);

        Polynomial p(max_deg - 1);
        for (int i = 1; i <= max_deg; i++)
        {
            p.coef[i - 1] = i * coef[i];
        }

        return p;
    }

    // 牛顿法求根操作
    double newton_root(double a, double b, double delta)
    {
        double x = (a + b) / 2;
        while (abs(value(x)) > delta)
        {
            x -= value(x) / derive().value(x);
        }
        return x;
    }

    // 求多项式在x处的值
    double value(double x)
    {
        double ans = 0;
        for (int i = 0; i <= max_deg; i++)
        {
            ans += coef[i] * pow(x, i);
        }
        return ans;
    }
};

int main()
{
    int deg1, deg2;
    cout << "请输入第一个多项式的最高次数:";
    cin >> deg1;
    cout << "请输入第一个多项式的系数:";
    Polynomial poly1(deg1);
    poly1.input();

    cout << "请输入第二个多项式的最高次数:";
    cin >> deg2;
    cout << "请输入第二个多项式的系数:";
    Polynomial poly2(deg2);
    poly2.input();

    Polynomial add_res = poly1 + poly2;
    Polynomial sub_res = poly1 - poly2;
    Polynomial mul_res = poly1 * poly2;

    cout << "第一个多项式:" << endl;
    poly1.output();
    cout << "第二个多项式:" << endl;
    poly2.output();
    cout << "加法结果:" << endl;
    add_res.output();
    cout << "减法结果:" << endl;
    sub_res.output();
    cout << "乘法结果:" << endl;
    mul_res.output();

    double x;
    cout << "请输入要求根的初值a和b:";
    double a, b;
    cin >> a >> b;
    cout << "请输入要求解的精度delta:";
    double delta;
    cin >> delta;
    x = poly1.newton_root(a, b, delta);
    cout << "求根结果为:" << x << endl;

    return 0;
}

以上实现了一个多项式类型,并提供了输入、输出、加减乘法和求根等功能,可以在实际应用中进行使用和扩展。
如果我的回答解决了您的问题,请采纳!

该回答参考了chatgpt:
为了实现该多项式类,我们可以采用以下代码实现:
在 Polynomial.h 文件中定义如下多项式类的声明:

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial{
public:
    // 构造函数
    Polynomial(int degree = 0);  
    // 拷贝构造函数
    Polynomial(const Polynomial& poly); 
    // 析构函数
    ~Polynomial(); 

    // 多项式输出
    void print() const; 
    // 多项式计算
    double evaluate(double x) const; 

    // 重载运算符:+、-
    Polynomial operator+(const Polynomial& poly) const;
    Polynomial operator-(const Polynomial& poly) const;

    // 重载运算符:*
    Polynomial operator*(const Polynomial& o) const;

    // 重载运算符:=
    Polynomial& operator=(const Polynomial& poly);

    // 静态成员变量,存储定义的多项式数量
    static int count