| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1016 人关注过本帖
标题:链表求多项式相加,有程序代码 ! 问题出在那了?-->wfpb转移
只看楼主 加入收藏
ylwuwei
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-2
收藏
 问题点数:0 回复次数:4 
链表求多项式相加,有程序代码 ! 问题出在那了?-->wfpb转移
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int datatype;
typedef struct node
{
float coef; /*多项式系数*/
int expn; /*多项式指数*/
struct node *next;
}listnode;
typedef listnode *linklist;
/*---------创建带头结点的多项式链表--------*/
linklist creat()
{ linklist head,s,p,pre;
float coef;
int expn;
head=(linklist)malloc(sizeof(listnode)); /*表头结点*/
head->next=NULL;
cout<<"输入系数:";
cin>>coef;
cout<<endl<<"输入指数:";
cin>>expn;
while (coef!=0)
{ cout<<"用系数=0作为结束标记!"<<endl;
s=(linklist)malloc(sizeof(listnode)); /*生成新结点*/

s->coef=coef;
s->expn=expn;
s->next=NULL;
pre=head; /*插入到有序的多项式链表中去*/
p=head->next;
while (p && p->expn <expn)
{ pre=p;
p=p->next;
}
s->next=p;
pre->next=s;
cout<<"读下一项:";
cout<<endl<<"输入系数:";
cin>>coef;
cout<<endl<<"输入指数:";
cin>>expn;
}
return head;
} /*-----------输出多项式链表-------------*/
void print(linklist head)
{ linklist p;
p=head->next;
while (p)
{
cout<<p->coef<<"X^"<<p->expn<<"+";
p=p->next;

}
cout<<endl;
}
/*-------------多项式相加----------------*/
linklist add(linklist pa,linklist pb)
{ linklist p,q,pre,r,head;
float x;
p=head=pa->next; //p,q 指向头结点的下一个接点,即多项式的第一个接点
q=pb->next;
pre=pa; //pre指向p的前驱
while((p!=NULL)&&(q!=NULL)) //处理多项式的相加的问题
if(p->expn<q->expn)
{
pre=p;
p=p->next;
}
else if (p->expn==q->expn)
{
x=p->coef+q->coef;
if(x!=0) //系数相加不为0的情况
{
p->coef=x;
pre=p;
//p=p->next;
}
else //系数相加为0的情况
{
pre->next=p->next;
free(p);
p=pre->next;
}

r=q;
q=q->next;
free(r);
}
else
{
r=q->next;
q->next=p;
pre->next=q;
pre=q;
q=r;
}
if(q!=NULL)
pre->next=q;
return head;
free(pb);
}
/*----主程序------*/
void main()
{
cout<<"*****************一元多项式相加******************"<<endl;
cout<<" !!!!2x^3表示 2乘以x的三次方~!!!!"<<endl;
cout<<"**************************************************"<<endl;
linklist a,b,c;
cout<<"请输入第一个多项式:"<<endl;
a=creat(); /*创建多项式链表a*/
cout<<endl;



cout<<"请输入第二个多项式:"<<endl;
b=creat(); /*创建多项式链表b*/
cout<<endl;
cout<<"您输入的第一个多项式为:"<<endl;
print(a);

cout<<"您输入的第二个多项式为:"<<endl;
print(b);
cout<<endl;


cout<<"两多项式相加后为:"<<endl;

c=add(a,b); /* 计算多项式a+b */
print(c);


cout<<"程序运行完啦,不过有发现问题了吗?第一项丢了-_-!"<<endl;
}
源程序如上,运行后第一项就不见了?
为什么?
怎么改?
请高手指点!!!
急啊!!!
搜索更多相关主题的帖子: 链表 多项式 wfpb 相加 
2007-04-06 10:04
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

问题很多啊!还不如重新写一次,先整理一下思路再写


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-04-06 12:20
ylwuwei
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-2
收藏
得分:0 

我重新编的:
#include<iostream.h>
#include<malloc.h>
#define len sizeof(JD)
typedef struct polyJD
{
float coef; //项系数
int expn; //项系数
struct polyJD *next; //下一个结点
}JD;
/** 建立多项式链表 */
JD *create(void)
{
JD *h,*r,*s;
float c;
int e;
h=(JD *)malloc(len);
r=h;
cout<<"输入系数:";
cin>>c;
cout<<"输入指数:";
cin>>e;
while(c!=0)
{
s=(JD *)malloc(len);
s->coef=c;
s->expn=e;
r->next=s;
r=s;
cout<<"输入下一项"<<endl;
cout<<"输入系数:";
cin>>c;
cout<<"输入指数:";
cin>>e;
}
r->next=NULL;
return(h);
}
/* 多项式相加 */
void add_poly(JD *pa,JD *pb)
{ JD *p,*q,*u,*pre;
float x;
p=pa->next;
q=pb->next;
pre=pa;
while((p!=NULL) && ((q!=NULL)))
{ if(p->expn<q->expn)
{ pre=p; p=p->next;}
else if(p->expn==q->expn)
{ x=p->coef+q->coef;
if(x!=0){ p->coef=x; pre=p;}
else { pre->next=p->next; free(p);}
p=pre->next;
u=q;
q=q->next;
free(u);
}
else
{ u=q->next;q->next=p;pre->next=q;
pre=q; q=u;
}
}
if(q!=NULL)
pre->next=q;
free(pb);
}
/* 输入出多项式链表 */
void print(JD * p)
{
while(p->next!=NULL)
{
p=p->next;
cout<<p->coef<<"X^"<<p->expn<<" + ";
}
cout<<endl;
}
void main()
{
JD * pa,* pb;
char m;
do
{
cout<<"请输入系数和指数,指数从小到大输入,";
cout<<"系数为0时结束"<<endl;
pa=create();
cout<<"输入的多项式为: ";
print(pa);
cout<<"请输入系数和指数,指数从小到大输入,";
cout<<"系数为0时结束"<<endl;
pb=create();
cout<<"输入的多项式为: ";
print(pb);
cout<<"两多项式的和为 ";
add_poly(pa,pb);
print(pa);
cout<<endl;
}
while(m=='y');
}
有点搞不懂这句什么意思:
while(m=='y');
这个循环终止条件是什么意思?
请高手指点一二
小弟谢过了!!!


为了明天的美好! 努力,发奋!!!
2007-04-06 13:12
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
你自己写的?怎么自己都不知道自己的意思

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-04-06 14:35
ylwuwei
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-2
收藏
得分:0 

最后那个我是试出来的!


为了明天的美好! 努力,发奋!!!
2007-04-06 22:23
快速回复:链表求多项式相加,有程序代码 ! 问题出在那了?-->wfpb转移
数据加载中...
 
   



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

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