m0_56743273 2021-04-11 13:51 采纳率: 50%

# 一元多项式的相加（数据结构链表）

//头文件

#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct Polynode{
float coef;
int expn;
struct Polynode *next;
}Polynomial;
void ShowPolynomial(Polynomial *L);

{
Polynomial *p, *s;
float c;
int e,i;
for(i=1;i<=n;i++)
{
cout<<"输入第"<<i<<"项的系数：";
cin>>c;
cout<<"输入第"<<i<<"项的指数：";
cin>>e;
s = new Polynomial;
s->coef = c;
s->expn = e;
p->next = s;
p = p->next;
}
p->next = NULL;
}
{
Polynomial *p,*q,*s,*l;
float co;
p = a->next;
q = b->next;
l=c;
while( p&&q )
{
s = new Polynomial;
if(p->expn>q->expn)
{
s->coef = p->coef;
s->expn = p->expn;
l->next = s;
p = p->next;
l = l->next;
}
else if(p->expn<q->expn)
{
s->coef = q->coef;
s->expn = q->expn;
l->next = s;
q = q->next;
l = l->next;
}
else
{
co=p->coef+q->coef;
s->coef=co;
if(co!=0)
{
s->expn=p->expn;
l->next=s;
l=l->next;
p=p->next;
q=q->next;
}
else
p=p->next;
q=q->next;
}
}
if(p==NULL)
{
while(q)
{
s = new Polynomial;
s->coef = q->coef;
s->expn = q->expn;
l->next = s;
q = q->next;
l = l->next;
}
}
else if(q==NULL)
{
while(p)
{
s = new Polynomial;
s->coef = p->coef;
s->expn = p->expn;
l->next = s;
p = p->next;
l = l->next;
}
}
l->next = NULL;
}
{
Polynomial *p;
while(p)
{
if(p->next=NULL)
cout<<p->coef<<"x^"<<p->expn;
else
cout<<p->coef<<"x^"<<p->expn<<"+";
p=p->next;
}
}
//主函数

#include <iostream>
#include "polynominal.h"
using namespace std;

int main()
{
int n,m;
Polynomial *la,*lb,*lc;
lc=NULL;
cout<<"请输入多项式la的项数:";
cin>>n;
PolyCreate(la,n);
cout<<"请输出一元多项式la:";
ShowPolynomial(la);
cout<<"请输入多项式lb的项数:";
cin>>m;
PolyCreate(lb,m);
cout<<"请输出一元多项式lb:";
ShowPolynomial(lb);
cout<<"请输出相加后的一元多项式lc";
ShowPolynomial(lc);
}

• 写回答

#### 2条回答默认 最新

• SoftwareTeacher 《编程之美》作者 2021-04-11 14:44
关注

PolyCreate(la,n);

在调用这个函数前， 要先初始化 la，给它空间。

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 用C语言完成一个复杂的游戏
• ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名？
• ¥50 MTK手机模拟HID鼠标出现卡顿
• ¥20 求下下面这个数据结构代码
• ¥20 前端 二进制文件流图片转化异常
• ¥15 github上的这个C语言项目如何跑起来
• ¥15 java 判断某个数 区间是否存在
• ¥15 appium控制多个雷电模拟器问题
• ¥15 C# iMobileDevice
• ¥15 谁会做这个啊#ensp#Boson NetSim