#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;
}
所有的代码已经通过测试。
#include
#include
struct Polyn
{
int xishu;
int zhishu;
Polyn *next;
};
Polyn *CreatPolyn()
{
Polyn *Head , *p1 , *p2;
printf("按降幂依次输入多项式每项的系数和指数(当输入的系数为0时结束输入):\n");
Head = (Polyn *)malloc(sizeof(Polyn));
p1 = p2 = Head;
Head->next = NULL;
while(1)//系数和指数均不为0时结束输入
{
p2 = p1;
p1 = (Polyn *)malloc(sizeof(Polyn));
scanf("%d%d" , &p1->xishu , &p1->zhishu);
p2->next = p1;
if(p1->xishu == 0)
break;
}
p1->next = NULL;
return Head;
}
int AddPolyn(Polyn *La , Polyn *Lb)//多项式相加
{
Polyn *Ha,*Hb,*pa,*pb;
Ha = La;
Hb = Lb;
pa = Ha->next;
pb = Hb->next;
while(pa->xishu && pb->xishu)
{
if(pa->zhishu == pb->zhishu)
{
if((pa->xishu + pb->xishu) == 0)
{
Ha->next = pa->next;
pa = pa->next;
Hb->next = pb->next;
pb = pb->next;
}
else
{
pa->xishu += pb->xishu;
Hb->next = pb->next;
pb = pb->next;
}
}
else if(pa->zhishu > pb->zhishu)
{
Ha = pa;
pa = pa->next;
}
else if(pa->zhishu < pb->zhishu)
{
Ha->next = pb;
Ha = pb;
Hb->next = pb->next;
pb = pb->next;
Ha->next = pa;
}
}
if(pb->xishu)
Ha->next = pb;
return 0;
}
int main()
{
Polyn *La , *Lb , *p;
La = CreatPolyn();
Lb = CreatPolyn();
AddPolyn(La , Lb);
printf("相加得到的多项式为:\n");
p = La->next;
while(p->xishu)
{
printf("%d %d\n" , p->xishu , p->zhishu);
p = p->next;
}
free(La);
free(Lb);
system("pause");
return 0;
}