c++ - 多项式类

扫码查看

这是我试图解决的问题:



这不是为了上课,我只是想自学 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);
};

我的实现允许您有效地存储高阶多项式。
我已经为你实现了加法。就 OOP 而言,它不是最佳状态,但您可以重构它。
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;
}

关于c++ - 多项式类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30456982/

10-11 19:33
查看更多