gpt4 book ai didi

c - 如何使用用户的输入创建表,表的数量根据用户先前指定的输入而变化?

转载 作者:行者123 更新时间:2023-11-30 14:35:28 25 4
gpt4 key购买 nike

我不允许使用数组。我需要列出用户在第一个 scanf_s 调用中指定的类(class)的所有类(class)代码、日期和时间。我不知道如何在不使用数组的情况下继续。任何帮助提示/帮助将不胜感激。

printf("Please enter the number of courses you'd like to take: ");
int numOfCourses;
scanf_s("%d", &numOfCourses);
int courseCode;
int courseDay;
int courseTime;

int i = 0;
while (i < numOfCourses) {
printf("Please enter the code of the course: ");
scanf_s("%d", &courseCode);
printf("Please enter the day of the course: ");
scanf_s("%d", &courseDay);
printf("Please enter the time of the course: ");
scanf_s("%d", &courseTime);
i++;
}

最佳答案

在链表中使用动态内存分配。看看malloc。您的列表结构可能如下所示:

typedef struct COURSES {
int courseCode;
int courseDay;
int courseTime;
struct COURSES *next;
} t_Courses;

按如下方式分配列表元素:

    t_Courses *pCourse= malloc(sizeof(t_Courses));

然后像现在一样读取数据,例如:

    scanf_s("%d", &pCourse->courseCode);

管理链表并不简单。我把它作为家庭作业的一部分留给你。互联网和 Stack Exchange 上有很多示例。

关于c - 如何使用用户的输入创建表,表的数量根据用户先前指定的输入而变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58519619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com