c++多项式的计算(单项链表的应用)设计处理n次一元多项式P(x) = a0 + a1x + a2x2 + … + anxn,

谢谢各位了 !时间紧急!3Q!!!!
2024-11-06 13:27:42
推荐回答(2个)
回答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;i 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 coefs[i]=coef[i];
for (i=0;i exps[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;i coefs[i]=p.coefs[i];
for (i=0;i exps[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;i coefs[i]=p.coefs[i];
for (i=0;i exps[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;i if (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= (size Polynomial temp;
temp.coefs=new double[maxsize];
temp.exps=new int[maxsize];
for(int i=0;i temp.coefs[i]=coefs[i]+p.coefs[i];
for (;i temp.coefs[i]=coefs[i];
for (int j=0;j temp.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= (size Polynomial temp;
temp.coefs=new double[maxsize];
temp.exps=new int[maxsize];
for(int i=0;i temp.coefs[i]=coefs[i]-p.coefs[i];
for (;i temp.coefs[i]=coefs[i];
for (int j=0;j temp.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;m temp.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;
}
所有的代码已经通过测试。

回答2:

#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;
}