当前位置: 代码迷 >> C语言 >> 吖[求助]多项式相加出现死循环
  详细解决方案

吖[求助]多项式相加出现死循环

热度:371   发布时间:2006-05-05 11:13:00.0
比如(5x^5+4x^4+…+1)+(4x^3+5x+8)=?这样吗?
我说的是具体要求,最好给个例子说明好些。
这样不明不白的,
----------------解决方案--------------------------------------------------------
一元多项式的相加:
Pn(X)=P0+P1X+P2X^2+....+PnX^n

----------------解决方案--------------------------------------------------------

做这题需要初中数学知识和结构体链表知识就够了


----------------解决方案--------------------------------------------------------
就是对结构体链表知识不熟悉出现问题了啥/
才要大家来帮我了/
----------------解决方案--------------------------------------------------------

/*
耐着性子,把LZ的程序"缩格排列"了。
楼主关注我拼命打//////的地方
*/
#include<stdio.h>
#define NULL 0

typedef struct LNode
{
int coef;
int expn;
struct LNode *next;
} term;

typedef struct
{
term *head,*tail;
int len;
} LinkList;

void InitTerm(term *P)
{
P->next=NULL;
}

void InitList(LinkList *L)
{
L->head=L->tail=NULL;
L->len=0;
}

void CreatPolyn(term *P,LinkList *L,int m)
{
term *S,*T;int i;
InitList(L);InitTerm(P);
T=L->head=P;
T->coef=0;
T->expn=-1;
for(i=1; i<=m; ++i)
{
S=(term *)malloc(sizeof(term));
InitTerm(S);
scanf("%d %d",&S->coef,&S->expn);
T->next=S;
free(S); ////////////////////////
T=T->next;
}
L->tail=T;
L->len=m;
}

void AddPolyn(term *Pa,LinkList *La,term *Pb,LinkList *Lb)
{
term *ha,*hb,*qa,*qb;
int n;
float sum;
ha=La->head;
hb=Lb->head;
qa=Pa->next;qb=Pb->next;
while (qa && qb)
{
if(qa->expn>qb->expn)n=1;
else if(qa->expn<qb->expn)n=-1;
else n=0;
switch(n)
{
case -1: ha=qa;qa=qa->next;break;
case 0: sum=qa->coef+qb->coef;
if(sum!=0.0)
{ qa->coef=sum;
ha=qa;qa=qa->next;
hb=qb;qb=qb->next;
}
else
{ ha->next=qa=qa->next;
hb->next=qb=qb->next;
}
break;
case 1: hb=qb;
qb=qb->next;
ha->next=hb;
qb->next=qa;
hb=qb;
qb=qb->next;
break;
}
}
if(!Pb->next)La->tail=Pb->next;
free(Pb);
}

void PrintPolyn(term *P)
{
term *h;
h=P->next;
while(h!=NULL)
{
printf("Y=");
printf("%dx(%d)+",h->coef,h->expn);
h=h->next;
}
printf("\b \n");
}

void main()
{
term *Pa,*Pb;
LinkList *La,*Lb;
int n;
Pa=(term *)malloc(sizeof(term));
Pb=(term *)malloc(sizeof(term));
La=(LinkList *)malloc(sizeof(LinkList));
Lb=(LinkList *)malloc(sizeof(LinkList));
puts("Now,Creat the first polynomail:");
puts("\nplease input the numbers of the first polynomail: ");
scanf("%d",&n);
puts("\nPlease input coef and expn each term with order from small to large by expn:\n");
CreatPolyn(Pa,La,n);
puts("the first polynomail is:\n");
PrintPolyn(Pa);
puts("Now,Creat the second polynamail:\n");
puts("please input the numbers of the second polynamail:\n");
scanf("%d",&n);
puts("\nPlease input coef and expn each term with order from small to large yb expn:\n");
CreatPolyn(Pb,Lb,n);
puts("the second polynomail is:\n"); PrintPolyn(Pb);
AddPolyn(Pa,La,Pb,Lb);
puts("the result is:\n");
PrintPolyn(Pa);
}


----------------解决方案--------------------------------------------------------
回复:LZ的程序"缩格排列"了。...
刚申请来的结点内存,马上就free( )了,谁教你这么做的?
----------------解决方案--------------------------------------------------------
呵呵,厉害,我链表学得很差很差,
那个金币的事情你让他给,别来找我哦,嘿嘿
----------------解决方案--------------------------------------------------------
以下是引用soft_wind在2006-5-5 13:23:00的发言:
呵呵,厉害,我链表学得很差很差,
那个金币的事情你让他给,别来找我哦,嘿嘿

我倒忘记金币了,再说这位仁兄,前脚malloc给了我,后脚又让我free给他,咋办?哈哈


----------------解决方案--------------------------------------------------------
for(i=1;i<=m;++i)
{
S=(term *)malloc(sizeof(term));
InitTerm(S);
scanf("%d %d",&S->coef,&S->expn);
T->next=S;
free(S); (我那个时候的意思是:先分配一个空间给变量S,赋值以后裢到裢表T中,                 
T=T->next;  释放掉S的空间.然后再重新分配一个空间给变量S,再赋值,再裢...)
}

----------------解决方案--------------------------------------------------------

还有一个问题:如果系数是浮点数又出问题了/
 出错的信息是:scanf:floating point formats not linked
Abnormal progrom termination
在这上面怎么弄图片上去啊!!


----------------解决方案--------------------------------------------------------
  相关解决方案