用链表实现两个多项式相加
发布网友
发布时间:2022-05-20 13:25
我来回答
共1个回答
热心网友
时间:2023-11-16 06:16
#include<iostream.h>
class Linkedlist;
class Node
{
friend class Linkedlist;
private:
Node *next;
public:
float coef;
int exp;
Node(Node *ptrNext=NULL);
Node(float a,int b,Node *ptrNext=NULL);
};
Node::Node(Node *ptrNext):next(ptrNext){}
Node::Node(float a,int b,Node *ptrNext)
{
coef=a;
exp=b;
next=ptrNext;
}
class Linkedlist
{
private:
Node *head;
int size;
Node *currPtr;
public:
Linkedlist();
~Linkedlist();
void Create();
int ListSize();
void Add(Linkedlist &s1,Linkedlist &s2);
void Insert(float a,int b,int pos);
Node *Index(int pos);
void ClearList();
float GetCoef(int pos);
int GetExp(int pos);
Node *Next();
};
Linkedlist::Linkedlist()
{
head=new Node();
size=0;
}
Linkedlist::~Linkedlist()
{
ClearList();
delete head;
head=NULL;
}
void Linkedlist::Create()
{
Node *ptr=head->next;
float a;
int b,i=0,pos=0;
cout<<"请输入多项式各项的系数和指数,系数输入'0'结束"<<endl;
while(1)
{
cin>>a>>b;
if(a!=0)
{
while(ptr->exp<b&&ptr->next!=NULL)
{
ptr=Index(pos-1);
pos++;
}
ptr=Index(pos-1);
Node *qtr=new Node(a,b,ptr->next);
ptr->next=qtr;
size++;
}
else
break;
}
}
Linkedlist::ListSize()
{
return size;
}
void Linkedlist::Add(Linkedlist &s1,Linkedlist &s2)
{
int i=0,m=0,n=0;
while(s1.Next()&&s2.Next())
{
if(s1.GetExp(m)>s2.GetExp(n))
{
Node *ptr=Index(i-1);
Node *qtr=new Node(s2.GetCoef(n),s2.GetExp(n),ptr->next);
ptr->next=qtr;
size++;
n++;
i++;
s2.Next();
}
if(s1.GetExp(m)==s2.GetExp(n))
{
if(s1.GetCoef(m)+s2.GetCoef(n)!=0)
{
Node *ptr=Index(i-1);
Node *qtr=new Node(s1.GetCoef(m)+s2.GetCoef(n),s1.GetExp(m),ptr->next);
ptr->next=qtr;
size++;
m++,n++,i++;
s1.Next();
s2.Next();
}
else
{
m++,n++;
s1.Next();
s2.Next();
}
}
if(s1.GetExp(m)<s2.GetExp(n))
{
Node *ptr=Index(i-1);
Node *qtr=new Node(s1.GetCoef(m),s1.GetExp(m),ptr->next);
ptr->next=qtr;
size++;
m++,i++;
s1.Next();
}
}
if(s1.Next())
{
Node *ptr=Index(i-1);
Node *qtr=new Node(s1.GetCoef(m),s1.GetExp(m),ptr->next);
ptr->next=qtr;
size++;
m++,i++;
s1.Next();
}
else
if(s2.Next())
{
Node *ptr=Index(i-1);
Node *qtr=new Node(s2.GetCoef(n),s2.GetExp(n),ptr->next);
ptr->next=qtr;
size++;
i++,n++;
s2.Next();
}
}
void Linkedlist::Insert(float a,int b,int pos)
{
Node *ptr=Index(pos-1);
Node *qtr=new Node(a,b,ptr->next);
ptr->next=qtr;
size++;
}
Node *Linkedlist::Index(int pos)
{
if(pos<-1||pos>size-1)
{
cout<<"下标越界:"<<endl;
return NULL;
}
if(pos==1) return head;
Node *p=head->next;
int i=0;
while(i<pos)
{
p=p->next;
i++;
}
return p;
}
void Linkedlist::ClearList()
{
Node *p,*pl;
p=head->next;
while(p!=NULL)
{
pl=p;
p=p->next;
delete pl;
}
size=0;
}
float Linkedlist::GetCoef(int pos)
{
Node *p=Index(pos);
return p->coef;
}
int Linkedlist::GetExp(int pos)
{
Node *p=Index(pos);
return p->exp;
}
Node *Linkedlist::Next()
{
if(currPtr!=NULL)
currPtr=currPtr->next;
return currPtr;
}
void Print(Linkedlist &L)
{
int i,length=L.ListSize();
for(i=0;i<length;i++)
{
if(L.GetExp(i)==0)
cout<<L.GetCoef(i);
else
cout<<"+"<<L.GetCoef(i)<<"x^"<<L.GetExp(i);
}
cout<<endl;
}
void main()
{
Linkedlist s1,s2,s3;
s1.Create();
cout<<"a(x)=";
Print(s1);
s2.Create();
cout<<"b(x)=";
Print(s2);
s3.Add(s1,s2);
cout<<"两多项式和为:"<<endl;
cout<<"c(x)=";
Print(s3);
}
找不到错误,但运行不了。。。