m0_69937552 2022-10-01 17:08 采纳率: 66.7%

# C语言一元多项式的求和无法运行

###### 用代码块功能插入代码，请勿粘贴截图
``````
#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode{
float coe;
int ind;
struct Lnode* next;

int main(){
int n;
printf("\n请输入多项式的项数：");
scanf("%d",&n);
printf("\n请输入L1的各项：");
L1=Creat(n);
printf("\nL1为：");
outputc(L1,n);
printf("\n请输入L2的各项：");
L2=Creat(n);
printf("\nL2为：");
outputc(L2, n);
printf("\n将两式相加得:");
outputc(L3,n);
}

//创建单链表 尾插法
r=L;
int i;
for(i=0;i<n;i++){
float coe;
int ind;
scanf("%f %d",&coe,&ind);
p->coe =coe;
p->ind =ind;
p->next =NULL;
r->next =p;
r=p;
}
return L;
}

//输出单链表
int i=0;
p=L->next ;
while(p && i<n){
if(p->next ){//若p的下一项不存在，则p为最后一项
if(p->ind >=0){
printf("%fx^%d+",p->coe ,p->ind );
}
else
printf("%fx^(%d)+",p->coe ,p->ind );

}
else {
printf("%fx^%d",p->coe ,p->ind );
}
p=p->next ;
i++;
}
}

//比较单链表中同类项系数大小，并加入新链表中
p1=L1->next ;
p2=L2->next ;
p3=L3->next ;
while(L1 && L2){
if(p1->ind ==p2->ind ){
p3->coe =p1->coe +p2->coe ;
p3->ind =p1->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p1=p1->next ;
p2=p2->next ;
p3=p3->next ;
}
if(p1->ind < p2->ind ){
p3->coe =p1->coe ;
p3->ind =p1->ind ;
p3=p3->next ;
while(p1){
p1=p1->next ;
if(p1->ind ==p2->ind ){
p3->coe =p1->coe +p2->coe ;
p3->ind =p1->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
}
else if(p1->ind <p2->ind ){
p3->coe =p1->coe  ;
p3->ind =p1->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
}
else if(p1->ind >p2->ind ){
p3->coe =p2->coe  ;
p3->ind =p2->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
}
}
p2=p2->next ;
}
if(p1->ind > p2->ind ){
p3->coe =p2->coe ;
p3->ind =p2->ind ;
p3=p3->next ;
while(p2){
if(p1->ind ==p2->ind ){
p3->coe =p1->coe +p2->coe ;
p3->ind =p1->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
}
else if(p2->ind <p1->ind){
p3->coe =p2->coe  ;
p3->ind =p2->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
}
else if(p2->ind >p1->ind ){
p3->coe =p1->coe  ;
p3->ind =p1->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
}
}
p1=p1->next ;
}
}
while(p1 && !p2){

p3->coe =p1->coe  ;
p3->ind =p1->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
p1=p1->next ;
}
while(!p1&& p2 ){
p3->coe =p2->coe  ;
p3->ind =p2->ind ;
p3->next =NULL;
t->next =p3;
t=p3;
p3=p3->next ;
p2=p2->next ;
}
return L3;
}
``````
• 写回答

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

• 赵4老师 2022-10-01 17:57
关注
``````//链表实现一元多项式的加法减法
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
float  coef;   //系数
int    expn;   //指数
struct node *next;
} PolyNode;
typedef PolyNode* Polynomial;
Polynomial createPolynomial() {  //创建多项式
PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));
float coef;
int expn;
printf("输入该多项式每一项的系数和指数，每项一行，输入0 0结束！\n");
while (1) {
scanf("%f %d", &coef, &expn);
if (0.0==coef && 0==expn) break;
while (p->next && expn < p->next->expn) p = p->next;
if (p->next) {
if (expn == p->next->expn) { //有相同指数的直接把系数加到原多项式
p->next->coef += coef;
if (-0.00001f < p->next->coef && p->next->coef < 0.00001f) { //若是相加后系数为0，则舍弃该节点
q = p->next;
p->next = q->next;
free(q);
}
} else {
q       = (PolyNode*)malloc(sizeof(PolyNode));
q->coef = coef;
q->expn = expn;
q->next = p->next;
p->next = q;
}
} else {
p->next = (PolyNode*)malloc(sizeof(PolyNode));
p       = p->next;
p->coef = coef;
p->expn = expn;
p->next = NULL;
}
} else {
}
}
}
Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多项式相加 poly1+poly2形成一个新的多项式
Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点
poly->next = NULL;
PolyNode *p, *q, *r;
r = poly;
p = poly1->next;
q = poly2->next;
while (p&&q) {
if (p->expn > q->expn) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r       = r->next;
r->coef = p->coef;
r->expn = p->expn;
p       = p->next;
} else if (p->expn < q->expn) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r       = r->next;
r->coef = q->coef;
r->expn = q->expn;
q       = q->next;
} else {
float m = p->coef + q->coef;
if (!(-0.00001f <m && m < 0.00001f)) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r       = r->next;
r->coef = m;
r->expn = p->expn;
}
q = q->next;
p = p->next;
}
}
while (p) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r       = r->next;
r->coef = p->coef;
r->expn = p->expn;
p       = p->next;
}
while (q) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r       = r->next;
r->coef = q->coef;
r->expn = q->expn;
q       = q->next;
}
r->next = NULL;
return poly;
}
Polynomial polySubtract(Polynomial poly1, Polynomial poly2) {  //多项式减法 poly1-poly2形成一个新的多项式
//把poly2的系数取相反数，形成一个新的多项式
Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点
PolyNode *p, *q;
p = poly;
q = poly2->next;
while (q) {
p->next = (PolyNode*)malloc(sizeof(PolyNode));
p       = p->next;
p->coef = -(q->coef);  //系数取反
p->expn = q->expn;
q       = q->next;
}
p->next = NULL;
Polynomial poly3 = polyAdd(poly1, poly);  //利用加法
return poly3;
}
void printPoly(Polynomial poly) {  //打印多项式
if (poly && poly->next) {
PolyNode *p = poly->next;  //p指向第一个节点
while (p->next) {
if (1!=p->expn) printf("%g X^%d", p->coef, p->expn);
else            printf("%g X"   , p->coef         );
p = p->next;
if (p) {
if (p->coef > 0) printf(" +");
else             printf(" ");
}
}
if (p->expn == 0)
printf("%g", p->coef);   //打印常数项
else {
if (1!=p->expn) printf("%g X^%d", p->coef, p->expn);
else            printf("%g X"   , p->coef         );
}
printf("\n");
} else {
printf("0\n");
}
}
void freePoly(Polynomial poly) {  //释放内存
if (poly && poly->next) {
PolyNode *p, *q;
p = poly;
while (p) {
q = p->next;
free(p);
p = q;
}
}
poly = NULL;
}
int main() {
printf("用链表实现多项式的加减法\n");
Polynomial poly1, poly2, poly3;

printf("创建多项式一\n");
poly1 = createPolynomial();

printf("创建多项式二\n");
poly2 = createPolynomial();

printf("          多项式一：");printPoly(poly1);
printf("          多项式二：");printPoly(poly2);

printf("两多项式相加，和为：");printPoly(poly3);
freePoly(poly3);

poly3 = polySubtract(poly1, poly2);
printf("两多项式相减，差为：");printPoly(poly3);
freePoly(poly3);

freePoly(poly2);
freePoly(poly1);
system("pause");
return 0;
}
//用链表实现多项式的加减法
//创建多项式一
//输入该多项式每一项的系数和指数，每项一行，输入0 0结束！
//4 9
//3 6
//2 5
//0 0
//创建多项式二
//输入该多项式每一项的系数和指数，每项一行，输入0 0结束！
//4 9
//3 6
//2 5
//0 0
//        多项式一：4 X^9 +3 X^6 +2 X^5
//        多项式二：4 X^9 +3 X^6 +2 X^5
//两多项式相加，和为：8 X^9 +6 X^6 +4 X^5
//两多项式相减，差为：0
//请按任意键继续. . .

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

• 系统已结题 10月31日
• 已采纳回答 10月23日
• 创建了问题 10月1日

#### 悬赏问题

• ¥200 csgo2的viewmatrix值是否还有别的获取方式
• ¥15 Stable Diffusion，用Ebsynth utility在视频选帧图重绘，第一步报错，蒙版和帧图没法生成，怎么处理啊
• ¥15 请把下列每一行代码完整地读懂并注释出来
• ¥15 pycharm运行main文件，显示没有conda环境
• ¥15 寻找公式识别开发，自动识别整页文档、图像公式的软件
• ¥15 为什么eclipse不能再下载了？
• ¥15 编辑cmake lists 明明写了project项目名，但是还是报错怎么回事
• ¥15 关于#计算机视觉#的问题：求一份高质量桥梁多病害数据集
• ¥15 特定网页无法访问，已排除网页问题
• ¥50 如何将脑的图像投影到颅骨上