编写一个程序用单链表存储多项式并实现多项
编写一个程序用单链表存储多项式并实现多项式相加的函数四
/*文件名:作业.txt
文件描述:1道程序设计题目
创建人:王黎明
时间:2013.4.14
版本号:
*/
/*编写一个程序用单链表存储多项式并实现多项式相加的函数。*/
#include<iostream>
using namesace std;
struct oly{ //定义结构体
int coef,ex;
};
bool oerator<(oly&am; a,oly&am; b){ //重载小于号
return a.ex<b.ex;
}
/*void oerator=(oly&am; a,oly&am; b){ //???等号重载有问题不过用不到
a.ex=
b.ex;
a.coef=
b.coef;
}*/
class List; //链表
class Node{ //结点
oly info;
Node* link;
ublic:
Node(){link=NULL;}
Node(const oly&am; data){
link=NULL;
}
/* void oerator=(Node* n){ //重载等号info=n->info;
link=n->link;
}*/
friend class List;
};
class List{
Node *head,*tail;
ublic:
List(){head=tail=new Node();}
List(List&am; L){ //定义复制构造函数head=tail=new Node();
Node*tem1,*tem2;
oly ;
tem2=L.head->link;
while(tem2!=NULL)
{
=tem2->info;
tem1=CreatNode();
InsertRear(tem1);
tem2=tem2->link;
}
}
~List(){
MakeEmty();
delete head;}
void MakeEmty();
void rintList();
void InsertRear(Node *);
void makeorder();
List add(List &am; );
Node* CreatNode(oly data);
};
void List::MakeEmty(){
Node* tem;
while(head->link!=NULL){
tem=head->link;
head->link=tem->link;
}
tail=head;
}
void List::rintList(){
Node* tem;
int counter=-1;
tem=head->link;
while(tem!=NULL){
if(counter){
cout<<(tem->info).coef<<"x^"<<(tem->info).ex; counter++;
tem=tem->link;
}
else{
cout<<"+"<<(tem->info).coef<<"x^"<< ;(tem->info).ex;
tem=tem->link;
}
}
cout<<endl;
}
void List::InsertRear(Node* ){ //向后增加结点
->link=tail->link;
tail->link=;
tail=;
}
void
List::makeorder(){ //排序按照指数的由大到小但是为什么无法打印。
oly ;
Node* tem1,* tem2,tem3;
for(tem1=head->link;tem1!=NULL;tem1=tem1->link){ //逐个比较将大的结点赋给tem3。
for(tem2=tem1->link;tem2!=NULL;tem2=tem2->link){
if((tem1->info)<(tem2->info))
{ //已重载过oly类型的小于号
=tem1->info;
tem1->info=tem2->info;
tem2->info=;
}
}
//返回时貌似出现问题程序不能执行!
}
//重新构造当前链表。
List List::add(List&am; l){
//合并链表
makeorder(); //合并前先排序排序后通过逐个比较ex的大小进行合并。
l.makeorder();
List tl;
oly t;
Node* tn1,*tn2,*tn3;
tn1=head->link;
tn2=l.head->link;
while(tn1!=NULL){
while(tn2!=NULL){
if(tn1->info<tn2->info) {
tn3=tl.CreatNode(tn2->info);
tl.InsertRear(tn3);
tn2=tn2->link;
break;
}
if(tn2->info<tn1->info){
tn3=tl.CreatNode(tn1->info);
tl.InsertRear(tn3);
tn1=tn1->link;
break;
}
if(tn2->info.ex==tn1->info.ex){
t.coef=tn2->info.coef+tn1->info.coef;
t.ex=tn1->info.ex;
tn3=tl.CreatNode(t);
tl.InsertRear(tn3);
tn1=tn1->link;
tn2=tn2->link;
break;
}
if(tn2==NULL)break;
}
while(tn1==NULL&am;&am;tn2!=NULL){
//当有一个结束时将另一个剩余多项式接入
t=tn2->info;
tn3=tl.CreatNode(t);
tl.InsertRear(tn3);
tn2=tn2->link;
}
while(tn2==NULL&am;&am;tn1!=NULL){
t=tn1->info;
tn3=tl.CreatNode(t);
tl.InsertRear(tn3);
tn1=tn1->link;
}
return tl;
}
Node* List::CreatNode(oly data){ //创建结点Node* tem=new Node(data);
return tem;
}
int main(){ //主函数
oly 1,2;
Node * N1,*N2;
List l1,l2,l3;
int a[100],b[100],c[100],d[100],i,j,k;
cout<<"请分别输入两个多项式的项数(小于100个)"<<endl; cin>>i>>j;
cout<<"请输入第一个多项式的系数和指数"<<endl;
for(k=0;k<i;k++){
cin>>a[k];
cin>>b[k];
}
cout<<"第一个多项式为"<<endl;
for(k=0;k<i;k++){
1.coef=a[k];
1.ex=b[k];
N1=l1.CreatNode(1);
l1.InsertRear(N1);
}
l1.rintList();
cout<<"请输入第二个多项式的系数和指数"<<endl; for(k=0;k<j;k++){
cin>>c[k];
cin>>d[k];
}
cout<<"第二个多项式为"<<endl;
for(k=0;k<j;k++){
2.coef=c[k];
2.ex=d[k];
N2=l2.CreatNode(2);
l2.InsertRear(N2);
}
l2.rintList();
cout<<"排序后的两个多项式为"<<endl;
l1.makeorder();
l1.rintList();
l2.makeorder();
l2.rintList();
cout<<"合并后的两个多项式为"<<endl;
(l1.add(l2)).rintList();
return 0;
}