鱼C论坛

 找回密码
 立即注册
查看: 1194|回复: 16

[已解决]关于链表的一个程序

[复制链接]
发表于 2019-12-18 08:48:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>

struct student
{
        int num;
        float score;
        struct student *next;
};


void main()
{
        struct student *p1, *p2, *head;
        struct student student[10];
        int n, i;
        i = 0;

        printf("Please enter the student's number and score: \n");                                   /*这里运行出来是正确的,编译运行出来的是这样的:
        printf("Number:");                                                                                             Please enter the student's number and score:      
        scanf("%d", &student[0].num);                                                                           Number:1
        printf("Score:");                                                                                                 Score:98*/
        scanf("%.2f", &student[0].score);
       
        p1 = &student[0];
        p2 = &student[0];

        head = NULL;
        n = 0;

        while( (*p1).num != 0 )
        {
                n = n + 1;
                if(n == 1)
                {
                        head = p1;       
                }
                else
                {
                        (*p2).next = p1;
                }


                p2 = p1;
                i++;
                p1 = &student;


                printf("\n\nPlease enter the student's number and score: \n");            /*这段是复制前面那段printf的,然后进行了一些修改,然后编译运行的时候,想像上面printf那样打印出来,可是这里
                printf("Number:");                                                                             直接运行跳过了scanf("%d", &student.num);都没给我输入的机会,一直执行到scanf("%.2f", &student.score);
                scanf("%d", &student.num);                                                           这里才停下给我输入,最后运行出来是这样的:   
                printf("Score:");                                                                                 Please enter the student's number and score:        
                scanf("%.2f", &student.score);                                                         Number:Score:94*/
        }

                                                                                                                    
        (*p1).next = NULL;


}



有大佬能帮我看看这是怎么回事吗?


最佳答案
2019-12-18 16:21:17
我这个现在可以运行,而且要注意输入的数据不能超过数组容量
不知道你的问题,具体出在了哪里。。
1.png
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

struct student
{
        int num;
        float score;
        struct student *next;
};


void main()
{
        struct student *p1, *p2, *head;
        struct student student[10];
        int n, i;
        i = 0;
        printf("Please enter the student's number and score: \n");                                   
        printf("Number:");                                                                                            
        scanf("%d", &student[0].num);

        printf("Score:");
        scanf("%f", &student[0].score);

        p1 = &student[0];
        p2 = &student[0];

        head = NULL;
        n = 0;

        while ((*p1).num != 0)
        {
                n = n + 1;
                if (n == 1)
                {
                        head = p1;
                }
                else
                {
                        (*p2).next = p1;
                }


                p2 = p1;
                i++;
                p1 = &student[i];


                printf("\n\nPlease enter the student's number and score: \n");           
                printf("Number:");                                                                             
                scanf("%d", &student[i].num);    
                int ch;
                while ((ch = getchar()) != EOF && ch != '\n')
                {
                        ;
                }
                printf("Score:");                                                                               
                scanf("%f", &student[i].score);                                                         
}


        (*p1).next = NULL;


}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-12-18 08:51:20 | 显示全部楼层
编译的时候没有报错,仔细想了,好像也没有逻辑错误,scanf("%d", &student.num);  这里为什么会直接给跳过了呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-18 08:54:14 | 显示全部楼层
        printf("\n\nPlease enter the student's number and score: \n");
                printf("Number:");
                scanf("%d", &student[i].num);
                printf("Score:");
                scanf("%.2f", &student[i].score);


不知道为什么[i]没有复制过来,出问题的printf程序是这样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-18 09:09:20 | 显示全部楼层
在scanf前面加一个
getchar();
试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-18 09:19:36 | 显示全部楼层
Tec 发表于 2019-12-18 09:09
在scanf前面加一个
getchar();
试试

                printf("\n\nPlease enter the student's number and score: \n");
                printf("Number:");
                getchar();
                scanf("%d", &student.num);
                printf("Score:");
                scanf("%.2f", &student.score);

这样吗?输出出来还是
Please enter the student's number and score:
Number:Score:94

Number后面根本不给我输入的机会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-18 09:21:52 | 显示全部楼层
[i]又没有复制过来,怎么回事。。。


printf("\n\nPlease enter the student's number and score: \n");
printf("Number:");
getchar();
scanf("%d", &student[i].num);
printf("Score:");
scanf("%.2f", &student[i].score);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-18 09:33:29 | 显示全部楼层
那你改成这个试试,主要问题我觉得就是缓冲区没有清空
导致你之前输入的回车,被下一个Number接收了
int ch;
while ((ch = getchar()) != EOF && ch != '\n')
{
   ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-18 15:06:06 | 显示全部楼层
Tec 发表于 2019-12-18 09:33
那你改成这个试试,主要问题我觉得就是缓冲区没有清空
导致你之前输入的回车,被下一个Number接收了

我用编译器查看了一下,发现最开始的student[0].num里面确实接收到了我输入的“1”,然后student[0].score里面没有接收到我输入的“98”,是一堆乱码,于是我想,可能是接收到了缓冲区里的回车吧,然后我就在scanf("%d", &student[0].num);这一句后面试了你说的这个代码,变成了这样:

printf("Please enter the student's number and score: \n");
printf("Number:");
scanf("%d", &student[0].num);
while( (ch = getchar()) != EOF && ch != '\n')
        {
                ;
        }
printf("Score:");
scanf("%.2f", &student[0].score);

可是查看的时候,发现Score根本没有接收到我输入的数,还是一堆乱码,然后执行到后面的再次需要输入的地方,也就是scanf("%d", &student.num);这里,就是这个Number根本不给我输入的机会的地方,我查看了一下student.num里面的数据,发现反而是它接收到了我前面输入的98?!然后后面全是这样,每次执行到Score,就让我输入,我输入了一个数,这个数就会存入到之后的Number里面去?于是Score里面的数据就全是乱码。
       
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-18 15:10:31 | 显示全部楼层
Tec 发表于 2019-12-18 09:33
那你改成这个试试,主要问题我觉得就是缓冲区没有清空
导致你之前输入的回车,被下一个Number接收了

并且这个程序诡异的地方是:

printf("Please enter the student's number and score: \n");
printf("Number:");
scanf("%d", &student[0].num);
printf("Score:");
scanf("%.2f", &student[0].score);             /*这里改成%f,按理说是没毛病的,但是我一改,执行的时候
                                                                噔的一声弹出来一个带红叉的窗口,告诉我debug。*/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-18 15:14:04 | 显示全部楼层
本帖最后由 Tec 于 2019-12-18 15:16 编辑

改成scanf("%f", &student[0].score);
这里好像也有问题
scanf好像没有格式化输入吧

debug内容是什么啊。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-18 15:52:47 | 显示全部楼层
Tec 发表于 2019-12-18 15:14
改成scanf("%f", &student[0].score);
这里好像也有问题
scanf好像没有格式化输入吧

Debug Error!

Program:F:\软件\C语言\another\结构体\Debug\例10.exe

runtime error

(Press Retry to debug the application)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-18 15:56:33 | 显示全部楼层
本帖最后由 Tec 于 2019-12-18 15:58 编辑

你看一下cmd那个黑窗口有没有输出什么错误提示

而且输入数据停止是根据number为0才停止,student数组容易越界
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-18 15:58:07 | 显示全部楼层
Tec 发表于 2019-12-18 15:56
你看一下cmd那个黑窗口有没有输出什么错误提示

没有,点弹出的错误窗口,中止之后,cmd窗口就直接显示结束了

Press any key to continue
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-18 16:21:17 | 显示全部楼层    本楼为最佳答案   
我这个现在可以运行,而且要注意输入的数据不能超过数组容量
不知道你的问题,具体出在了哪里。。
1.png
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

struct student
{
        int num;
        float score;
        struct student *next;
};


void main()
{
        struct student *p1, *p2, *head;
        struct student student[10];
        int n, i;
        i = 0;
        printf("Please enter the student's number and score: \n");                                   
        printf("Number:");                                                                                            
        scanf("%d", &student[0].num);

        printf("Score:");
        scanf("%f", &student[0].score);

        p1 = &student[0];
        p2 = &student[0];

        head = NULL;
        n = 0;

        while ((*p1).num != 0)
        {
                n = n + 1;
                if (n == 1)
                {
                        head = p1;
                }
                else
                {
                        (*p2).next = p1;
                }


                p2 = p1;
                i++;
                p1 = &student[i];


                printf("\n\nPlease enter the student's number and score: \n");           
                printf("Number:");                                                                             
                scanf("%d", &student[i].num);    
                int ch;
                while ((ch = getchar()) != EOF && ch != '\n')
                {
                        ;
                }
                printf("Score:");                                                                               
                scanf("%f", &student[i].score);                                                         
}


        (*p1).next = NULL;


}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-12-18 16:52:27 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-18 16:54 编辑
#include <stdio.h>
#include <malloc.h>
typedef struct student
{
    int num;
    float score;
    struct student *next;
} Stu;
Stu *CreatNodeList()//创建链表
{
    Stu *n = (Stu *)malloc(sizeof(Stu));
    Stu *h = n;
    printf("Please enter the student's number and score: \n");
    printf("Number:");
    scanf("%d", &h->num);
    printf("Score:");
    scanf("%f", &h->score);
    while (n->num != 0 || n->score != 0)
    {
        n->next = (Stu *)malloc(sizeof(Stu));
        n = n->next;
        printf("Please enter the student's number and score: \n");
        printf("Number:");
        scanf("%d", &n->num);
        printf("Score:");
        scanf("%f", &n->score);
        n->next = NULL;
    }
    return h;
}
void PrintStu(Stu *h)//输出链表数据
{
    while (h->next != NULL)
    {
        printf("num = %d\nscore = %.2f\n", h->num, h->score);
        Stu *t = h;
        h = h->next;
        free(t);
    }
    
}
int main(int argc, char const *argv[])
{
    Stu *h;
    h = CreatNodeList();
    printf("put out num & score\n");
    PrintStu(h);
    return 0;
}
=====================================================
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-gxuuun5j.ohs --stdout=Microsoft-MIEngine-Out-3y3dihb2.zz0 --stderr=Microsoft-MIEngine-Error-1pakn4hx.5t0 --pid=Microsoft-MIEngine-Pid-pwuuuxt4.d5l --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
Please enter the student's number and score:
Number:0
Score:1.02
Please enter the student's number and score:
Number:1
Score:0.023
Please enter the student's number and score:
Number:3
Score:0
Please enter the student's number and score:
Number:2
Score:0.2
Please enter the student's number and score:
Number:0
Score:0

put out num & score
num = 0
score = 1.02
num = 1
score = 0.02
num = 3
score = 0.00
num = 2
score = 0.20
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-12-18 20:46:21 | 显示全部楼层
Tec 发表于 2019-12-18 16:21
我这个现在可以运行,而且要注意输入的数据不能超过数组容量
不知道你的问题,具体出在了哪里。。

我照着这个程序,重新写了一遍,还是出debug,唉,大佬辛苦了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-19 08:37:57 | 显示全部楼层
bin554385863 发表于 2019-12-18 16:52
=====================================================
Microsoft Windows [版本 10.0.18363.535]
(c ...

谢谢大佬,我这个题是根据小脚鱼的题意做的,在没看过答案的情况下,我知道我这个程序对于动态链表这个题其实是不对的,但是我想知道的是我这个程序,编译器既没有报错,逻辑问题检查了好几遍好像也没有问题的情况下,为什么在运行的时候会出问题,其实我想知道我的是这个程序是不是哪里写错了,但是根据上楼用一模一样的程序执行了一遍,并没有问题,也就是说那个程序其实没有写错,问题出在别的地方
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-11 18:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表