求一份C++编写的多项式加减乘除的课程设计

2024年11月14日 15:08
有1个网友回答
网友(1):

#include
using namespace std;
class Polynomial
{
double *coefs; //系数数组
int *exps; //指数数组
int size; //项数

public:

Polynomial() {coefs=NULL,exps=NULL,size=0;cout<<"构造完成"<Polynomial(double coef[],int exp[],int );
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<<"函数的项数为:"<if(coefs==NULL||exps==NULL)
{cout<<"函数没有合适的值"<exit(-1);
}
cout<<"此函数为:"<for (int i=0;icout<cout<}

Polynomial::Polynomial(double coef[],int exp[],int s)
{coefs=new double[s];
exps=new int[s];
size=s;
for (int i=0;icoefs[i]=coef[i];
for (i=0;iexps[i]=exp[i];
cout<<"complete!"<//for (int n=0;n}

Polynomial::Polynomial(const Polynomial &p)
{coefs=new double[p.size];
exps=new int[p.size];
size=p.size;
for (int i=0;icoefs[i]=p.coefs[i];
for (i=0;iexps[i]=p.exps[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;icoefs[i]=p.coefs[i];
for (i=0;iexps[i]=p.exps[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{for (int j=1;j<=i;j++)
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;iif (coefs[i]!=p.coefs[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= (sizePolynomial temp;
temp.coefs=new double[maxsize];
temp.exps=new int[maxsize];
for(int i=0;itemp.coefs[i]=coefs[i]+p.coefs[i];
for (;itemp.coefs[i]=coefs[i];
for (int j=0;jtemp.exps[j]=j;
temp.size=maxsize;
return temp;
}

Polynomial Polynomial:: operator-(const Polynomial &p) const //完成测试
{int maxsize= (size>p.size)? size:p.size;
int minsize= (sizePolynomial temp;
temp.coefs=new double[maxsize];
temp.exps=new int[maxsize];
for(int i=0;itemp.coefs[i]=coefs[i]-p.coefs[i];
for (;itemp.coefs[i]=coefs[i];
for (int j=0;jtemp.exps[j]=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;mtemp.exps[m]=m;
for (int i=0;i{double tempsum=0;
for (int j=0,k=i;j<=i;j++,k--)
{
if(k>=p.size) continue;
tempsum+=coefs[j]*p.coefs[k];
}
//cout<<"tempsum"<temp.coefs[i]=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;
}
所有的代码已经通过测试。