如何对具有多个条件的结构数组进行排序?

教授

在这种情况下,我得到一个结构如下:

struct student_list{
    int id;
    char *name[50];
    char department[100];
    char program[100];
    float cgpa;
}studlist[5];

如何对列表进行排序,同时指示姓名的字母顺序、ID 号的升序和部门名称的字母顺序?

以下是我所做的,但结果是错误:

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

struct student_list{
    int id;
    char name[50];
    char department[100];
    char program[100];
    float cgpa;
}studlist[5];

int comp(const void *a, const void *b){
    int result;
    const char **str_a=(const char **)a;
    const char **str_b=(const char **)b;
    return strcmp(*str_a, *str_b);
}

main(){
    int num, i, j;
    char *temp;
    
    printf("Number of student?:");
    scanf(" %d", &num);
    
    printf("\n-----------------------------------------------------------");
    printf("\nPlease enter all information in capital letter!!\n");
    
    for(i=0; i<num; i++){
        printf("\nStudent %d", i+1);
        printf("\nEnter student ID:");
        scanf(" %d", &studlist[i].id);
        printf("\nEnter student name:");
        scanf(" %[^\n]", &studlist[i].name);
        printf("\nEnter student department:");
        scanf(" %[^\n]", &studlist[i].department);
        printf("\nEnter student program:");
        scanf(" %[^\n]", &studlist[i].program);
        printf("\nEnter student cgpa:");
        scanf(" %f", &studlist[i].cgpa);
    }
    
    qsort(studlist.name, num, sizeof(char*), comp);
    
    printf("\n------------------Student Information----------------------");
    printf("\nID\t\tName\t\tDepartment\t\tProgram\t\t\tCGPA");
    for(i=0; i<num; i++){
        printf("\n%d\t\t%s\t\t%s\t\t%s\t\t\t%.2f", studlist[i].id, studlist[i].name, studlist[i].department, studlist[i].program, studlist[i].cgpa);
    }
    
    return 0;
}

应用答案中建议的更改后,我现在有以下代码:

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

struct student_list{
    int id;
    char name[50];
    char department[100];
    char program[100];
    float cgpa;
}studlist[5];

int comp(const void *a, const void *b);

int comp(const void *a, const void *b){
    int result;
    struct student_list* stud_a;
    struct student_list* stud_b;
    
    stud_a=(struct student_list*)a;
    stud_b=(struct student_list*)b;
    
    result=strcmp(stud_a->name, stud_a->name);
    if(result!=0){
        return result;
    }   
    
    result=strcmp(stud_a->id, stud_a->id);
    if(result!=0){
        return result;
    }   
    
    result=strcmp(stud_a->department, stud_a->department);
    if(result!=0){
        return result;
    }   
}

main(){
    int num, i, j;
    
    printf("Number of student?:");
    scanf(" %d", &num);
    
    printf("\n-----------------------------------------------------------");
    printf("\nPlease enter all information in capital letter!!\n");
    
    for(i=0; i<num; i++){
        printf("\nStudent %d", i+1);
        printf("\nEnter student ID:");
        scanf(" %d", &studlist[i].id);
        printf("\nEnter student name:");
        scanf(" %[^\n]", &studlist[i].name);
        printf("\nEnter student department:");
        scanf(" %[^\n]", &studlist[i].department);
        printf("\nEnter student program:");
        scanf(" %[^\n]", &studlist[i].program);
        printf("\nEnter student cgpa:");
        scanf(" %f", &studlist[i].cgpa);
    }
    
    qsort(studlist, num, sizeof(struct student_list), comp);
    
    printf("\n------------------Student Information----------------------");
    printf("\nID\t\tName\t\tDepartment\t\tProgram\t\t\tCGPA");
    for(i=0; i<num; i++){
        printf("\n%d\t\t%s\t\t%s\t\t%s\t\t\t%.2f", studlist[i].id, studlist[i].name, studlist[i].department, studlist[i].program, studlist[i].cgpa);
    }
    
    return 0;
}

现在,我无法在主函数中将链接int comp(const void *a, const void *b);到 my qsort(),另一个问题是我无法同时比较字符串和整数int comp(const void *a, const void *b);

灌木丛

这不编译:

qsort(studlist.name, num, sizeof(char*), comp);

因为studlist是一个结构数组,而不是单个结构,因此没有成员。

似乎您的意图基于此,并且比较功能是按名称排序,但这不是您这样做的方式。qsort函数将您的数组作为第一个参数,将数组元素的大小作为第三个参数。所以你会这样称呼它:

qsort(studlist, num, sizeof(struct student_list), comp);

然后比较函数的参数将是指向结构类型的指针:

int comp(const void *a, const void *b){
    const struct student_list *student_a = a;
    const struct student_list *student_b = b;
    return strcmp(student_a->name, student_b->name);
}

现在可以访问比较函数中的完整结构,您可以strcmp根据其他字段进行额外检查是否返回 0。

关于您更新代码中的这一点:

result=strcmp(stud_a->id, stud_a->id);

strcmp函数用于比较字符串,但该id字段具有类型int所以你会做这样的事情:

if (stud_a->id < stud_b->id) {
    result = -1;
} else if (stud_a->id > stud_b->id) {
    result = 1;
} else {
    result = 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何对具有条件的多个数组进行排序

如何对具有多个条件的字符串数组进行排序?

如何使用PHP对具有多个条件的JSON进行排序?

如何对具有多个项目的数组进行排序-Javascript

对具有多个元素的数组进行排序

如何使用 qsort 对具有一些 NULL 值的结构数组进行排序

如何在JavaScript或jQuery中对每个数组记录具有多个对象的数组进行排序

如何对具有多个字典和数组的数组进行排序

如何对具有多个先决条件的字符串进行排序?

对具有多个条件的嵌套哈希进行排序

在JavaScript中对具有许多条件的数组进行排序

如何对具有多个日期时间列的 javascript 数组进行排序

在PHP中对具有多个值的多维数组进行排序

用Javascript对具有多个值的数组进行排序

根据多个条件对结构进行排序?

用C中的冒泡排序通过多个条件对结构数组进行排序

如何基于多个条件对数组进行排序?

打字稿:如何按多个条件对数组进行排序

按层次结构和名称对具有层次结构的对象数组进行排序

如何对具有特殊条件的列表元组进行排序?

如何对具有两个条件的列表进行排序

如何对具有多个定界符的文件进行排序?

如何对具有多个类的div /帖子进行排序?

如何对具有多个输入的字典进行排序

如何对具有json对象组的json数组进行排序

如何对具有唯一值的数组进行排序

如何对具有日期属性的对象数组进行排序?

基于多个条件对多维数组进行排序

如何对字符串和整数值,具有字母顺序和“反向”-字母顺序的多个属性的数组进行排序