北冥有鱼nkl 2021-03-31 19:39 采纳率: 50%
浏览 35
已采纳

自定义函数部分输入p->num后空格后未判断就继续输入p->name,p->score的值;

 

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int num;
    char name[20];
    int score;
    struct node *next;
};

struct node *creatlist();

int main()
{
    struct node *p,*head;
    int temp_score;
    head=creatlist();
    printf("\n");
    scanf("%d",&temp_score);
    for(p=head;p!=NULL;p=p->next)
    {
        if(p->score>=temp_score);
        {
            printf("%d %s %d\n",p->num,p->name,p->score);
        }
    }
    return 0;
}

struct node *creatlist()
{
    struct node *p,*head;
    head=(struct node *)malloc(sizeof(struct node)); //建立头结点
    head->next=NULL; //将head->next置为空
    while(1)
    {
        p=(struct node *)malloc(sizeof(struct node)); //建立p结点,分配内存空间
        scanf("%d",&p->num);
        if(p->num==0)
        {
            break;
        }
        else
        {
            scanf("%s%d",p->name,&p->score);
            p->next=head->next;
            head->next=p;
        }
    }
    return head->next;
    //输入学生个人信息结束,返回头指针

}

  • 写回答

3条回答 默认 最新

  • CSDN专家-丸子老师 2021-04-01 08:24
    关注
    #include <stdio.h>
    #include <stdlib.h>
     
    struct node
    {
        int num;
        char name[20];
        int score;
        struct node *next;
    };
     
    struct node *creatlist();
     
    int main()
    {
        struct node *p,*head;
        int temp_score;
        head=creatlist();
        printf("\n");
        //scanf("%d",&temp_score);
        for(p=head;p!=NULL;p=p->next)
        {
            if(p->score>=temp_score);
            {
                printf("%d %s %d\n",p->num,p->name,p->score);
            }
        }
        return 0;
    }
     
    struct node *creatlist()
    {
        struct node *p,*head;
        head=(struct node *)malloc(sizeof(struct node)); //建立头结点
        head->next=NULL; //将head->next置为空
        while(1)
        {
            p=(struct node *)malloc(sizeof(struct node)); //建立p结点,分配内存空间
            scanf("%d",&p->num);
            if(p->num==0)
            {
                break;
            }
            else
            {
                scanf("%s%d",p->name,&p->score);
                p->next=head->next;
                head->next=p;
            }
        }
        return head->next;
        //输入学生个人信息结束,返回头指针
     
    }
     
    

    你好,你在自定义函数中if判断了p->num为0就break,却是是把自定义函数结束掉了,自定义函数结束后回到主函数,主函数之中下2条执行的代码是   

    printf("\n");
    scanf("%d",&temp_score);

    printf打印换行,但是scanf在你结束后又接收了一次数据,因此导致了输入0结束了自定义函数后还需要输入一次才能真正结束掉。

    我把问题所在的scanf给注释掉,功能不影响,你看下能不能解决你的问题,如果解决了你的问题,请采纳,谢谢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路