标题:有没有大佬帮忙看一下哪里出现了问题,编译没有错误却无法输出,知道的麻烦 ...
只看楼主
浅唱低吟
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-3-28
结帖率:100%
已结贴  问题点数:20 回复次数:1 
有没有大佬帮忙看一下哪里出现了问题,编译没有错误却无法输出,知道的麻烦帮一下吧,多谢了!
#include<stdio.h>
#include<malloc.h>
typedef struct Polynode
{
    int coef;
    int exp;
    struct Polynode *next;
}Polynode,*Polylist;
Polylist PolyCreate();
void polyadd(Polylist polya,Polylist polyb);
void output(Polylist poly);



Polylist PolyCreate()
{
    Polynode *head,*rear,*s;
    int c,e;
    head=(Polynode *)malloc(sizeof(Polynode));
    rear=head;
    printf("输入系数与指数,系数为零时结束\n");
    scanf("%d,%d",&c,&e);
    while(c!=0)
    {
        s=(Polynode *)malloc(sizeof(Polynode));
        s->coef=c;
        s->exp=e;
        rear->next=s;
        rear=s;
        scanf("%d,%d",&c,&e);   
    }
    rear->next=NULL;
    return(head);
}


void output(Polylist poly)
{
    Polynode *p;
    p=(Polynode *)malloc(sizeof(Polynode));
    p=poly->next;
    while(p!=NULL)
    {
        if(p->exp=0)
        {
            if(p->coef>0)
            printf("+%d",p->coef);
            else
            printf("%d",p->coef);
        }
        if(p->exp>0)
        {
            if(p->coef>0)
            printf("+%d*x^%d",p->coef,p->exp);
            else
            printf("%d*x^%d",p->coef,p->exp);
        }
        p=p->next;
    }
}






void polyadd(Polylist polya,Polylist polyb)
{
    Polynode *p,*q,*tail,*temp;
    int sum;
    p=polya->next;
    q=polyb->next;
    tail=polya;
    while(p!=NULL&&q!=NULL)
    {
        if(p->exp<q->exp)
        {
            tail->next=p;
            tail=p;
            p=p->next;
        }
        else if(p->exp==q->exp)
        {
            sum=p->coef+q->coef;
            if(sum!=0)
            {
                p->coef=sum;
                tail->next=p;
                tail=p;
                p=p->next;
                temp=q;
                q=q->next;
                free(temp);
            }
            else
            {
                temp=p;
                p=p->next;
                free(temp);
                temp=q;
                q=q->next;
                free(temp);
            }
        }
         else
         {
             tail->next=q;
             tail=q;
             q=q->next;
         }
    }
    if(p!=NULL)
    tail->next=p;
    else
    tail->next=q;
}
int main()
{
    Polynode *a,*b;

    a=PolyCreate();
    output(a);
    b=PolyCreate();
    output(b);
    polyadd(a,b);
    output(a);
    return (0);
     
}
搜索更多相关主题的帖子: temp exp printf output next 
2021-03-28 08:58
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:14 
程序代码:
#include<stdio.h>
#include<malloc.h>
typedef struct Polynode
{
    int coef;
    int exp;
    struct Polynode *next;
}Polynode,*Polylist;
Polylist PolyCreate();
void polyadd(Polylist polya,Polylist polyb);
void output(Polylist poly);



Polylist PolyCreate(int w)
{
    Polynode *head,*rear,*s;
    int c=w,e=2;
    head=(Polynode *)malloc(sizeof(Polynode));
    rear=head;
    printf("输入系数与指数,系数为零时结束\n");
    //scanf("%d,%d",&c,&e);
    while(c>0)
    {
        s=(Polynode *)malloc(sizeof(Polynode));
        s->coef=c;
        s->exp=e;
        rear->next=s;
        rear=s;
        //scanf("%d,%d",&c,&e);   
        c=c-2;
    }
    rear->next=NULL;
    return(head);
}


void output(Polylist poly)
{
    Polynode *p;
    //p=(Polynode *)malloc(sizeof(Polynode));
    p=poly->next;
    while(p!=NULL)
    {
        printf("%d,%d\n",p->coef,p->exp);
        if(p->exp=0)
        {
            if(p->coef>0)
            printf("+%d",p->coef);
            else
            printf("%d",p->coef);
        }
        if(p->exp>0)
        {
            if(p->coef>0)
            printf("+%d*x^%d",p->coef,p->exp);
            else
            printf("%d*x^%d",p->coef,p->exp);
        }
        p=p->next;
    }
}






void polyadd(Polylist polya,Polylist polyb)
{
    Polynode *p,*q,*tail,*temp;
    int sum;
    p=polya->next;
    q=polyb->next;
    tail=polya;
    while(p!=NULL&&q!=NULL)
    {
        if(p->exp<q->exp)
        {
            tail->next=p;
            tail=p;
            p=p->next;
        }
        else if(p->exp==q->exp)
        {
            sum=p->coef+q->coef;
            if(sum!=0)
            {
                p->coef=sum;
                tail->next=p;
                tail=p;
                p=p->next;
                temp=q;
                q=q->next;
                free(temp);
            }
            else
            {
                temp=p;
                p=p->next;
                free(temp);
                temp=q;
                q=q->next;
                free(temp);
            }
        }
         else
         {
             tail->next=q;
             tail=q;
             q=q->next;
         }
    }
    if(p!=NULL)
    tail->next=p;
    else
    tail->next=q;
}
int main()
{
    Polynode *a,*b;

    a=PolyCreate(5);
    output(a);
    b=PolyCreate(6);
    output(b);
    polyadd(a,b);
    printf("\n");
    output(a);
    return (0);
     
}

剑栈风樯各苦辛,别时冰雪到时春
2021-03-28 09:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505272-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.063996 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved