一元多项式的相减
程序代码:#include<stdio.h>
#include<malloc.h>
struct node
{
int coef;
int expn;
struct node * next;
}
typedef struct node polyn;
polyn *createList()
{
int i,c,d;
polyn *head,*p,*q;
p=head=(polyn *)malloc(sizeof(polyn));
p->coef=-1;
p->next=NULL;
for(i=1;;i++)
{ printf("请输入第%d项的系数以及指数\n",i);
scanf("%d,%d",&c,&d);
while((c!=0)||(d!=0))
{if(d<p->coef)
printf("输入错误\n");
if(c==0)
printf("系数不能为0!\n");
else
{q=(polyn *)malloc(sizeof(polyn));
q->coef=c;
q->expn=d;
q->next=NULL;
p->next=q;
p=q;
}
}
return(head);
}
polyn *subpolyn(polyn *ha,polyn *hb)
{
int i;
polyn *p,*q,*s,*t;
p=ha;
q=ha->next;
s=hb->next;
while((q!=0)&&(s!=0))
{
if((q->expn)<(s->expn))
{
p=q;
q=q->next;
}
if((q->expn)>(s->expn))
{
t=(polyn *)malloc(sizeof(polyn));
t->coef=s->coef;
t->expn=s->expn;
t->next=q;
p->next=t;
p=t;
s=s->next;
}
else
{
i=(q->coef)-(s->coef);
if(i==0)
{
p->next=q->next;
free(q);
q=p->next;
s=s->next;
}
else
{
p->coef=i;
p=q;
q=q->next;
s=s->next;
}
}
}
if(s!=NULL)
{q->next=s;}
return(ha);
}
void printpolyn(polyn *head)
{
polyn *p;
p=head->next;
while(p!=NULL)
{printf("--> <&d,&d>",p->coef,p->expn);
p=p->next;
}
}
main()
{
polyn *ha,*hb;
printf("intput ha:\n");
ha=createList();
printf("intput hb:\n");
hb=createList();
printf("output ha:\n");
printpolyn(ha);
printf("output hb:\n");
printpolyn(hb);
ha=subpolyn(ha,hb);
printf("多项式相减的结果\n");
printpolyn(ha);
}
求助!!!哪里出问题了?









