C++问题:定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn,该类具有如下的接口:class Polynomial { .public:Polynomial(); Polynomial
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/27 10:54:52
C++问题:定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn,该类具有如下的接口:class Polynomial { .public:Polynomial(); Polynomial
C++问题:定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn
定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn,该类具有如下的接口:
class Polynomial
{ .
public:
Polynomial();
Polynomial(double coefs[],int exps[],int size);
//系数数组、指数数组和项数
Polynomial(const Polynomial&);
Polynomial();
Polynomial& operator=(const Polynomial&);
int degree() const; //最高幂指数
double evaluate(double x) const; //计算多项式的值
bool operator==(const Polynomial&) const;
bool operator!=(const Polynomial&) const;
Polynomial operator+(const Polynomial&) const;
Polynomial operator-(const Polynomial&) const;
Polynomial operator*(const Polynomial&) const;
Polynomial& operator+=(const Polynomial&);
Polynomial& operator-=(const Polynomial&);
Polynomial& operator*=(const Polynomial&);
};
C++问题:定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn,该类具有如下的接口:class Polynomial { .public:Polynomial(); Polynomial
其实很简单
class Polynomial
{ double *pcoefs;
int *pexps;
int num_of_items;
int add(const Polynomial &p,double *coefs,int *exps) const;
int subtract(const Polynomial &p,double *coefs,int *exps) const;
public :
Polynomial();
Polynomial(double coefs[],int exps[],int size);
Polynomial(const Polynomial &p);
Polynomial();
Polynomial &operator=(const Polynomial &p);
int degree() const;
double evaluate(double x) const;
bool operator==(const Polynomial &p) const;
bool operator!=(const Polynomial &p) const;
Polynomial operator+(const Polynomial &p) const;
Polynomial operator-(const Polynomial &p) const;
Polynomial operator*(const Polynomial &p) const;
Polynomial &operator+=(const Polynomial &p);
Polynomial &operator-=(const Polynomial &p);
Polynomial &operator*=(const Polynomial &p);
};
int Polynomial::add(const Polynomial &p,double *coefs,int *exps) const
{ int count=0,i=0,j=0;
while (i