| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 487 人关注过本帖
标题:[求助][急]后天就要交了,谁能告诉我这个多项式相加程序错在哪里?
只看楼主 加入收藏
reyes67
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-6-19
收藏
 问题点数:0 回复次数:0 
[求助][急]后天就要交了,谁能告诉我这个多项式相加程序错在哪里?

以下是关于一个两个多项式相加的程序,其中运用了链表来做,可我不知为何不能打乱指数顺序输入,只能按指数从大到少输入。如果要从小到大输入则只能输两个,否则就会花屏。请问有高人说给我听我错在哪里吗?谢谢指点!!!
#include<iostream.h>
struct number
{int shu;
int zhishu;
number*next;
};
number*creat()
{number *h,*q,*p,*temp,*pre;
h=NULL;
p=new number;
q=p;
cin>>p->shu>>p->zhishu;
while((p->zhishu>=0)&&(p->shu!=0))
{if(h==NULL) h=p;
else
{temp=h;pre=h;
while((temp->next!=NULL)&&(p->zhishu<temp->zhishu))
{pre=temp;temp=temp->next;}
if((p->zhishu>temp->zhishu)&&(temp==h))
{p->next=h;h=p;}
if((p->zhishu>temp->zhishu)&&(temp!=h))
{p->next=temp;pre->next=p;}
if((p->zhishu<temp->zhishu)&&(temp->next==NULL))
{temp->next=p;q=p;}
}
q->next=NULL;
p=new number;
cin>>p->shu>>p->zhishu;}
return h;
}
void destruct(number*h)
{number *p;
p=h;
while(h!=NULL)
{h=p->next;
delete p;
p=h;
}
}
void print(number*h)
{number *p;
p=h;
cout<<"\nThe duo xiang shi is:\n";
if(h==NULL) cout<<"The list is NULL!"<<endl;
else
{while(p!=NULL)
{if(p->zhishu==0) cout<<p->shu;
else {cout<<p->shu<<"X"<<"^"<<p->zhishu;}
p=p->next;
if(p->shu>0&&p!=NULL) cout<<"+";}
}
cout<<endl;
}
number *insert(number*h,number*num)
{number *p,*q,*temp;
temp=h;
q=num;
if(h==NULL)
{h=q;
q->next=NULL;
return h;
}
else
{while(q->zhishu<temp->zhishu&&temp->next!=NULL)
{p=temp;
temp=temp->next;
}
if(q->zhishu>temp->zhishu)
{if(h==temp)
{q->next=temp;h=q;}
else
{p->next=q;q->next=temp;}
}
else
{temp->next=q;q->next=NULL;}
}
return h;
}
long main()
{number *head1,*head2,*hc,*add,*p1,*p2;long r;char ch;
ch='y';hc=NULL;
while(ch=='y'||ch=='Y')
{cout<<"Please enter the first one:"<<endl;
head1=creat();
print(head1);
cout<<"Please enter the second one:"<<endl;
head2=creat();
print(head2);
p1=head1;p2=head2;
if(head1==NULL) hc=head2;
else
if(head2==NULL) hc=head1;
else
{while(p1!=NULL&&p2!=NULL)
{if(p1->zhishu==p2->zhishu)
{r=p1->shu+p2->shu;
if(r==0) {p1=p1->next;p2=p2->next;}
else
{add=new number;
add->zhishu=p1->zhishu;
add->shu=r;
hc=insert(hc,add);
p1=p1->next;p2=p2->next;
}
}
else
{if(p1->zhishu>p2->zhishu)
{add=new number;
add->zhishu=p1->zhishu;
add->shu=p1->shu;
hc=insert(hc,add);
p1=p1->next;
}
else
{add=new number;
add->zhishu=p2->zhishu;
add->shu=p2->shu;
hc=insert(hc,add);
p2=p2->next;
}
}
}
}
if(p1==NULL)
{while(p2!=NULL)
{add=new number;
add->zhishu=p2->zhishu;
add->shu=p2->shu;
hc=insert(hc,add);
p2=p2->next;
}
}
else
{while(p1!=NULL)
{add=new number;
add->zhishu=p1->zhishu;
add->shu=p1->shu;
hc=insert(hc,add);
p1=p1->next;
}
}
cout<<"------------"<<endl;
print(hc);
destruct(head1);
destruct(head2);
destruct(hc);
cout<<"----------------"<<endl;
cout<<"Continue?(Y--yes/N--no):";
cin>>ch;
}
return 0;
}


搜索更多相关主题的帖子: 多项式 number 相加 zhishu 
2007-06-19 01:16
快速回复:[求助][急]后天就要交了,谁能告诉我这个多项式相加程序错在哪里?
数据加载中...
 
   



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

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