#include
#include
/* 用于排序的结构 */
typedef struct t_s {
int a;
int b;
}t_t;
int int_cmp(const void *a, const void *b)
{
int *pa, *pb;
if (a == NULL || b == NULL) {
printf("intcmp invalid paras\n");
return 0;
}
pa = a;
pb = b;
if (*pa > *pb) {
return 1;
} else if (*pa == *pb) {
return 0;
} else {
return -1;
}
}
int t_cmp(const void *a, const void *b)
{
t_t *pa, *pb;
if (a == NULL || b == NULL) {
printf("Invalid paras\n");
return 0;
}
pa = (t_t *)a;
pb = (t_t *)b;
if (pa->a > pb->a) {
return 1;
} else if (pa->a == pb->a) {
return int_cmp(&pa->b, &pb->b);
} else {
return -1;
}
}
int main()
{
t_t *array;
int n, i;
printf("please input member's numbers\n");
scanf("%d", &n);
array = (t_t *)malloc(n * sizeof(t_t));
if (array == NULL) {
printf("malloc failed!\n");
return -1;
}
for (i = 0; i < n; i++) {
printf("please input %d members!\n", i + 1);
scanf("%d %d", &array[i].a, &array[i].b);
}
/* no qsort */
printf("before qsort!\n");
for (i = 0; i < n; i++) {
printf("%d %d\n", array[i].a, array[i].b);
}
qsort(array, n, sizeof(t_t), t_cmp);
printf("after qsort!\n");
for (i = 0; i < n; i++) {
printf("%d %d\n", array[i].a, array[i].b);
}
#if 0
t_t a, b;
printf("please iniput a.a a.b\n");
scanf("%d %d", &a.a, &a.b);
printf("please iniput b.a b.b\n");
scanf("%d %d", &b.a, &b.b);
if (t_cmp(&a, &b) == 0) {
printf("a == b\n");
} else {
printf("a != b\n");
}
#endif
return 0;
}
阅读(1120) | 评论(0) | 转发(0) |