这是我要解决的问题:
使用动态数组,通过多项式加法,减法和乘法来实现多项式类。讨论:多项式中的变量仅充当系数的占位符。因此,关于多项式的唯一有趣的事情是系数的数组和相应的指数。考虑多项式xxx + x + 1 x * x中的项在哪里?实现多项式类的一种简单方法是使用双精度数组来存储系数。数组的索引是相应项的指数。如果缺少一项,则其系数为零。有一些技术可以表示缺少多项的高阶多项式。这些使用所谓的稀疏矩阵技术。除非您已经知道这些技术或学得很快,否则请不要使用这些技术。提供默认构造函数,副本构造函数和参数化构造函数,以构造任意多项式。提供一个重载的运算符=和一个析构函数。提供以下运算:多项式+多项式,常数+多项式,多项式+常数,多项式-多项式,常数-多项式,多项式-常数。多项式*多项式,常数*多项式,多项式*常数,提供函数以分配和提取系数,由指数索引。提供一个函数来评估类型为double的多项式。您应该决定是将这些功能实现为成员,好友还是独立功能。
这不是为了上课,我只是想自学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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句