gpt4 book ai didi

c - 如何使用结构将文件的内容正确传递给 qsort?

转载 作者:太空宇宙 更新时间:2023-11-04 01:11:26 24 4
gpt4 key购买 nike

我试图让程序读取一个文本文件,其中包含诸如“1001 name surname 10 20 30”之类的信息、学生人数、姓名和他的 3 个成绩。这是我的代码:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Student {
int number;
char name[30];
char surname[30];
int midterm1,midterm2,midterm3;
} Student;

int comp(const void * aa, const void * bb)
{
struct Student a = *(struct Student*)aa;
struct Student b = *(struct Student*)bb;
if (a.midterm1==b.midterm1)
return 0;
else if (a.midterm1 < b.midterm1)
return -1;
else
return 1;
} // comp


int main(void)
{
int choice,studentnumber,midterm1,midterm2,midterm3,i,n;
char surname;
FILE *cfPtr;

struct student *name;
name = malloc( 10 * sizeof(Student));

if ((cfPtr = fopen("grades.txt", "r")) == NULL)
return 1;

const int STUDENTSMAX = 100;
struct Student students[STUDENTSMAX];
char buff[1024];
while(1)
{
memset(buff, 0, sizeof(buff));
fgets(buff, sizeof(buff) -1, cfPtr);
if (feof(cfPtr)) {
break;
}
sscanf(buff, "%d %s %s %d %d %d", &students[i].number, students[i].name, students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
printf("%4d %15s %15s %10d %10d %10d\n", students[i].number, students[i].name, students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
i++;
} // while

while (!feof(cfPtr))
{
fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
i++;
} // while

printf("What would you like to do? \n"
"1- Sort according to midterm 1\n"
"2- Sort according to midterm 2\n"
"3- Sort according to midterm 3\n"
"4- Exit\n");
scanf("%d",&choice);

scanf("%d",&choice);
switch (choice) {
case 1:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm1);
} // for
break;
case 2:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm2);
} // for
break;
case 3:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm3);
} // for
break;
} // switch
} // main?
fclose(cfPtr);


system("PAUSE");
return 0;
} // the editor failed to find the corresponding '{' !OP should fix this

此当前代码返回此错误:

95 C:\Users\UseR\Desktop\main.c [Warning] parameter names (without types) in function declaration 
95 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
98 C:\Users\UseR\Desktop\main.c syntax error before string constant
98 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
C:\Users\UseR\Desktop\Makefile.win [Build Error] [main.o] Error 1

程序正在编译和运行,没有任何问题,但它仍然报错并且根本没有排序。我在这里做错了什么?

编辑:这是我的输入文件;

100201 al beaver 40 50 70
100202 andrew matthews 30 90 75
100203 leah doga 60 55 80
100204 rob kurt 45 80 60
100205 aliah devon 65 70 50
100206 sally pir 70 40 85
100207 eric bekta 75 65 55
100208 nile coul 55 75 65
100209 mina umur 72 60 90
100210 john hot 73 63 87

附言是的,这是在任何人问之前的作业,我的时间不多了,我真的已经尝试了好几天,但没有任何稳固的进展。我被问到的主要是“

  1. Sort grades of chosen exam in decreasing order and output file will contain only three columns: names,surnames and scores of chosen exam.
  2. Define a structure named “student”, to store name,surname and scores of each student under the same variable name. So all data in the file will be stored in an array of new data type named “student”."

最佳答案

你在这段代码中有很多问题,我将逐节回顾我更改的内容。由于这是家庭作业,我不会发布完全修改后的程序,但您(或将来发现此内容的人)可以使用足够的信息来让您的代码正常工作。

首先,您不需要类型定义结构。只需声明一个结构:

改变这个:

 typedef struct {
int number;
char name[30];
char surname[30];
int midterm1,midterm2,midterm3;
} Student;

对此:

struct Student {
int number;
char name[30];
char surname[30];
int midterm1,midterm2,midterm3;
};

第二个问题是您的比较函数原型(prototype)不正确。 qsort()期望回调恰好采用 const void* 类型的两个参数, 所以把它改成这样:

int comp(const void * aa, const void * bb)

这将消除您看到的警告,但它实际上只是一个警告。你不是在打电话 qsort()用正确的论点。我们稍后再谈,我在运行你的程序时发现了其他问题。

第三个问题是没有处理文件打开失败,导致segmentation fault(老师一般给seg faults打分),所以不是这样的:

 if ((cfPtr = fopen("grades.txt", "r")) == NULL)
printf("File cannot be opened.\n");
else {

只是退出,你不能运行,没有必要再缩进永远不会达到的逻辑。例如:

 if ((cfPtr = fopen("grades.txt", "r")) == NULL) {
printf("File cannot be opened.\n");
return 1;
}

第四个问题是,您正在从文件中读取虚假输入而不是对其进行测试。我更喜欢使用缓冲区和 sscanf()而不是 fscanf() ,但这只是偏好。不管怎样,我就是这样做的:

    const int STUDENTSMAX = 100;
struct Student students[STUDENTSMAX];
char buff[1024];
while (1)
{
memset(buff, 0, sizeof(buff));
fgets(buff, sizeof(buff) -1, cfPtr);
if (feof(cfPtr)) {
break;
}
sscanf(buff, "%d %s %s %d %d %d", &students[i].number, students[i].name, students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
printf("%4d %15s %15s %10d %10d %10d\n", students[i].number, students[i].name, students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
i++;
}

请注意,您必须添加 #include <string.h>如果你走那条路并使用 memset()在每次迭代中重置缓冲区。另请注意,我取消引用 char[]您在结构中声明的数组(在向给定模式解释 &name 时,请参阅缺少 surname)。

然后,这段代码:

scanf("%d",&choice);

while (choice != 4) {


switch (choice) {

case 1:
qsort(students,10,sizeof(int),comp);
printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1);

}
}
}

... 正在进入无限循环。一旦他们选择 1,结构将被排序并永远如此快速地打印,因为没有选项可以处理任何其他退出选项,而且它飞得如此之快,以至于用户无论如何都无法输入它。我改成了这样,比较合理:

    scanf("%d",&choice);
switch (choice) {
case 1:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm1, students[n].midterm2, students[n].midterm3);
}
break;
}

注意 break .你之前没有突破switch()while()你需要同时做这两件事。没有 while()周围switch()在我修改的代码中。

现在,对 qsort() 的修改调用应该向您显示输入未排序的原因。你有这个:

qsort(students,10,sizeof(int),comp);

第二个参数可以是i , 自 i知道你有多少元素。第三个参数需要是您要排序的成员 的大小,而不是整数的大小(不太确定您是如何走上这条路的)。这就是排序结构被截断的原因。

来自手册页:

   void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

DESCRIPTION The qsort() function sorts an array with nmemb elements of size size. The base argument > points to the start of the array.

如您所见,它想要(按此顺序):

  • 你在整理什么?
  • 它有多少成员?
  • 每个成员的大小是多少?
  • 我应该使用什么回调函数来做决定?

因为你要往返于void *Student在你的回调中,你需要修改它:

int comp(const void * aa, const void * bb)
{
struct Student a = *(struct Student*)aa;
struct Student b = *(struct Student*)bb;
if (a.midterm1 == b.midterm1)
return 0;
else if (a.midterm1 < b.midterm1)
return -1;
else
return 1;
}

参见 this answer了解更多详情。

最后一击,main应该是 int main(void) ,因为您不希望出现争论。

您还需要进行一些其他修改,或者您可以根据我提供的反馈快速修复现有内容。我摆脱了所有未使用的值,您分配的结构(不确定那是什么)并真正清理了它。如果你想取得好成绩,我建议你也这样做。

还有很多我没有完全解释,因为这个答案已经足够长了,我不想缩短你的更多研究。

关于c - 如何使用结构将文件的内容正确传递给 qsort?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11914284/

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