N个一元多项式的加减乘除执行代码.docx
《N个一元多项式的加减乘除执行代码.docx》由会员分享,可在线阅读,更多相关《N个一元多项式的加减乘除执行代码.docx(17页珍藏版)》请在冰豆网上搜索。
N个一元多项式的加减乘除执行代码
N个一元多项式的加减乘除执行代码
#include
usingnamespacestd;
structNode
{
public:
intcoe;
intexp;
Node*next;
};
classList
{
public:
Node*CreatList(Node*Head);
Node*InsertNode(Node*Head,Node*Ptr);
Node*AddList(Node*Head1,Node*Head2);
Node*SubList(Node*Head1,Node*Head2);
Node*MulList(Node*Head1,Node*Head2);
};
Node*List:
:
CreatList(Node*Head)
{
intnum=0;
Node*Ptr=newNode;
Ptr->next=NULL;
cout<<"请输入一元多项式的项数:
";
cin>>num;
for(inti=1;i<=num;i++)
{
cout<<"请输入多项式的第"<
cin>>Ptr->coe;
cin>>Ptr->exp;
if(Ptr->coe==0)
{
}
else
{
InsertNode(Head,Ptr);
}
Ptr=newNode;
Ptr->next=NULL;
}
returnHead;
}
Node*List:
:
InsertNode(Node*Head,Node*Ptr)
{
Node*PrePos=newNode;
Node*NowPos=Head->next;
if(NowPos==NULL)
{
Head->next=Ptr;
Ptr->next=NULL;
}
else
{
PrePos=Head;
while(NowPos!
=NULL)
{
if(Ptr->exp>NowPos->exp)
{
PrePos->next=Ptr;
Ptr->next=NowPos;
break;
}
elseif(Ptr->expexp)
{
PrePos=NowPos;
NowPos=NowPos->next;
}
else
{
if((Ptr->coe+NowPos->coe)==0)
{
PrePos->next=NowPos->next;
deleteNowPos;
break;
}
else
{
NowPos->coe=Ptr->coe+NowPos->coe;
break;
}
}
if(NowPos==NULL)
{
PrePos->next=Ptr;
Ptr->next=NULL;
}
}
}
returnHead;
}
Node*List:
:
AddList(Node*Head1,Node*Head2)
{
Node*NewHead=newNode;
Node*Ptr=newNode;
Ptr->next=NULL;
NewHead->next=NULL;
Node*Ptr1=Head1->next;
Node*Ptr2=Head2->next;
if(Ptr1==NULL)
{
if(Ptr2==NULL)
{
}
else
{
while(Ptr2!
=NULL)
{
Ptr->coe=Ptr2->coe;
Ptr->exp=Ptr2->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr2=Ptr2->next;
}
}
}
else
{
if(Ptr2==NULL)
{
while(Ptr1!
=NULL)
{
Ptr->coe=Ptr1->coe;
Ptr->exp=Ptr1->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr1=Ptr1->next;
}
}
else
{
while(Ptr1!
=NULL)
{
Ptr->coe=Ptr1->coe;
Ptr->exp=Ptr1->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr1=Ptr1->next;
}
while(Ptr2!
=NULL)
{
Ptr->coe=Ptr2->coe;
Ptr->exp=Ptr2->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr2=Ptr2->next;
}
}
}
returnNewHead;
}
Node*List:
:
SubList(Node*Head1,Node*Head2)
{
Node*NewHead=newNode;
Node*Ptr=newNode;
Ptr->next=NULL;
NewHead->next=NULL;
Node*Ptr1=Head1->next;
Node*Ptr2=Head2->next;
if(Ptr1==NULL)
{
if(Ptr2==NULL)
{
}
else
{
while(Ptr2!
=NULL)
{
Ptr->coe=-Ptr2->coe;
Ptr->exp=Ptr2->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr2=Ptr2->next;
}
}
}
else
{
if(Ptr2==NULL)
{
while(Ptr1!
=NULL)
{
Ptr->coe=Ptr1->coe;
Ptr->exp=Ptr1->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr1=Ptr1->next;
}
}
else
{
while(Ptr1!
=NULL)
{
Ptr->coe=Ptr1->coe;
Ptr->exp=Ptr1->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr1=Ptr1->next;
}
while(Ptr2!
=NULL)
{
Ptr->coe=-Ptr2->coe;
Ptr->exp=Ptr2->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr2=Ptr2->next;
}
}
}
returnNewHead;
}
Node*List:
:
MulList(Node*Head1,Node*Head2)
{
Node*NewHead=newNode;
Node*Ptr=newNode;
Ptr->next=NULL;
NewHead->next=NULL;
Node*Ptr1=Head1->next;
Node*Ptr2=Head2->next;
if(Ptr1==NULL)
{
}
else
{
if(Ptr2==NULL)
{
}
else
{
while(Ptr1!
=NULL)
{
while(Ptr2!
=NULL)
{
Ptr->coe=Ptr1->coe*Ptr2->coe;
Ptr->exp=Ptr1->exp+Ptr2->exp;
NewHead=InsertNode(NewHead,Ptr);
Ptr=newNode;
Ptr->next=NULL;
Ptr2=Ptr2->next;
}
Ptr=newNode;
Ptr->next=NULL;
Ptr2=Head2->next;
Ptr1=Ptr1->next;
}
}
}
returnNewHead;
}
voidShowList(Node*Head)
{
Node*Ptr=Head->next;
if(Ptr==NULL)
{
cout<<0;
}
else
{
if(Ptr->coe>0)
{
if(Ptr->coe==1)
{
if(Ptr->exp==0)
{
cout<coe;
}
elseif(Ptr->exp==1)
{
cout<<"x";
}
else
{
cout<<"x^"<exp;
}
}
else
{
if(Ptr->exp==0)
{
cout<coe;
}
elseif(Ptr->exp==1)
{
cout<coe<<"x";
}
else
{
cout<coe<<"x^"<exp;
}
}
}
else
{
if(Ptr->coe==-1)
{
if(Ptr->exp==0)
{
cout<coe;
}
elseif(Ptr->exp==1)
{
cout<<"-"<<"x";
}
else
{
cout<<"-"<<"x^"<exp;
}
}
else
{
if(Ptr->exp==0)
{
cout<coe;
}
elseif(Ptr->exp==1)
{
cout<coe<<"x";
}
else
{
cout<coe<<"x^"<exp;
}
}
}
Ptr=Ptr->next;
while(Ptr!
=NULL)
{
if(Ptr->coe>0)
{
if(Ptr->coe==1)
{
if(Ptr->exp==0)
{
cout<<"+"<coe;
}
elseif(Ptr->exp==1)
{
cout<<"+"<<"x";
}
else
{
cout<<"+"<<"x^"<exp;
}
}
else
{
if(Ptr->exp==0)
{
cout<<"+"<coe;
}
elseif(Ptr->exp==1)
{
cout<<"+"<coe<<"x";
}
else
{
cout<<"+"<coe<<"x^"<exp;
}
}
}
else
{
if(Ptr->coe==-1)
{
if(Ptr->exp==0)
{
cout<coe;
}
elseif(Ptr->exp==1)
{
cout<<"-"<<"x";
}
else
{
cout<<"x^"<exp;
}
}
else
{
if(Ptr->exp==0)
{
cout<coe;
}
elseif(Ptr->exp==1)
{
cout<coe<<"x";
}
else
{
cout<coe<<"x^"<exp;
}
}
}
Ptr=Ptr->next;
}
}
}
voidmain()
{
Node*head1=newNode;
Node*head2=newNode;
head1->next=NULL;
head2->next=NULL;
Listlist1;
Listlist2;
ListNewList;
head1=list1.CreatList(head1);
head2=list2.CreatList(head2);
cout<<"f(x)=";
ShowList(head1);
cout<cout<<"g(x)=";
ShowList(head2);
cout<cout<<"两式相加的结果"<cout<<"F(x)=";
ShowList(NewList.AddList(head1,head2));
cout<cout<<"两式相减的结果"<cout<<"F(x)=";
ShowList(NewList.SubList(head1,head2));
cout<cout<<"两式相乘的结果"<cout<<"F(x)=";
ShowList(NewList.MulList(head1,head2));
cout<system("pause");