在这种情况下,我得到一个结构如下:
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] 删除。
我来说两句