| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1438 人关注过本帖
标题:用链表实现多项式相加
只看楼主 加入收藏
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
 问题点数:0 回复次数:8 
用链表实现多项式相加
各位大哥大姐,请帮忙看看我的这个程序那有错误!!!!急用
#define NULL 0
struct term
{int coef;
int expn;
struct term *next;
};
int n;
struct term *creat(void)
{struct term *La;
struct term *p1,*p2;
n=0;
p1=p2=(struct term *)malloc(sizeof(struct term));
scanf("%d,%d",&p1->coef,&p1->expn);
La=NULL;
while(p1->coef!=0)
{n=n+1;
if(n==1)La=p1;
else p2->next=p1;
p2=p1;
p1=(struct term *)malloc(sizeof(struct term));
scanf("%d,%d",&p1->coef,&p1->expn);
}
p2->next=NULL;
return(La);
}
void print(struct term *La)
{struct term *p;
printf("\nNOW,there %d items are :\n",n);
p=La;
if(La!=NULL)
do
{printf("%d%d",p->coef,p->expn);
p=p->next;}while(p!=NULL);
}
void sort(struct term *La)
{struct term *q,*p,*r;
int i,j,temp,ts,min,max;
p=La;
q=p->next;
while(p!=NULL)
{max=p->coef;
min=p->expn;
r=p;
while(q!=NULL)
{if(min>q->expn)
{max=q->coef;min=q->expn;r=q;}
q=q->next;}
temp=p->expn;ts=p->coef;
p->expn=r->expn;p->coef=r->coef;
r->expn=temp;r->coef=ts;
p=p->next;
q=q->next;
}
}
void *add(struct term *La,struct term *Lb)
{struct term *p1,*p2,*mid;
p1=La->next;p2=Lb->next;mid=Lb->next;
while(p1&&p2)
{if(p1->expn<p2->expn)
{La=p1;p1=p1->next;}
else if(p1->expn==p2->expn)
{p1->coef=p1->coef+p2->coef;mid=mid->next;
if(p1->coef==0)
{La->next=p1->next;free(p1);}
mid=p2;Lb->next=p2->next;free(p2);
p2=p2->next;p1=p1->next;}
else if(p1->expn>p2->expn)
{Lb->next=p2->next;La->next=p2;
p2->next=p1;mid=p2;
p2=p2->next;p1=p1->next;}
}
if(Lb)p1->next=p2;free(p2);
return(La);
}
main()
{struct term *La,*Lb;
printf("\ninput the La:\n" );
La=creat();
print(La);
printf("\ninput the Lb:\n");
Lb=creat();
print(Lb);
sort(La);
print(La);
sort(Lb);
print(Lb);
add(La,Lb);
print(La);

}

搜索更多相关主题的帖子: term 链表 多项式 struct int 
2006-05-05 17:14
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
我晕, 你都不说你程序有什么问题,我们怎么给你看,就好比你说你的飞机上小了个零件,没说什么部位,那叫我们怎么找

日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-05-05 19:18
仁者无敌
Rank: 1
等 级:新手上路
帖 子:199
专家分:0
注 册:2006-3-5
收藏
得分:0 
在我这儿运行通过!

I am a programmer !
2006-05-05 19:37
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
得分:0 
运行的结果是前半部分出现想要的结果,而后半部分却出现NULL POINTER ASSIGMENT,这是怎么回事呀!
2006-05-05 22:19
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
得分:0 

怎么得不到我想要的结果呀

2006-05-05 22:26
韦应贵
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-4-30
收藏
得分:0 
哦,对不起,大哥大姐,我最近运行了一下,好像是我的链表排序出现错误码,请大哥大姐们帮忙看一下错在那个,顺便修改一下吧,小弟我先谢了.
2006-05-05 22:37
awindy
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2006-5-4
收藏
得分:0 
   你要具体说错在哪里我们才好改呀!!

你的过去不是你的潜力,伟大的成就是干出来的,而不是想出来的!
2006-05-06 11:25
快速回复:用链表实现多项式相加
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014106 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved