鱼C论坛

 找回密码
 立即注册
查看: 1422|回复: 5

[已解决]无法进行顺序表的遍历输出,到底是什么地方错了

[复制链接]
发表于 2020-6-13 17:34:50 | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
typedef struct students
{
        int number[max];
        int score;
}list;
typedef list datatype;
typedef struct shunxubiao
{
        datatype students[max];
        int length;
}info;
info kong();//置空一个顺序表
void Scanf(info L);//向顺序表中输入想要输入的数据
void printflist(info L);//此函数是为了进行函数的输出
int main()
{
        info A = kong();//定义一个顺序表啊
        Scanf(A);//向顺序表中输入想要输入的数据
        printflist(A);
        return 0;
}
info kong()//置空一个顺序表
{
        info L;
        L.length = 0;
        return L;
}
void Scanf(info L)//向顺序表中输入想要输入的数据
{
        if (L.length == max)
        {
                printf("顺序表中已经满了,无法进行输入");
                exit(1);
        }
        char ch;
        do
        {
                printf("请输入学生成绩:");
                scanf("%d",& L.students[L.length++].score);
                printf("请输入学生的学号:");
                scanf("%d", &L.students[L.length++].number);
                L.length++;
                printf("是否继续输入?(Y||N):");
                scanf(" %c", &ch);
        } while (ch=='y'||ch=='Y');
}//这个函数是为了进行数据的输入而准备的
void printflist(info L)//此函数是为了进行函数的输出
{
        int i;
        for (i=0;i<;i++)
        {
                printf("学生的成绩");
                printf("%d", L.students[i].score);
                printf("学生的学号");
                printf("%s", L.students[i].number);
        }
}请求帮助
最佳答案
2020-6-14 01:15:00
WFR 发表于 2020-6-13 17:49
顺序表的长度为什么经过了输入之后还是0,明明有将长度加1的语句

在csdn论坛上也是你问的吗,有回答
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-13 17:36:16 | 显示全部楼层
这个遍历函数应该怎么写啊??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 17:37:33 | 显示全部楼层
在vs2019上像是printflist函数中for循环位置的L.length 的值是零,该怎么解决啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 17:49:23 | 显示全部楼层
顺序表的长度为什么经过了输入之后还是0,明明有将长度加1的语句
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 19:50:55 | 显示全部楼层
本帖最后由 superbe 于 2020-6-13 19:56 编辑

输入数据的函数应使用指针参数,否则的话,数据只是输入到临时变量里,main里的顺序表并没有得到数据。还有其它一些修改。
  1. #include <stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define MAX 100

  5. typedef struct student
  6. {
  7.     int number;
  8.     int score;
  9. }datatype;
  10. typedef struct shunxubiao
  11. {
  12.     datatype students[MAX];
  13.     int length;
  14. }info;
  15. info kong();               //置空一个顺序表
  16. void Scanf(info * L);      //向顺序表中输入想要输入的数据
  17. void printflist(info * L); //输出顺序表

  18. int main()
  19. {
  20.     info A = kong();       //定义一个顺序表
  21.     Scanf(&A);             //向顺序表中输入想要输入的数据
  22.     printflist(&A);
  23.     return 0;
  24. }

  25. info kong()                //置空一个顺序表
  26. {
  27.     info L;
  28.     L.length = 0;
  29.     return L;
  30. }

  31. void Scanf(info * L)       //向顺序表中输入想要输入的数据
  32. {
  33.     if (L->length == MAX)
  34.     {
  35.         printf("顺序表中已经满了,无法进行输入");
  36.         exit(1);
  37.     }
  38.     char ch;
  39.     do
  40.     {
  41.         printf("请输入学生成绩: ");
  42.         scanf("%d", &L->students[L->length].score);
  43.         printf("请输入学生的学号: ");
  44.         scanf("%d", &L->students[L->length].number);
  45.         L->length++;
  46.         printf("是否继续输入?(Y||N): ");
  47.         scanf(" %c", &ch);
  48.     } while (ch == 'y' || ch == 'Y');
  49. }

  50. void printflist(info * L)  //输出顺序表
  51. {
  52.     int i;
  53.     for (i = 0; i < L->length; i++)
  54.     {
  55.         printf("学生的成绩: %3d ", L->students[i].score);
  56.         printf("学生的学号: %d\n", L->students[i].number);
  57.     }
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-14 01:15:00 | 显示全部楼层    本楼为最佳答案   
WFR 发表于 2020-6-13 17:49
顺序表的长度为什么经过了输入之后还是0,明明有将长度加1的语句

在csdn论坛上也是你问的吗,有回答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-2 07:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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