#include<stdio.h>
#include<malloc.h>
typedef struct Polynode
{
int coef;
int exf;
struct Polynode *next;
}*Polylist;
void Init(Polylist *l)//初始化链表
{
*l =(Polylist)malloc(sizeof(Polynode));
(*l)->next = NULL;
}
void Creat(Polylist l)//用尾插法存入多项式的系数和指数
{
Polynode *r,*s;
r = l;
int c,e;
scanf("%d%d",&c,&e);
while(c != 0)
{
s = (Polynode*)malloc(sizeof(Polynode));
s->coef = c;
s->exf = e;
s->next = NULL;
r->next = s;
r = s;
scanf("%d%d",&c,&e);
}
}
Polylist Polyadd(Polylist la,Polylist lb)//两个多项式la、lb之和,结果存入多项式la中
{
Polynode *p,*q,*tail,*temp;
int sum;
p=la->next;
q=lb->next;
tail=la;
while(p!=NULL&&q!=NULL)
{
if(p->exf<q->exf)
{
tail->next=p;
tail=p;
p=p->next;
}
else if(p->exf==q->exf)
{
sum=p->coef+q->coef;
if(sum!=0)
{
p->coef=sum;
tail->next=p;
tail=p;
p=p->next;
temp=q;
q=q->next;
free(temp);
}
else
{
temp=p;
p=p->next;
free(temp);
temp=q;
q=q->next;
free(temp);
}
}
else
{
tail->next=q;
tail=q;
q=q->next;
}
}
if(p!=NULL)
tail->next=p;
else
tail->next=q;
return la;
}
Polylist Polylistdanmult(Polylist la,int coef,int exf)//单项式乘多项式
{
Polynode *s;
s=la->next;
while(s!=NULL)
{
s->coef=s->coef*coef;
s->exf=s->exf+exf;
s=s->next;
}
return la;
}
void Printf(Polylist l)//输出多项式的系数及指数
{
Polynode *p = l->next;
while(p != NULL)
{
printf("%d %d ", p->coef, p->exf);
p = p->next;
}
printf("\n");
}
Polylist Polylistduomult(Polylist la,Polylist lb)//多项式相乘
{
Polylist result,temp;
Init(&result);Init(&temp);
result->next=NULL;
Polynode *p,*q;
int c,e;
p=la->next;
q=lb->next;
c=p->coef;
e=p->exf;
while(p!=NULL)
{
temp=Polylistdanmult(lb,c,e);
result=(temp,result);
p=p->next;
c=p->coef;
e=p->exf;
}
return result;
}
int main()
{
Polylist la,lb,lc;
Init(&la);Init(&lb);Init(&lc);
Creat(la);Creat(lb);
lc=Polylistduomult(la,lb);
Printf(lc);
return 0;
}