lihaoyuaiqq130 2018-08-16 13:43 采纳率: 66.7%
浏览 619
已采纳

有关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;
}

  • 写回答

1条回答

  • devmiao 2018-08-16 15:18
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器