啊哈磊_编程从这里起步
标题:
可以帮我看看哪里出了问题吗/
[打印本页]
作者:
13711239300
时间:
2020-12-28 12:09
标题:
可以帮我看看哪里出了问题吗/
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
float coef;
int exp;
struct node * next;
}PolyNode;
void CreateListR(PolyNode * &L,double a[],int b[],int n)
{
PolyNode *s,*tc;
int i;
L=(PolyNode * )malloc(sizeof(PolyNode));
tc=L;
for(i=0;i<n;i++)
{
s=(PolyNode * )malloc(sizeof(PolyNode));
s-> coef=a[i];
s-> next=b[i];
tc-> next=s;
tc=s;
}
tc->next=NULL;
}
void DestoryList(PolyNode * &L)
{
PolyNode * pre=L,*p=pre->next;
while (p!=NULL)
{ free(pre);
pre=p;p=p->next;
}
free(pre);
}
void DisPoly(PolyNode *L)
{ PolyNode *p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;p=p->next;
}
free(pre);
}
void DispPoly(PolyNode *L)
{ PolyNode *p=L->next;
while(p!=NULL)
{
printf("(%gx^%d)",p->coef,p->exp);
p=p->next;
}
printf("");
}
void Add(PloyNode * ha,PolyNode * hb,PolyNode * &hc)
{ PolyNode * pa=ha-> next,*pb=hb->next,*s,*tc;
double c;
hc=(PolyNode * )malloc(sizeof(PloyNode));
tc=hc;
while(pa!=NULL&&pb!=NULL)
{ if (pa->exp>pb->exp)
{ s=(PolyNode * )malloc(sizeof(PolyNode));
s->exp=pa->exp;s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
else if (pa->exp<pb->exp)
{ s=(PolyNode * )malloc(sizeof(PolyNode));
s->exp=pb->exp;s->coef=pb->coef;
tc->next=s; tc=s;
pb=pb->next;
}
else
{ c=pa->coef+pb->coef;
if (c!=0)
{ s=(PolyNode * )malloc(sizeof(PolyNode));
s->exp=pa->exp;s->coef=c;
tc->next=s;tc=s;
}
pa=pa->next;
pb=pb->next;
}
}
if(pb!=NULL) pa=pb;
while (pa!=NULL)
{ s=(PolyNode * )malloc(sizeof(PolyNode));
s->exp=pa->exp;
s->coef=pa->coef;
tc->next=s;tc=s;
pa=pa->next;
}
tc->next=NULL;
}
void main()
{ PolyNode *poly1,*poly2,*poly3;
double a[MAX];
int b[MAX];
//----创建第一个多项式单链表----
a[0]=3.2; b[0]=5;
a[1]=2.0; b[1]=3;
a[2]=-6.0; b[2]=0;
a[3]=10.0; b[3]=0;
n=4;
CreatListR(Poly1,a,b,n);
printf("第一个多项式:");DosPoly(Poly1);
//----创建第二个多项式单链表----
a[0]=1.8; b[0]=5;
a[1]=-2.5; b[1]=4;
a[2]=-2.0; b[2]=3;
a[3]=1.0; b[3]=2;
a[4]=6.0; b[4]=1;
a[5]=-5.0; b[5]=0;
n=6;
CreatListR(poly2,a,b,n);
printf("第二个多项式:");DispPoly(Poly2);
Add(Poly1,Poly2,Poly3);
printf("相加h后多项式:");DispPoly(Poly3);
DestoryList(Poly1);
DestoryList(Poly2);
DestoryList(poly3);
}
复制代码
欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/)
Powered by Discuz! X3.2