| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4302 人关注过本帖
标题:有没有大佬帮忙看一下哪里出现了问题,编译没有错误却无法输出,知道的麻烦 ...
只看楼主 加入收藏
浅唱低吟
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
帖 子:2277
专家分: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
快速回复:有没有大佬帮忙看一下哪里出现了问题,编译没有错误却无法输出,知道的 ...
数据加载中...
 
   



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

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