#include
using namespace std;
class Polynomial
{
double *coefs; //系数数组
int *exps; //指数数组
int size; //项数
public:
Polynomial() {coefs=NULL,exps=NULL,size=0;cout<<"构造完成"<
Polynomial(const Polynomial &p);
~Polynomial(){delete []coefs,delete []exps;coefs=NULL;exps=NULL;cout<<"吸构调用"<
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 &);
void display(); //测试函数 没有参数时候,退出全部程序
};
//测试函数
void Polynomial::display()
{cout<<"函数的项数为:"<
{cout<<"函数没有合适的值"<
}
cout<<"此函数为:"<
Polynomial::Polynomial(double coef[],int exp[],int s)
{coefs=new double[s];
exps=new int[s];
size=s;
for (int i=0;i
for (i=0;i
cout<<"complete!"<
Polynomial::Polynomial(const Polynomial &p)
{coefs=new double[p.size];
exps=new int[p.size];
size=p.size;
for (int i=0;i
for (i=0;i
cout<<"copy调用"<
Polynomial & Polynomial::operator=(const Polynomial &p) //完成测试
{ if(this!=&p)
{
delete []coefs;
delete []exps;
size=p.size;
coefs=new double[size];
exps=new int[size];
for (int i=0;i
for (i=0;i
}
return *this;
} //完成测试
int Polynomial:: degree() const //完成测试
{return size-1;
}
double Polynomial:: evaluate (double x) const //完成测试
{double sum=coefs[0];
for (int i=1;i
x*=x;
sum+=coefs[i]*x;
}
return sum;
}
bool Polynomial:: operator==(const Polynomial &p) const // 完成测试
{if(size!=p.size)
return false;
for (int i=0;i
return false;
return true;
}
bool Polynomial:: operator!=(const Polynomial &p) const //完成测试
{return !(operator==(p));
}
Polynomial Polynomial:: operator+(const Polynomial &p) const //完成测试
{int maxsize= (size>p.size)? size:p.size;
int minsize= (size
temp.coefs=new double[maxsize];
temp.exps=new int[maxsize];
for(int i=0;i
for (;i
for (int j=0;j
temp.size=maxsize;
return temp;
}
Polynomial Polynomial:: operator-(const Polynomial &p) const //完成测试
{int maxsize= (size>p.size)? size:p.size;
int minsize= (size
temp.coefs=new double[maxsize];
temp.exps=new int[maxsize];
for(int i=0;i
for (;i
for (int j=0;j
temp.size=maxsize;
return temp;
}
Polynomial Polynomial:: operator*(const Polynomial &p) const //已测试
{int tempsize=size+p.size-1;
Polynomial temp;
temp.coefs=new double[tempsize];
temp.exps=new int[tempsize];
for (int m=0;m
for (int i=0;i
for (int j=0,k=i;j<=i;j++,k--)
{
if(k>=p.size) continue;
tempsum+=coefs[j]*p.coefs[k];
}
//cout<<"tempsum"<
}
temp.size=tempsize;
return temp;
}
Polynomial& Polynomial:: operator+=(const Polynomial &p) //已测试
{Polynomial temp(*this+p);
this->~Polynomial();
*this=temp;
return *this;
}
Polynomial& Polynomial::operator-=(const Polynomial &p) //已测试
{Polynomial temp(*this-p);
this->~Polynomial();
*this=temp;
return *this;
}
Polynomial& Polynomial::operator*=(const Polynomial &p) //已测试
{Polynomial temp((*this)*p);
this->~Polynomial();
*this=temp;
return *this;
}
所有的代码已经通过测试。