麻烦大家帮我看下我写的一元多项式错在哪里,我想了好久都不知道。谢谢大家了!
#include <stdio.h>#include <math.h>
typedef struct LinkNode
{
float coef;
int index;
struct LinkNode *next;
}linknode;
void createlink(linknode *head)
{
int z;
linknode *p,*s;
p=head;
float a=1;
int n;
int b=1;
printf("请按多项式指数由高到低输入系数和指数:\n");
while(z)
{
scanf("%f %d",&a,&b);
if(fabs(a)>0.000001)
{
s=new linknode;
s->coef=a;
s->index=b;
s->next=NULL;
p->next=s;
p=s;
n++;
}
else z=0;
}
}
void show(linknode *head)
{
linknode *p;
p=head->next;
while(p&&p->next)
{
printf("\(%fx^%d\)+",p->coef,p->index);
p=p->next;
}
printf("\(%fx^%d\) ",p->coef,p->index);
printf("\n");
}
void addtion(linknode *fa,linknode *fb,linknode *fc) //把相加的结果放进一个新链表fc中
{
linknode *m,*n,*k;
m=fa->next;
n=fb->next;
k=fc->next;
while(m&&n)
{
if(m->index>n->index)
{
k=m;
m=m->next;
k=k->next;
}
else if(m->index<n->index)
{
k=n;
n=n->next;
k=k->next;
}
else
{
k->coef=m->coef+n->coef;
k->index=m->index;
m=m->next;
n=n->next;
k=k->next;
}
if(m)
k=m;
else
k=n;
}
}
void main()
{
linknode *fa,*fb,*fc;
fa=new linknode;
fb=new linknode;
fc=new linknode;
fa->next=NULL;
fb->next=NULL;
fc->next=NULL;
createlink(fa);
printf("fa(x)=");
show(fa);
createlink(fb);
printf("fb(x)=");
show(fb);
addtion(fa,fb,fc);
printf("fa(x)+fb(x)=");
show(fc);
}