一个多项式类

A polynomial class

本文关键字:多项式 一个      更新时间:2023-10-16

这是我想解决的问题:

使用动态数组,实现一个多项式类与多项式加法,减法和乘法。讨论:多项式中的变量除了作为占位符之外什么都不做的系数。因此,多项式唯一有趣的地方就是它的数组系数和相应的指数。想想多项式X X X + X + 1x*x的项在哪里?实现多项式类的一个简单方法是使用双精度数组来存储系数。数组的索引是对应项的指数。如果缺少一项,那么它就是0系数。有一些技术可以表示有很多缺失的高次多项式条款。它们使用所谓的稀疏矩阵技术。除非你已经知道这些技巧,还是学得很快,不要用这些技巧。提供一个默认构造函数、一个复制构造函数和一个参数化构造函数这样就可以构造任意多项式。提供重载的操作符=和析构函数。提供这些操作:多项式+多项式,常数+多项式,多项式+常数,多项式-多项式,常数-多项式,多项式-常数。多项式*多项式,常数*多项式,多项式*常数,提供函数分配和提取系数,指数索引。提供一个函数来计算双精度类型的多项式值。您应该决定是将这些函数实现为成员函数、友元函数还是独立函数。

这不是为了上课,我只是想自学c++,因为我需要它,因为今年秋天我将在FSU开始我的金融数学研究生学习。下面是我到目前为止的代码:

class Polynomial
{
private:
    double *coefficients; //this will be the array where we store the coefficients       
    int degree; //this is the degree of the polynomial (i.e. one less then the length of the array of coefficients)
public:
    Polynomial(); //the default constructor to initialize a polynomial equal to 0
    Polynomial(double coeffs[] , int nterms); //the constructor to initialize a polynomial with the given coefficient array and degree
    Polynomial(Polynomial&); //the copy constructor
    Polynomial(double); //the constructor to initialize a polynomial equal to the given constant
    ~Polynomial() { delete coefficients; } //the deconstructor to clear up the allocated memory
    //the operations to define for the Polynomial class
    Polynomial operator+(Polynomial p) const;
    Polynomial operator-(Polynomial p) const;
    Polynomial operator*(Polynomial p) const;
};
//This is  the default constructor 
Polynomial::Polynomial() {
    degree = 0;
    coefficients = new double[degree + 1];
    coefficients[0] = 0;
}
//Initialize a polynomial with the given coefficient array and degree
Polynomial::Polynomial(double coeffs[], int nterms){
    degree = nterms;
    coefficients = new double[degree]; //array to hold coefficient values
    for(int i = 0; i < degree; i++)
        coefficients[i] = coeffs[i];
}
Polynomial::Polynomial(Polynomial&){
}
//The constructor to initialize a polynomial equal to the given constant
Polynomial::Polynomial(double){
}
Polynomial::operator *(Polynomial p) const{
}
Polynomial::operator +(Polynomial p) const{
}
Polynomial::operator -(Polynomial p) const{
}
我只是想知道我是否在正确的轨道上,如果有更好的方法,请让我知道。如有任何意见或建议,我将不胜感激。

这不是一个完整的答案,但对您来说是一个起点。我使用std::set是因为它保持元素有序,所以我实现了一个函子并用于我的集合。现在,集合中的元素将根据比较函子排序。在当前的实现中,项将按照指数降序排序。

#include<set>
struct Term
{
    int coefficient;
    int exponent;
    Term(int coef, int exp) : coefficient{ coef }, exponent{ exp } {}
};
struct TermComparator
{
    bool operator()(const Term& lhs, const Term& rhs) {
        return lhs.exponent < rhs.exponent;
    }
};

class Polynomial
{
private:
    std::set<Term, TermComparator> terms;
public:
    Polynomial();
    ~Polynomial();
    Polynomial operator+(Polynomial p); 
};

我的实现允许你有效地存储高阶多项式。我已经为你实现了加法。从面向对象的角度来看,它并不是最好的,但是你可以重构它。

Polynomial Polynomial::operator+(Polynomial p) 
{
    auto my_it = terms.begin();
    auto p_it = p.terms.begin();
    Polynomial result;
    while (my_it != terms.end() && p_it != p.terms.end())
    {
        if (my_it->exponent > p_it->exponent)
        {
            result.terms.insert(*my_it);
            ++my_it;
        }
        else if (my_it->exponent == p_it->exponent)
        {
            result.terms.insert(Term(my_it->coefficient + p_it->coefficient, my_it->exponent));
            ++my_it;
            ++p_it;
        }
        else
        {
            result.terms.insert(*p_it);
            ++p_it;
        }
    }
    //only one of the for loops will be effective
    for (; my_it != terms.end(); ++my_it) result.terms.insert(*my_it);
    for (; p_it != p.terms.end(); ++p_it) result.terms.insert(*p_it);
    return result;
}

状态代码的

你的代码到目前为止是正确的,如果你说的nterms是指多项式的最大次数

实现多项式类的一个简单方法是使用双精度数组来存储系数。

这就是你所做的

数组的下标是对应项

的指数。

这就是为什么我告诉你数组大小等于度数+ 1这样就可以使用度(键)

访问系数(数组的值)

如果缺少一项,则它的系数为0。

请注意,在给出的示例中,x²不存在,但coefficients[2]在您的代码中存在并且等于0

提供这些操作:多项式+多项式,常数+多项式,多项式+常数,多项式-多项式,常数-多项式,多项式-常数。多项式*多项式,常数*多项式,多项式*常数,提供函数分配和提取系数,指数索引。提供一个函数,在double

类型的值处对多项式求值

正如你提到的,你错过了一些操作符重载。

更进一步

这里有一个非详尽的列表,当你完成这个练习时,可以做些什么来获得更多的c++经验:你可以实现一个表达式解析器(使用)-处理更复杂的多项式(如x²+ y²+ 2xy + 1)-使用map来存储你的系数(map在这个练习中可能不被认为是动态数组,但对你来说可能很有趣)或另一个数据结构来获取你系数中的零!(cf稀疏矩阵/数组技术)

祝你今后的学习愉快!