6.3w+
社区成员
#include <iostream>
using namespace std;
//节点类的定义和实现
class linlist; //前视定义
class listnode
{
friend class linlist; //友元类linlist
public:
float co;
listnode *next;
int ex;
listnode()
{ next=NULL;}
listnode (const float &co1,const int &ex1)
{ co=co1;ex=ex1;next=NULL;}
listnode(const float &co1,const int &ex1,listnode *ptrnext)
{ co=co1;ex=ex1;next=ptrnext;}
~listnode(){}
};
//链表类的定义和实现
class linlist
{
public:
linlist();
~linlist();
void insert1(const float &item1,const int &item2,int pos);
void insert2(const float &item1,const int &item2,int pos);
void delnode(listnode *temp);
listnode *index1(int pos);
listnode *index2(int pos);
listnode *found(int ex1);
void add();
void display();
void display( listnode *head )
{
int i = 0;
if ( head )
{
while ( head = head->next )
{
if ( i != 0 )
cout << " + ";
cout << head->co << "*N^" << head->ex;
if ( ++i%10 == 0 )
cout << endl;
}
cout << endl;
}
}
//private:
listnode *head1,*head2,*head3;
int size1,size2;
};
linlist::linlist()
{
head1 = new listnode;
head2 = new listnode;
head3 = new listnode;
size1 = size2 = 0;
}
linlist ::~linlist()
{
listnode *p1,*p2,*q1,*q2,*p3,*q3;
q1 = p1 = head1->next;
q2 = p2 = head2-> next;
// 修改
while ( size1 > 0 )
{
p1 = p1-> next;
delete q1;
q1 = p1;
size1--;
}
// M
while( size2 > 0 )
{
p2=p2-> next;
delete q2;
q2=p2;
size2--;
}
// M 这里已经将head3 delete 了
p3=head3;
while(p3 !=NULL)
{
q3 = p3;
p3 = p3-> next;
delete q3;
}
delete head1;
delete head2;
}
void linlist::insert1(const float &item1,const int &item2,int pos)
{
listnode *p=index1(pos-1);
listnode *newnode = new listnode(item1,item2,p->next);
p->next=newnode;
size1++;
}
void linlist ::insert2(const float &item1,const int &item2,int pos)
{
listnode *p=index2(pos-1);
listnode *newnode = new listnode(item1,item2,p->next);
p->next=newnode;
size2++;
}
listnode * linlist::index1(int pos)
{
if( pos < 0 || pos>size1)
{
cout << " 参数pos越界出错,晕!!!" <<endl;
exit(0);
}
if(pos==0)
return head1;
listnode *p = head1->next;
for( int i = 1; i < pos; i++ )
p = p->next;
return p;
}
listnode * linlist::index2(int pos)
{
if( pos < 0 || pos > size2 )
{
cout << "参数pos越界出错,晕!!!" <<endl;
exit(0);
}
if( pos==0 )
return head2;
listnode *p = head2->next;
for( int i = 1; i < pos; i++ )
p=p->next;
return p;
}
listnode * linlist::found(int ex1)
{
listnode *p=head2;
// M
while(p->next)
{
if( p->next->ex == ex1 ) //??
return p;
p=p->next;
}
return NULL;
}
void linlist::delnode(listnode *temp)
{
// 有问题, 这个删除的不是 temp 而是 temp 的下一个Node ,建议把函数名改为 DelNextNode
//listnode *p = temp->next;
//temp->next = temp->next->next;
if ( temp )
{
if ( listnode *p = temp->next )
{
temp->next = p->next;
delete p;
}
}
}
void linlist ::add()
{
listnode *p3=head3;
listnode *p1=head1->next;
listnode *p2=head2->next;
while(p1 !=NULL && p2 !=NULL)
{
if( found(p1->ex) )
{
listnode *temp = found( p1->ex );
p3->next = new listnode( p1->co + temp->next->co, p1->ex );
p3=p3->next;
p1=p1->next;
delnode(temp);
p2 = head2->next;
size2--;
}
else
{
p3->next = new listnode( p1->co, p1->ex );
p1 = p1->next;
p3 = p3->next;
}
}
listnode *temp = (p1==0)?p2: p1;
while ( temp )
{
p3->next = new listnode( temp->co, temp->ex );
temp = temp->next;
p3 = p3->next;
}
}
void linlist::display()
{
listnode *p = head3->next;
while( p != NULL )
{
cout <<" ( " <<p->co <<" , " <<p-> ex <<" ) " <<endl;
p = p->next;
}
}
// 类的具体实现---多项式相加
int main()
{
int n,m;
linlist mylist;
cout << "请输入第一个多项式的项数n:" << endl;
cin >> n;
cout << "请输入第二个多项式的项数m:" << endl;
cin >> m;
for( int i = 1; i <= n; i++ )
{
cout << "请输入第一个多项式的第:" << i << "项" << endl;
float a1;
int b1;
cin >> a1 >> b1;
mylist.insert1(a1,b1,i);
}
mylist.display( mylist.head1 );
for( int j = 1; j <= m; j++ )
{
cout << "请输入第二个多项式的第:" << j << "项" << endl;
float a2;
int b2;
cin >> a2 >> b2;
mylist.insert2(a2,b2,j); //j,
}
mylist.display( mylist.head2 );
mylist.add();
cout << "两个多项式相加的结果为:";
mylist.display( mylist.head3 );
cout << endl;
return 0;
}
//编译可以了,不过结果不对,还有析构函数有问题,lz自己该吧,
#include <iostream>
using namespace std;
//节点类的定义和实现
class linlist; //前视定义
class listnode
{
friend class linlist; //友元类linlist
public:
float co;
listnode *next;
int ex;
listnode()
{ next=NULL;}
listnode (const float &co1,const int &ex1)
{ co=co1;ex=ex1;next=NULL;}
listnode(const float &co1,const int &ex1,listnode *ptrnext)
{ co=co1;ex=ex1;next=ptrnext;}
~listnode(){}
};
//链表类的定义和实现
class linlist
{
public:
linlist();
~linlist();
void insert1(const float &item1,const int &item2,int pos);
void insert2(const float &item1,const int &item2,int pos);
//void delnode(int pos);
void delnode(listnode *temp);
listnode *index1(int pos);
listnode *index2(int pos);
listnode *found(int ex1);
void add();
void display();
private:
listnode *head1,*head2,*head3;
int size1,size2;
};
linlist::linlist()
{
head1=new listnode;
head2=new listnode;
head3=new listnode;
size1=size2=0;
}
linlist ::~linlist()
{
listnode *p1,*p2,*q1,*q2;
q1=p1=head1->next;
q2=p2=head2->next;
while(size1 !=0)
{
p1=p1-> next;
delete q1;
q1=p1;
size1--;
}
while(size2 !=0)
{
p2=p2-> next;
delete q2;
q2=p2;
size2--;
}
}
void linlist::insert1(const float &item1,const int &item2,int pos)
{
listnode *p=index1(pos-1);
listnode *newnode=new listnode(item1,item2,p-> next);
p->next=newnode;
size1++;
}
void linlist ::insert2(const float &item1,const int &item2,int pos)
{
listnode *p=index2(pos-1);
listnode *newnode=new listnode(item1,item2,p-> next);
p-> next=newnode;
size2++;
}
listnode * linlist::index1(int pos)
{
if(pos <0 || pos> size1)
{
cout <<"参数pos越界出错,晕!!!" <<endl;
exit(0);
}
if(pos==0)
return head1;
listnode *p=head1-> next;
for(int i=1;i <pos;i++)
p=p-> next;
return p;
}
listnode * linlist::index2(int pos)
{
if(pos <0 || pos> size2)
{
cout <<"参数pos越界出错,晕!!!" <<endl;
exit(0);
}
if(pos==0)
return head2;
listnode *p=head2-> next;
for(int i=1;i <pos;i++)
p=p-> next;
return p;
}
listnode * linlist::found(int ex1)
{
listnode *p=head2;
while(p!=NULL)
{
if(p-> ex==ex1) //??
return p;
p=p-> next;
}
return p;
}
void linlist::delnode(listnode *temp)
{
listnode *p=temp-> next;
temp-> next=temp-> next-> next;
delete p;
}
void linlist ::add()
{
listnode *p3=head3;
listnode *p1=head1-> next;
listnode *p2=head2-> next;
while(p1 !=NULL && p2 !=NULL)
{
if(found(p1-> ex))
{
listnode *temp=found(p1->ex);
p3-> next=new listnode(p1-> co+temp-> next-> co,p1-> ex);
p3=p3-> next;
p1=p1-> next;
delnode(temp);
}
else
{
p3-> next=new listnode(p1-> co,p1-> ex);
p1=p1-> next;
p3=p3-> next;
}
}
if(p1)
p3-> next=p1;
else
p3-> next=p2;
}
void linlist::display()
{
listnode *p=head3-> next;
while(p!=NULL)
{
cout <<" ( " <<p->co <<" , " <<p->ex <<" ) " <<endl;
p=p->next;
}
}
// 类的具体实现---多项式相加
void main()
{
int n,m;
linlist mylist;
cout <<"请输入第一个多项式的项数n:" <<endl;
cin>> n;
cout <<"请输入第二个多项式的项数m:" <<endl;
cin>> m;
for(int i=1;i <=n;i++)
{
cout <<"请输入第一个多项式的第:" <<i <<"项" <<endl;
float a1;
int b1;
cin>> a1>> b1;
mylist.insert1(a1,b1,i);
}
for(int j=1;j <=m;j++)
{
cout <<"请输入第二个多项式的第:" <<j <<"项" <<endl;
float a2;
int b2;
cin>> a2>> b2;
mylist.insert2(a2,b2,j); //j,
}
mylist.add();
cout <<"两个多项式相加的结果为:";
mylist.display();
cout<<endl;
}