gpt4 book ai didi

c++ - 多项式类

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:22:34 26 4
gpt4 key购买 nike

这是我要解决的问题:

Using dynamic arrays, implement a polynomial class with polynomial addition, subtraction, and multiplication. Discussion: A variable in a polynomial does nothing but act as a placeholder for the coefficients. Hence, the only interesting thing about polynomials is the array of coefficients and the corresponding exponent. Think about the polynomial xxx + x + 1 Where is the term in x*x ? One simple way to implement the polynomial class is to use an array of doubles to store the coefficients. The index of the array is the exponent of the corresponding term. If a term is missing, then it simply has a zero coefficient. There are techniques for representing polynomials of high degree with many missing terms. These use so-called sparse matrix techniques. Unless you already know these techniques, or learn very quickly, do not use these techniques. Provide a default constructor, a copy constructor, and a parameterized constructor that enables an arbitrary polynomial to be constructed. Supply an overloaded operator = and a destructor. Provide these operations: polynomial + polynomial, constant + polynomial, polynomial + constant, polynomial - polynomial, constant - polynomial, polynomial - constant. polynomial * polynomial, constant * polynomial, polynomial * constant, Supply functions to assign and extract coefficients, indexed by exponent. Supply a function to evaluate the polynomial at a value of type double . You should decide whether to implement these functions as members, friends, or standalone functions.

这不是为了上课,我只是想自学 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 因为它保持其元素有序,所以我实现了一个仿函数并用于我的集合。现在,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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com