多项式相加(请求修改)

lanowen 2008-03-15 06:15:35
//节点类的定义和实现
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
{ private:
listnode *head1,*head2,*head3;
int size1,size2;
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);
listnode *index1(int pos);
listnode *index2(int pos);
listnode *found(int ex1);
void add();
void display();
};

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--;
}
}

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++;
}

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->next->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;
}
}


// 类的具体实现---多项式相加
#include "stdlib.h"
#include <iostream.h>
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,i);
}
mylist.add();
cout<<"两个多项式相加的结果为:"<<mylist.display()<<endl;
}



...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wujun6042 2008-06-10
  • 打赏
  • 举报
回复
还是不明白,要进一步的学习!!1
夜雨_倚琴 2008-03-16
  • 打赏
  • 举报
回复
总的来说,主要是你的Add函数和析构函数不一致的问题.

#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;
}
lanowen 2008-03-16
  • 打赏
  • 举报
回复
这个编译和结果都运行正确了,但是每次执行结果后都是什么需要调试????高手请指教
//节点类的定义和实现
#include<stdlib.h>
#include <iostream.h>
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
{ private:
listnode *head1,*head2,*head3;
int size1,size2;
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();
};

linlist ::linlist()
{ head1=new listnode();
head2=new listnode();
head3=new listnode();
size1=size2=0;
}

linlist ::~linlist()
{
listnode *p1,*p2,*p3,*q1,*q2,*q3;
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--;
}
p3=q3=head3->next;
while(p3 !=NULL)
{
p3=p3->next;
delete q3;
q3=p3;
}
delete head1;
delete head2;
delete head3;
}

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->next!=NULL)
{
if(p->next->ex==ex1)
return p;
p=p->next;
}
return p->next;
}

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)!=NULL)
{
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;
}
}
p2=head2->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);
}
mylist.add();
cout<<" 两个多项式相加的结果为:"<<endl;
mylist.display();

}



lanowen 2008-03-16
  • 打赏
  • 举报
回复
//编译可以了,可是我改了很久,也就是这个样子了,不知道哪里错了,要调试,麻烦高手帮帮忙
#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);
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,*p3,*q3;
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--;
}
p3=q3=head3->next;
while(p3 !=NULL)
{
p3=p3->next;
delete q3;
q3=p3;
}
delete head1;
delete head2;
delete head3;
}

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->next->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;
}
ryfdizuo 2008-03-15
  • 打赏
  • 举报
回复

//编译可以了,不过结果不对,还有析构函数有问题,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;
}

64,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧