#include<iostream>
#include<algorithm>
using namespace std;
class Node
{
public:
int para;
int exp;
Node * next;
};
bool cmp (Node x,Node y)
{
return x.para<y.para;
}
void multi(Node *head1,Node *head2,Node *ans)
{
//head1表示第一个多项式的头结点,head2表示第二个多项式的头结点
head1=head1->next;
head2=head2->next;
ans=ans->next;
bool notEnd=true;
int isWhichEnd=0;
//如果变成了1则说明第一个多项式先结束,如果变成了2则说明第二个多项式先结束,如果为3则为两个多项式相等。
while(notEnd)
{
//如果两个多项式当前的指数系数相等的情况。
if(head1->para==head2->para)
{
cout<<"1"<<endl;
ans->para=head1->para;
ans->exp=head1->exp+head2->exp;
cout<<"1"<<ans->para<<" "<<ans->exp<<" ";
ans=ans->next;
head1=head1->next;
head2=head2->next;
// break;
}
//如果第一个多项式的指数系数大于第二个多项式的当前的指数系数。
else if(head1->para > head2->para)
{
//将小的那个多项式的系数与指数存入ans当中。
ans->para=head2->para;
ans->exp=head2->exp;
cout<<" 2"<<ans->para<<" "<<ans->exp<<" ";
ans=ans->next;
// cout<<head2->para<<" "<<head2->exp;
head2=head2->next;
// break;
}
else if(head1->para < head2->para)
{
//如果第一个多项式的系数小于第二个多项式的系数,处理如上。
ans->para=head1->para;
ans->exp=head1->exp;
cout<<" 3"<<ans->para<<" "<<ans->exp<<" ";
ans=ans->next;
head1=head1->next;
// break;
}
else
{
cout<<"系数的判断你有漏的地方,傻子"<<endl;
}
if(head1->next==NULL || head2->next==NULL)
{
if(head1->next==NULL && head2->next!=NULL)
{
//说明多项式1已经到了最后一个位置,而多项式2还没有结束
notEnd=false;
isWhichEnd=1;
// break;
}
else if(head1->next!=NULL && head2->next==NULL)
{
//说明多项式2已经到了最后一个位置,而多项式2还没有结束
notEnd=false;
isWhichEnd=2;
// break;
}
else if(head1->next==NULL && head2->next==NULL)
{
//说明两个多项式同时结束
notEnd=false;
isWhichEnd=3;
// break;
}
else
{
cout<<"结束的判断你有漏的地方,傻子"<<endl;
}
}
}
cout<<endl;
cout<<isWhichEnd<<endl;
if(!notEnd && isWhichEnd)
{
if(isWhichEnd==1)
{
while(head2->next!=NULL)
{
ans->para=head2->para;
ans->exp=head2->exp;
ans=ans->next;
head2=head2->next;
}
ans->para=head2->para;
ans->para=head2->exp;
ans->next=NULL;
}
if(isWhichEnd==2)
{
while(head1->next!=NULL)
{
ans->para=head1->para;
ans->exp=head1->exp;
ans=ans->next;
head1=head1->next;
}
ans->para=head1->para;
ans->para=head1->exp;
ans->next=NULL;
}
if(isWhichEnd==3)
{
if(head1->para > head2->para)
{
ans->para=head2->para;
ans->exp=head2->exp;
ans=ans->next;
ans->para=head1->para;
ans->exp=head1->exp;
ans->next=NULL;
}
else if(head1->para < head2->para)
{
ans->para=head1->para;
ans->exp=head1->exp;
ans=ans->next;
ans->para=head2->para;
ans->exp=head2->exp;
ans->next=NULL;
}
else if(head1->para == head2->para)
{
ans->para=head1->para;
ans->exp=head1->exp+head2->exp;
ans->next=NULL;
}
}
}
return ;
}
int main()
{
int T1,T2;
cin>>T1;
Node data1[T1+9];
data1[0].exp=-99;
data1[0].para=-99;
for(int i=1;i<=T1;i++)
{
cin>>data1[i].para>>data1[i].exp;
data1[i-1].next=data1+i;
if(i==T1)
{
data1[i].next=NULL;
}
}
cin>>T2;
Node data2[T2+9];
data2[0].para=-99;
data2[0].exp=-99;
for(int i=1;i<=T2;i++)
{
cin>>data2[i].para>>data2[i].exp;
data2[i-1].next=data2+i;
if(i==T2)
{
data2[i].next=NULL;
}
}
Node multiData[T1+T2+9];
multiData[0].exp=-99;
multiData[0].para=-99;
for(int i=1;i<=T1+T2;i++)
{
//对multi的存储地址进行初始化
multiData[i].para=-i;
multiData[i].exp=-i;
multiData[i-1].next=multiData+i;
// if(i==T1+T2)
// {
// multiData[i].next=NULL;
// }
}
//对于乘法运算的处理需要进行一个排序的算法。按照系数进行升序排列。
sort(data1,data1+T1+1,cmp);
sort(data2,data2+T2+1,cmp);
//排完序之后要再次搜寻到最后一个节点处将最后一个节点的next赋值为NULL
for(int i=1;i<=T1;i++)
{
data1[i-1].next=data1+i;
if(i==T1)
{
data1[i].next=NULL;
}
}
for(int i=1;i<=T2;i++)
{
data2[i-1].next=data2+i;
if(i==T2)
{
data2[i].next=NULL;
}
}
// Node *tem=multiData;
// cout<<tem->para<<endl;
//==============================================
// Node *first1;
// Node *first2;
//
// first1=data1;
// first2=data2;
//
// first1=first1->next;
// first2=first2->next;
// while(first1->next!=NULL)
// {
// cout<<first1->para<<" "<<first1->exp<<" ";
// first1=first1->next;
// }
// cout<<first1->para<<" "<<first1->exp<<endl;
//
// while(first2->next!=NULL)
// {
// cout<<first2->para<<" "<<first2->exp<<" ";
// first2=first2->next;
// }
// cout<<first2->para<<" "<<first2->exp<<endl;
//==================================================
multi(data1,data2,multiData);
Node *firstAns=multiData;
firstAns=firstAns->next;
while(firstAns->next!=NULL)
{
cout<<firstAns->para<<" "<<firstAns->exp<<" ";
firstAns=firstAns->next;
}
cout<<firstAns->para<<" "<<firstAns->exp<<endl;
return 0;
}
纯属娱乐
最新推荐文章于 2024-10-18 11:23:58 发布