gpt4 book ai didi

c - 在 C 中对结构进行排序

转载 作者:太空狗 更新时间:2023-10-29 15:55:30 27 4
gpt4 key购买 nike

我正在尝试对 C 中的学生结构进行排序。我正在使用 qsort 并进行简单的字符串比较。不知道为什么我会遇到段错误。我在比较器函数中适本地转换和取消引用结构。一段时间以来,我一直在努力解决这个问题。

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

typedef struct Student {
char * last;
char * first;
char * social;
int age;
float gpa;
enum { ComputerScience = 1, History, Biology } major;
} Student;

void gpa_comp(Student **);
int gpa_comparator(const void * a, const void * b);
void name_comp(Student **);
int name_comparator(const void * a, const void * b);
void make_student_list(Student **);
int student_cmp(const void * a, const void * b);
void print_students (Student ** s, size_t len);
int flt_cmp(const float a, const float b);

int main(int argc, char const *argv[])
{
int i;
struct Student ** students = (struct Student **)malloc(5 * sizeof(struct Student *));

for (i = 0; i < 5; ++i) {
students[i] = (struct Student *)malloc(sizeof(struct Student));
}

make_student_list(students);

printf("Students in no order\n");
print_students(students, 5);

printf("\n");
printf("Students Sorted By First Name Asc\n");
name_comp(students);

return 0;
}

int gpa_comparator(const void * a, const void * b){
const Student * s1 = (const Student *)a;
const Student * s2 = (const Student *)b;
return (int)(100.f*s1->gpa - 100.f*s2->gpa);
}

int name_comparator(const void * a, const void * b){
const Student * s1 = (const Student *) a;
const Student * s2 = (const Student *) b;
return strcmp(s1->first, s2->first);
}

void print_students (Student ** s, size_t len) {
int c;
for (c = 0; c < len; c++){
printf("%s - %s - %2f\n", s[c]->first, s[c]->last, s[c] -> gpa);
}
}

void gpa_comp(Student ** students) {
size_t len = 5;
qsort(students, len, len, gpa_comparator);
print_students(students, len);
}

void name_comp(Student ** students){
qsort(students, 5, sizeof(Student), name_comparator);
print_students(students, 5);
}

void make_student_list(Student ** s){
s[0]->first = "Dave";
s[0]->last = "Johnson";
s[0]->social = "362-89-3284";
s[0]->major = ComputerScience;
s[0]->gpa = 3.5;

s[1]->first = "Sally";
s[1]->last = "Susie";
s[1]->social = "251-43-9220";
s[1]->major = History;
s[1]->gpa = 3.4;

s[2]->first = "Molly";
s[2]->last = "Thomas";
s[2]->social = "123-45-2343";
s[2]->major = Biology;
s[2]->gpa = 3.3;

s[3]->first = "Brett";
s[3]->last = "Bigler";
s[3]->social = "523-364-3462";
s[3]->major = ComputerScience;
s[3]->gpa = 3.9;

s[4]->first = "Sandra";
s[4]->last = "Salami";
s[4]->social = "724-457-2455";
s[4]->major = Biology;
s[4]->gpa = 4.0;
}

最佳答案

您正在对 Student* 数组进行排序,而不是 Student 数组。因此,对 qsort 的调用应该为 size 参数传递 sizeof(Student*) 而不是 sizeof(Student),并且比较器函数需要将 const void * 参数转换为 Student **,并解引用它们两次。

更好的解决方案可能是将数组更改为 Student* 而不是 Student**。然后,您不需要为每个单独的 Student 实例malloc,并且需要更改的代码更少。

关于c - 在 C 中对结构进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13239188/

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