输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数)。数字间以空格分隔,我用链表实现的,为什么会出现无限循环的问题?
#include
using namespace std;
struct node
{
int xishu;
int zhishu;
node*next;
};
node*front1=(node*)malloc(sizeof(node*));
node*front2=(node*)malloc(sizeof(node*));
int n1,n2;
void insert(node*i,node*a);
node* add(node*front1,node*fromt2);
node* cheng(node*front1,node*front2);
int main()
{
node* f=front1;
node* temp;
temp=(node*)malloc(sizeof(node*));
cin>>n1;
for(int i=0;i<n1;i++)
{
cin>>temp->xishu;
cin>>temp->zhishu;
temp->next=NULL;
insert(temp,f);
f=f->next;
}
f=front2;
cin>>n2;
for(int i=0;i<n2;i++)
{
cin>>temp->xishu;
cin>>temp->zhishu;
temp->next=NULL;
insert(temp,f);
f=f->next;
}
f=add(front1,front2);
f=f->next;
while(f!=NULL)
{
cout<<f->xishu<<" ";
cout<<f->zhishu<<endl;
f=f->next;
}
f=cheng(front1,front2);
f=f->next;
while(f!=NULL)
{
cout<<f->xishu<<" ";
cout<<f->zhishu<<endl;
f=f->next;
}
}
void insert(node*i,node*a)
{
a->next=i;
}
node*add(node*front1,node*front2)
{
node*result;
result=(node*)malloc(sizeof(node*));
node*head=result;
node*t1;node*t2;
t1=front1->next;
t2=front2->next;
while(t1!=NULL&&t2!=NULL)
{
node*temp1=(node*)malloc(sizeof(node*));
temp1->next=NULL;
if(t1->zhishu==t2->zhishu)
{
temp1->xishu=t1->xishu+t2->xishu;
temp1->zhishu=t1->zhishu;
result->next=temp1;
result=temp1;
t1=t1->next;
t2=t2->next;
}
else if(t1->zhishu>t2->zhishu)
{
temp1->xishu=t1->xishu;
temp1->zhishu=t1->zhishu;
result->next=temp1;
result=temp1;
t1=t1->next;
}
else if(t1->zhishuzhishu)
{
temp1->xishu=t2->xishu;
temp1->zhishu=t2->zhishu;
result->next=temp1;
result=temp1;
t2=t2->next;
}
}
while(t1!=NULL)
{
result->next=t1;
result=t1;
t1=t1->next;
}
while(t2!=NULL)
{
result->next=t2;
result=t2;
t2=t2->next;
}
return head;
}
node*cheng(node*front1,node*front2)
{
node*head1=front1->next;
node*head2=front2->next;
node*temp=(node*)malloc(sizeof(node*));
node*result=(node*)malloc(sizeof(node*));
while(head2!=NULL)
{
temp->xishu=head1->xishu*head2->xishu;
temp->zhishu=head1->zhishu+head2->zhishu;
temp->next=NULL;
insert(temp,result);
head2=head2->next;
}
head1=head1->next;
while(head1!=NULL)
{
head2=front2->next;
while(head2!=NULL)
{
temp->xishu=head1->xishu*head2->xishu;
temp->zhishu=head1->zhishu+head2->zhishu;
temp->next=NULL;
add(temp,result);
}
head1=head1->next;
}
return result;
}