m0_69937552 2022-10-01 17:08 采纳率: 66.7%
浏览 34
已结题

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

问题遇到的现象和发生背景

img

用代码块功能插入代码,请勿粘贴截图

#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode{
    float coe;
    int ind;
    struct Lnode* next;
}Lnode,*Linklist;

Linklist Creat(int n);
Linklist outputc(Linklist L,int n);
Linklist Comadd(Linklist L1,Linklist L2,Linklist L3);

int main(){
    Linklist L,L1,L2,L3;
    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将两式相加得:");
    L3=Comadd(L1,L2,L3);
    outputc(L3,n);
}

//创建单链表 尾插法
Linklist Creat(int n){
    Linklist L,r,p;
    L=(Linklist)malloc(sizeof(Lnode));
    r=L;
    int i;
    for(i=0;i<n;i++){
        p=(Linklist)malloc(sizeof(Lnode));
        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;
}

//输出单链表
Linklist outputc(Linklist L,int n){
    int i=0;
    Linklist p;
    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++;
    }
}

//比较单链表中同类项系数大小,并加入新链表中
Linklist Comadd(Linklist L1,Linklist L2,Linklist L3){
    L3=(Linklist)malloc(sizeof(Lnode));
    Linklist p1,p2,p3,t;
    p1=L1->next ;
    p2=L2->next ;
    p3=L3->next ;
    t=(Linklist)malloc(sizeof(Lnode));
    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));
        head->next = NULL;
        float coef;
        int expn;
        printf("输入该多项式每一项的系数和指数,每项一行,输入0 0结束!\n");
        while (1) {
            scanf("%f %d", &coef, &expn);
            if (0.0==coef && 0==expn) break;
            if (head->next) {
                p = head;
                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 {
                head->next       = (PolyNode*)malloc(sizeof(PolyNode));
                head->next->coef = coef;
                head->next->expn = expn;
                head->next->next = NULL;
            }
        }
        return head;
    }
    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);
    
        poly3 = polyAdd(poly1, 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
    //请按任意键继续. . .
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来
  • ¥15 java 判断某个数 区间是否存在