结构数组排序成员 [英] sorting members of structure array
问题描述
给定一个结构数组(C语言),我试图用数字顺序打印出性别组的结果,并在亚秩序。例如:
结构员工{
焦炭性别[13]
焦炭名[13];
INT ID;
};
说我定义结构阵列像这样:
结构员工信息[2] = {{男性,马特,1234},{女性,杰西卡,2345},{男性,乔希,1235}};
我怎么能去打印结果像
1234马特
1235乔希
2345杰西卡
您需要实现一个排序功能的结构,你需要比较
INT比较(常量无效* S1,常量无效* S2)
{
员工结构* E1 =(结构*员工)S1;
员工结构* E2 =(结构*员工)S2;
INT gendercompare = STRCMP(E1->性别,E2->性别);
如果(gendercompare == 0)/ *同性别等等排序ID * /
返回E1-> ID - E2-> ID;
其他
返回-gendercompare; / *负提出男性作为第一个问题中* /
}
和再使用的qsort标准库。
的qsort(数据,计数的sizeof(结构员工),比较);
里面的比较功能,你可能要检查的ID都是平等的,那么你就可以按名称排序(同样使用的strcmp()
),但是你喜欢。
肯
编辑:刚刚编译和固定这件事。这里有一个小测试程序
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT; 员工结构{
焦炭性别[13];
焦炭名[13];
INT ID;
}; INT比较(常量无效* S1,常量无效* S2)
{
员工结构* E1 =(结构*员工)S1;
员工结构* E2 =(结构*员工)S2;
INT gendercompare = STRCMP(E1->性别,E2->性别);
如果(gendercompare == 0)/ *同性别等等排序ID * /
返回E1-> ID - E2-> ID;
其他
返回-gendercompare;
} 主要()
{
INT I;
结构员工信息[] = {{男性,马特,1234},{女性,杰西卡,2345},{男性,乔希,1235}}; 对于(I = 0;我3; ++ⅰ)
的printf(%d个\\ t%S \\ t%S \\ n,信息[I] .ID,资讯[I] .gender,资讯[我]。名称); 的qsort(资讯,3,的sizeof(结构员工),比较); 对于(I = 0;我3; ++ⅰ)
的printf(%d个\\ t%S \\ t%S \\ n,信息[I] .ID,资讯[I] .gender,资讯[我]。名称);
}
通过输出:
$ ./a.exe
1234男马特
2345女杰西卡
1235男性乔希
1234男马特
1235男性乔希
2345女杰西卡
Given a structure array (in C) I am attempting to print out the results in groups of gender and in sub order by numerical order. For example:
struct employee{
char gender[13]
char name[13];
int id;
};
Say I define the structure array like so:
struct employee info[2]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
How could I go about printing the results like
1234 Matt
1235 Josh
2345 Jessica
You'll need to implement a sorting function that compares the structs as you require
int compare(const void *s1, const void *s2)
{
struct employee *e1 = (struct employee *)s1;
struct employee *e2 = (struct employee *)s2;
int gendercompare = strcmp(e1->gender, e2->gender);
if (gendercompare == 0) /* same gender so sort by id */
return e1->id - e2->id;
else
return -gendercompare; /* the minus puts "male" first as in the question */
}
And then use qsort from the standard library.
qsort(data, count, sizeof(struct employee), compare);
Inside the compare function you may want to check for id being equal, then you can sort by name (also using strcmp()
) however you like.
Ken
Edit: Just compiled and fixed this up. Here's a little test program
#include <stdio.h>
#include <stdlib.h>
struct employee{
char gender[13];
char name[13];
int id;
};
int compare(const void *s1, const void *s2)
{
struct employee *e1 = (struct employee *)s1;
struct employee *e2 = (struct employee *)s2;
int gendercompare = strcmp(e1->gender, e2->gender);
if (gendercompare == 0) /* same gender so sort by id */
return e1->id - e2->id;
else
return -gendercompare;
}
main()
{
int i;
struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
for (i = 0; i < 3; ++i)
printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
qsort(info, 3, sizeof(struct employee), compare);
for (i = 0; i < 3; ++i)
printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
}
With output:
$ ./a.exe
1234 male Matt
2345 female Jessica
1235 male Josh
1234 male Matt
1235 male Josh
2345 female Jessica
这篇关于结构数组排序成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!