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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛