有关c++实现多项式乘除的问题

输入分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;
}

c++

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐