我需要使用C创建一个表,该表接受用户的输入,而不使用数组、动态数组或动态内存分配
我不允许使用数组,也不允许使用本课程中未学习过的任何东西,例如:指针、动态内存分配等。到目前为止,我学习的是:函数和循环。我需要列出用户在第一次我需要使用C创建一个表,该表接受用户的输入,而不使用数组、动态数组或动态内存分配,c,C,我不允许使用数组,也不允许使用本课程中未学习过的任何东西,例如:指针、动态内存分配等。到目前为止,我学习的是:函数和循环。我需要列出用户在第一次scanf_s调用中指定的所有课程代码、天数和时间。我不知道不使用数组如何继续。如有任何提示/帮助,将不胜感激 printf("Please enter the number of courses you'd like to take: "); int numOfCourses; scanf_s("%d", &numOfCourses); int
scanf_s
调用中指定的所有课程代码、天数和时间。我不知道不使用数组如何继续。如有任何提示/帮助,将不胜感激
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++;
}
printf(“请输入您想修的课程数:”;
国际货币基金组织课程;
scanf_s(“%d”&numOfCourses);
国际课程代码;
国际课程日;
国际课程时间;
int i=0;
而(我的课程){
printf(“请输入课程代码:”);
scanf_s(“%d”和courseCode);
printf(“请输入课程日期:”);
scanf_s(“%d”和courseDay);
printf(“请输入课程时间:”);
扫描时间(“%d”和课程时间);
i++;
}
建议:
1) 创建一个包含单个学生所有信息的结构
2) 在本地目录或/tmp
3) 对于每个学生,在收集信息后,将结构写入文件
--或--
给定学生可以选修的最大课程数,则代码类似于:
int course1day;
int course1time;
int course1code;
(repeat for MAX number of courses, increment the digit for each course)
需要考虑的是:收集这些信息后,代码将如何处理这些信息?假设
courseCode
是表示课程的一位数字,您可以执行以下操作:
printf("Please enter the number of courses you'd like to take: ");
int numOfCourses;
scanf_s("%d", &numOfCourses);
int courseCode;
int courseDay;
int courseTime;
// unsigned long to have the most possible space
unsigned long courseCodeArray = 0;
unsigned long courseDayArray = 0;
unsigned long courseTimeArray = 0;
int i = 0;
while (i < numOfCourses) {
// single digit code
printf("Please enter the code of the course: ");
scanf_s("%d", &courseCode);
// 1 = Monday, 2 = Tuesday, 3 = Wednesday , etc.
printf("Please enter the day of the course: ");
scanf_s("%d", &courseDay);
// 1 = 8:00, 2 = 10:00, 3 = 12:00, etc.
printf("Please enter the time of the course: ");
scanf_s("%d", &courseTime);
courseCodeArray += courseCode;
courseDayArray += courseDay;
courseTimeArray += courseTime;
courseCodeArray = courseCodeArray * 10;
courseDayArray = courseDayArray * 10;
courseTimeArray = courseTimeArray * 10;
i++;
}
printf(“请输入您想修的课程数:”;
国际货币基金组织课程;
scanf_s(“%d”&numOfCourses);
国际课程代码;
国际课程日;
国际课程时间;
//unsigned long以获得最大可能的空间
无符号长courseCodeArray=0;
无符号长航程dAyarray=0;
无符号长航程平均航程=0;
int i=0;
而(我的课程){
//一位数代码
printf(“请输入课程代码:”);
scanf_s(“%d”和courseCode);
//1=周一,2=周二,3=周三等。
printf(“请输入课程日期:”);
scanf_s(“%d”和courseDay);
//1=8:00、2=10:00、3=12:00等。
printf(“请输入课程时间:”);
扫描时间(“%d”和课程时间);
courseCodeArray+=courseCode;
courseDayArray+=courseDay;
CourseTimerary+=courseTime;
CourseCodearay=courseCodeArray*10;
courseDayArray=courseDayArray*10;
CourseTimerarray=CourseTimerarray*10;
i++;
}
通过这种方式,您将拥有一个由单个int组成的“数组”,只要课程代码是1到9之间的整数,该数组就可以工作
例如,数字:435
、132
和431
意思是:4-1-4
课程4周一14:00,3-3-3
课程3周三12:00和5-2-1
课程5周二8:00
当然,您需要编写一些检查,以确保用户只输入有效的数字,不超过最大值unsigned long
并做出更好的设计选择,但我认为这就是您的教授的意思
使您的代码健壮且易于阅读
所以我不会为你做任何事;)
这样,只要知道如何将单个数字“解码”为其表示形式,就可以将多个值存储在单个
无符号long
或int
中。只需将数组除以10,删除最后的0,然后逐位读取生成的“数组”数字并将其转换回。所有课程代码列表的含义是什么?顺便说一句,你可以先在代码中使用一些好的缩进。是的,当我把它复制到这里时,格式被弄乱了。基本上,用户必须指定他有多少个课程,并且必须输入所有课程的代码以及它们的时间和天数,只使用整数。程序必须根据所有用户输入创建一个表;但是,我不知道如何在不使用数组的情况下保存用户的输入。OT:调用任何scanf()
函数族时,始终检查返回值(而不是参数值),以确保操作成功。注意:这些函数返回成功的输入格式转换说明符的数目。或EOF。除了预期的转换说明符数以外的任何返回值都表明发生了错误。学生可以修的最大课程数是多少?@BallenAbdullah您可以在问题中修正缩进。至于桌子,你还需要再解释一下。这张桌子是什么?在文件中?在屏幕上?在网页上?如果不允许使用data struct或intermediate将其保存到文件中,则无法将其显示在获取输入的屏幕上。最大课程数未知。我如何将其纳入我的代码中?对于最大课程数,建议一些超出学生实际可修课程数的大数字,例如20