鱼C论坛

 找回密码
 立即注册
查看: 1515|回复: 15

[已解决]为什么输出会乱码

[复制链接]
发表于 2021-10-26 13:19:51 | 显示全部楼层 |阅读模式

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

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

x
一个学生管理系统
输入 1 张三 60 90 50

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

  3. typedef struct Student {
  4.         int num;
  5.         char name[10];
  6.         float chinese;
  7.         float math;
  8.         float english;
  9. };

  10. typedef struct node {
  11.         struct Student data;
  12.         struct node* next;
  13. }StudentList;

  14. StudentList* InitList() {    // 初始化单链表
  15.         StudentList* L;
  16.         L = (StudentList*)malloc(sizeof(StudentList));
  17.         L->next = NULL;
  18. }

  19. int InsElem(StudentList* L, struct Student data, int i) {    // 插入元素
  20.         int j = 0;
  21.         StudentList* p = L, * s;
  22.         while (p != NULL && j < i - 1) {
  23.                 j++;
  24.                 p = p->next;
  25.         }
  26.         if (p == NULL) {
  27.                 return 0;
  28.         }
  29.         else {
  30.                 s = (StudentList*)malloc(sizeof(StudentList));
  31.                 s->data = data;
  32.                 s->next = p->next;
  33.                 p->next = s;
  34.                 return 1;
  35.         }
  36. }

  37. void DispList(StudentList *L) {     // 输出元素
  38.         StudentList* p = L->next;
  39.         while (p) {
  40.                 float avg = (p->data.chinese + p->data.math + p->data.english) / 3;
  41.                 printf("%d\t%s\t%f\n", p->data.num, p->data.name, avg);
  42.                 p = p->next;
  43.         }
  44. }

  45. void main() {
  46.         StudentList* L;
  47.         L = InitList();
  48.         struct Student info;
  49.         printf("请输入学生的学号,姓名,语文成绩,数学成绩,英语成绩\n");
  50.         int i;
  51.         scanf("%d, %s, %f, %f, %f\n", &info.num, &info.name, &info.chinese, &info.math, &info.english);
  52.         InsElem(L, info, 1);
  53.         DispList(L);
  54. }
复制代码
最佳答案
2021-10-26 15:16:30
村里小黑 发表于 2021-10-26 14:50
蹲个大佬来解答,记得替我一脚,学术不精啊,应该是指针那里出了问题,但是又分析不出来{:10_272 ...
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. //typedef struct Student {
  4. struct Student {
  5.         int num;
  6.         char name[10];
  7.         float chinese;
  8.         float math;
  9.         float english;
  10. };

  11. typedef struct node {
  12.         struct Student data;
  13.         struct node *next;
  14. } StudentList;

  15. StudentList* InitList() {    // 初始化单链表
  16.         StudentList* L;
  17.         //L = (StudentList*)malloc(sizeof(StudentList));
  18.         L = malloc(sizeof(StudentList));
  19.         L->next = NULL;

  20.         return L;
  21. }

  22. int InsElem(StudentList* L, struct Student data, int i) {    // 插入元素
  23.         int j = 0;
  24.         StudentList *p = L, *s;
  25.         while (p != NULL && j < i - 1) {
  26.                 j++;
  27.                 p = p->next;
  28.         }
  29.         if (p == NULL) {
  30.                 return 0;
  31.         }
  32.         else {
  33.                 //s = (StudentList*)malloc(sizeof(StudentList));
  34.                 s = malloc(sizeof(StudentList));
  35.                 s->data = data;
  36.                 s->next = p->next;
  37.                 p->next = s;
  38.                 return 1;
  39.         }
  40. }

  41. void DispList(StudentList *L) {     // 输出元素
  42.         StudentList* p = L->next;
  43.         while (p) {
  44.                 float avg = (p->data.chinese + p->data.math + p->data.english) / 3;
  45.                 printf("%d\t%s\t%f\n", p->data.num, p->data.name, avg);
  46.                 p = p->next;
  47.         }
  48. }

  49. //void main() {
  50. int main(void) {
  51.         StudentList* L;
  52.         L = InitList();
  53.         struct Student info;
  54.         printf("请输入学生的学号,姓名,语文成绩,数学成绩,英语成绩\n");
  55.         //int i;
  56.         //scanf("%d, %s, %f, %f, %f\n", &info.num, &info.name, &info.chinese, &info.math, &info.english);
  57.         scanf("%d%s%f%f%f", &info.num, info.name, &info.chinese, &info.math, &info.english);
  58.         InsElem(L, info, 1);
  59.         DispList(L);

  60.         return 0;
  61. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-26 13:20:24 | 显示全部楼层
输出是 学号 姓名 平均成绩
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 13:24:20 | 显示全部楼层
数据结构 头疼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-26 13:40:15 | 显示全部楼层

是很难,不过都选C了,不学好真的对不起自己
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 14:09:31 | 显示全部楼层
a327904410 发表于 2021-10-26 13:40
是很难,不过都选C了,不学好真的对不起自己

上学期学这个差点没了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 14:50:42 | 显示全部楼层
蹲个大佬来解答,记得替我一脚,学术不精啊,应该是指针那里出了问题,但是又分析不出来(也有可能分析错了)。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 15:16:30 | 显示全部楼层    本楼为最佳答案   
村里小黑 发表于 2021-10-26 14:50
蹲个大佬来解答,记得替我一脚,学术不精啊,应该是指针那里出了问题,但是又分析不出来{:10_272 ...
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. //typedef struct Student {
  4. struct Student {
  5.         int num;
  6.         char name[10];
  7.         float chinese;
  8.         float math;
  9.         float english;
  10. };

  11. typedef struct node {
  12.         struct Student data;
  13.         struct node *next;
  14. } StudentList;

  15. StudentList* InitList() {    // 初始化单链表
  16.         StudentList* L;
  17.         //L = (StudentList*)malloc(sizeof(StudentList));
  18.         L = malloc(sizeof(StudentList));
  19.         L->next = NULL;

  20.         return L;
  21. }

  22. int InsElem(StudentList* L, struct Student data, int i) {    // 插入元素
  23.         int j = 0;
  24.         StudentList *p = L, *s;
  25.         while (p != NULL && j < i - 1) {
  26.                 j++;
  27.                 p = p->next;
  28.         }
  29.         if (p == NULL) {
  30.                 return 0;
  31.         }
  32.         else {
  33.                 //s = (StudentList*)malloc(sizeof(StudentList));
  34.                 s = malloc(sizeof(StudentList));
  35.                 s->data = data;
  36.                 s->next = p->next;
  37.                 p->next = s;
  38.                 return 1;
  39.         }
  40. }

  41. void DispList(StudentList *L) {     // 输出元素
  42.         StudentList* p = L->next;
  43.         while (p) {
  44.                 float avg = (p->data.chinese + p->data.math + p->data.english) / 3;
  45.                 printf("%d\t%s\t%f\n", p->data.num, p->data.name, avg);
  46.                 p = p->next;
  47.         }
  48. }

  49. //void main() {
  50. int main(void) {
  51.         StudentList* L;
  52.         L = InitList();
  53.         struct Student info;
  54.         printf("请输入学生的学号,姓名,语文成绩,数学成绩,英语成绩\n");
  55.         //int i;
  56.         //scanf("%d, %s, %f, %f, %f\n", &info.num, &info.name, &info.chinese, &info.math, &info.english);
  57.         scanf("%d%s%f%f%f", &info.num, info.name, &info.chinese, &info.math, &info.english);
  58.         InsElem(L, info, 1);
  59.         DispList(L);

  60.         return 0;
  61. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 16:21:01 | 显示全部楼层

主要是因为初始化单链表之后没有返回指针?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 16:34:19 | 显示全部楼层
村里小黑 发表于 2021-10-26 16:21
主要是因为初始化单链表之后没有返回指针?

这个问题不好回答,我感觉没有 “主要” 这么一说
代码中任何一个字母错了都不行
我感觉不能说这个字母错的比那个字母主要

代码中的问题我都是注释了原来的代码,然后改的,当然还有添加的内容
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 16:47:59 | 显示全部楼层
人造人 发表于 2021-10-26 16:34
这个问题不好回答,我感觉没有 “主要” 这么一说
代码中任何一个字母错了都不行
我感觉不能说这个字母 ...

但是我觉得L = (StudentList*)malloc(sizeof(StudentList));这一句没啥问题啊,
申请空间后返回一个void指针赋值给Students型指针L和返回转换成Students型指针在赋值给Students型指针L都没问题吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 17:00:27 | 显示全部楼层
村里小黑 发表于 2021-10-26 16:47
但是我觉得L = (StudentList*)malloc(sizeof(StudentList));这一句没啥问题啊,
申请空间后返回一个void ...


我明白了,我确实是有一些犯二了,感谢大佬赐教,以后我也多注意这些细节
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 17:01:15 | 显示全部楼层
人造人 发表于 2021-10-26 16:34
这个问题不好回答,我感觉没有 “主要” 这么一说
代码中任何一个字母错了都不行
我感觉不能说这个字母 ...

我明白了,我确实是有一些犯二了,感谢大佬赐教,以后我也多注意这些细节
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-26 17:47:24 | 显示全部楼层
村里小黑 发表于 2021-10-26 16:47
但是我觉得L = (StudentList*)malloc(sizeof(StudentList));这一句没啥问题啊,
申请空间后返回一个void ...

C语言中不需要这个转换,没有任何必要
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-26 18:08:57 | 显示全部楼层

看了你的代码后我知道我哪里错了,是输入格式错了,明明里面有逗号隔开我还用空格隔开,我真是个大傻逼。不过还是谢谢你帮我改了下代码,学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-26 18:09:29 | 显示全部楼层
jackz007 发表于 2021-10-26 16:03
编译、运行实况:

感谢,辛苦了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-26 18:10:51 | 显示全部楼层
村里小黑 发表于 2021-10-26 16:21
主要是因为初始化单链表之后没有返回指针?

知道哪里错了,代码没错,是我的输入格式错了。输入里面用逗号隔开的,而我用空格隔开,导致输出结果乱码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 23:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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