我正在编写一个程序,将天数打印到供学生使用的文件中。 低端是他们将从一个列表中进行选择,并击中了我下面的switch statemtnt,在其中调用了我的打印语句。在print语句中,它将具有数据结构的排序功能。但是我对如何编写函数进行排序感到有些困惑。我想按MWF和TR排序
在这里出现查询语句,在查询语句下方是天数排序。
#include<stdio.h>
#include<string.h>
#include "student.h"
class_t classes[20];
void queryUser(class_t classes[], int n){
// This will prompt the user what they would like to do within the program
// the idea here is to decide how each function needs to run
char year;
char user = 'a';
char *time = " ";
char *day = " ";
char nl;
// FILE* iptr = fopen("/public/csci112sp19/pgm1/classes.txt", "r");
FILE* fptr = fopen("out.txt", "w");
while(user != 'Q'){
printf("What would you like to do?\n");
printf("Print all classes: A\n");
printf("Print all classes by day: B\n");
printf("Print Classes by time: C\n");
printf("Print all classes for student year: D \n");
printf("Quit : Q\n");
scanf("%c%c", &user, &nl);
// there will be an switch case statement in this function
// similar to if statements, if a case is called
// the function will dictate what will print the information
// stored in the selected array
// put lower case in also
printf("user is %c and n is %d\n", user, n);
switch(user){
case 'A':
printAllClasses(classes, n,fptr);
break;
case 'B':
printf("What days would you like to sort by MWF or TR?");
scanf("%s", day);
scanf("%c", &nl);
printClassDay(classes, day , n, fptr);
break;
case 'C':
printf("what time and what day");
scanf("%s %s", time, day);
scanf("%c", &nl);
printClassTime(classes, day, time, n, fptr);
//search array for day and time matching
break;
case 'D':
printf("Please enter what year in school, Fresh: 1, Soph: 2, JR: 3, SR, 4\n");
scanf("%c", &year);
scanf("%c", &nl);
yearMethod(classes, year, n, fptr);
break;
case 'Q':
break;
default:
printf("Please use capital letters\n");
}
}
}
$
void sortbyclassdays(class_t classes[], int n, FILE* fptr){
int i, j, doswap = 0;
class_t temp;
for (i = 0; i < n; ++i) {
for (j = i; j < n; ++j) {
doswap = strcmp(classes[i].classDays, classes[j].classDays);
}
if (doswap > 0) {
temp = classes[i];
classes[i]= classes[j];
classes[j] = temp;
}
}
return;
}
void printClassDay(class_t classDays[], char* day, int n, FILE* fptr){
//this method will print classes by days selected by user
sortbyclassdays(classes, n, fptr);
for(int i = 0; i < n; ++i){
if(strcmp(day,"MWF")){
printOneClass(classes[i], fptr);
}
else if(strcmp(day, "TR")){
printOneClass(classes[i], fptr);
}
}
return;
答案 0 :(得分:0)
关于:
printf("What would you like to do?\n");
printf("Print all classes: A\n");
printf("Print all classes by day: B\n");
printf("Print Classes by time: C\n");
printf("Print all classes for student year: D \n");
printf("Quit : Q\n");
函数printf()
是CPU周期的昂贵消耗者。
“菜单”将难以阅读和理解。
建议:
printf("%s",
"\nWhat would you like to do?\n"
"A: Print all classes:\n"
"B: Print all classes by day:\n"
"C: Print Classes by time:\n"
"D: Print all classes for student year:\n"
"Q: Quit\n");
甚至更好:
puts( "\nWhat would you like to do?\n"
"A: Print all classes:\n"
"B: Print all classes by day:\n"
"C: Print Classes by time:\n"
"D: Print all classes for student year:\n"
"Q: Quit\n");
OT:关于:
scanf("%c%c", &user, &nl);
在调用任何scanf()系列函数时,请始终检查返回的值以确保操作成功。注意:返回的值为EOF或成功的输入格式说明符转换的次数。在当前情况下,建议:
if( scanf("%c%c", &user, &nl) != 2)
{
// handle error
}
关于这种陈述:
scanf("%c", &nl);
强烈建议使用:
int nl;
while( (nl = getchar() ) != '\n' && nl != EOF );
关于:
for (j = i; j < n; ++j)
{
doswap = strcmp(classes[i].classDays, classes[j].classDays);
}
这将遍历所有“ classDays”,然后退出循环,因此下一个代码块:,以:开头:
if (doswap > 0)
将仅查看数组中的最后一个条目。因此,其他所有数据都不会排序。
您的编译器“可以”让您摆脱困境:
default:
printf("Please use capital letters\n");
但是,在致电printf()
之后应该是:
break;
关于:
switch(user)
如果用户输入小写字母,这将不起作用。
建议:
switch( toupper(user) )
关于:
char user = 'a';
名称user
并不代表其内容。
建议更有意义的内容,例如:
selection
关于:
void sortbyclassdays(class_t classes[], int n, FILE* fptr){
未使用函数参数FILE *fptr
。建议在代码的其余部分中删除该功能参数及其所有引用
关于:
if(strcmp(day,"MWF")){
printOneClass(classes[i], fptr);
}
为什么跳过在“天”字段中具有“ MWF”的记录?
关于:
else if(strcmp(day, "TR")){
printOneClass(classes[i], fptr);
}
为什么跳过day
字段中具有“ TR”的记录?
发布的代码缺少main()
函数,并且缺少该函数的最后}
:printClassDay()
强烈建议您使用实际有效的排序算法替换函数sortbyclassdays()
的主体。建议:bubble sort
发布的代码中缺少功能printOneClass()
。
OT:声明为返回void
的函数具有'return`语句。不需要那句话
关于如下语句:
temp = classes[i];
classes[i]= classes[j];
classes[j] = temp;
这些语句实际上不会移动整个struct class_t
建议:
memcpy( &temp, &classes[i], sizeof( class_t ) );
memcpy( &classes[i], &classes[j], sizeof( class_t ) );
memcpy( &classes[j], &temp, sizeof( class_t ) );