找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 614|回复: 0

[求助][急]后天就要交了,谁能告诉我这个多项式相加程序错在哪里?

[复制链接]
发表于 2009-11-6 00:56:55 | 显示全部楼层 |阅读模式 IP:江苏扬州
以下是关于一个两个多项式相加的程序,其中运用了链表来做,可我不知为何不能打乱指数顺序输入,只能按指数从大到少输入。如果要从小到大输入则只能输两个,否则就会花屏。请问有高人说给我听我错在哪里吗?谢谢指点!!!
#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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 15:29 , Processed in 0.234565 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表