大侠帮我解决下,是求函数相加的,F(X1)+F(X)2=y
#include<malloc.h>typedef struct polynomial
{
int coef;
int expn;
struct polynomial *next;
}JD;
void printfpolyn(JD *pa)
{
while(pa)
{
printf("%dX^%d ",pa->coef,pa->expn);
pa=pa->next;
}
}
JD *creatpolyn(int n)
{
JD *l,*p;
l=(JD *)malloc(sizeof(JD));
l->next=NULL;
for(;n>0;n--)
{
p=(JD *)malloc(sizeof(JD));
scanf("%d,%d",&p->coef,&p->expn);
p->next=l->next;
l->next=p;
}
return(l);
}
int cmd(int a,int b)
{
if(a<b)
return(-1);
else if(a==b)
return(0);
else
return(1);
}
JD *Addpolyn(JD *pa,JD *pb)
{
JD *ha,*hb,*p,*heard;
int sum;
ha=pa;
hb=pb;
heard=pa=pa->next;
pb=pb->next;
while(pa&&pb)
{
switch(cmd(pa->expn,pb->expn))
{
case -1: ha=pa;pa=pa->next;break;
case 0: sum=pa->coef+pb->coef;
if(sum!=0)
{
pa->coef=sum;
ha=pa;
pa=pa->next;
}
else
{
ha->next=pa->next;
p=pa;
pa=pa->next;
free(p);
}
hb->next=pb->next;
p=pb;
pb=pb->next;
free(p);
break;
case 1: hb->next=pb->next;p=pb;ha->next=p;
p->next=pa;pb=pb->next;ha=ha->next;break;
}
}
if(pa)
ha=pb;
free(hb);
return(heard);
}
main()
{
int n;
JD *pa,*pb;
printf("\nshu ru pa xiang shu:");
scanf("%d ",&n);
pa=creatpolyn(n);
printf("\nshu ru pb xiang shu:");
scanf("%d ",&n);
pb=creatpolyn(n);
pa=Addpolyn(pa,pb);
printfpolyn(pa);
getch();
}
为什么求的 时候有错啊