两个一元多项式相加
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/30 07:05:24
两个一元多项式相加
两个一元多项式相加
两个一元多项式相加
polynode * addpolynomial(polynode *f,polynode *g)
//多项式相加
{
polynode * fg;
polynode *t,*q,*s,*r;
float m;
t=f->next;
q=g->next;
fg=r=(polynode*)malloc(sizeof(polynode));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn) //指数相等时系数相加
{
m=t->coef+q->coef;
if(m!=0) //系数为不0时加到结果中去
{
s=(polynode *)malloc(sizeof(polynode));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else //指数小的加到结果中去再后移
if(t->expnexpn)
{
s=(polynode *)malloc(sizeof(polynode));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(polynode *)malloc(sizeof(polynode));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}
if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}//while
r->next=t?t:q; //把没加完的接上
return fg;
}