请教:关于C语言bsearch的用法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*compfn)(const void*, const void*);
struct date
{
char m[10]; /**< January 等等 */
int d; /**< 日 1 2 3 4 5 6.。。。。。 */
int y; /**< 年 90-99 00-12 */
};
int cmp_month(const char *m1, const char *m2)
{
int i, n, n1, n2;
static const char *months[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
};
n = sizeof(months) / sizeof(months[0]);
n1 = n2 = -1;
for (i = 0; i < n; ++i) {
if (n1 < 0 && strcmp(m1, months[i]) == 0)
n1 = i;
if (n2 < 0 && strcmp(m2, months[i]) == 0)
n2 = i;
if (n1 >= 0 && n2 >= 0)
break;
}
return n1 - n2;
}
int cmp_year(const void *v1, const void *v2)
{
int temp;
const struct date *y1, *y2;
y1 = (const struct date *)v1;
y2 = (const struct date *)v2;
if ((temp = (y1->y != y2->y)) != 0) {
if (y1->y >= 70 && y2->y >= 70)
return y1->y - y2->y;
if (y1->y >= 70 && y2->y < 70)
return -1;
if (y1->y < 70 && y2->y < 70)
return y1->y - y2->y;
if (y1->y < 70 && y2->y >= 70)
return +1;
}
}
int cmp_day(const void *v1, const void *v2)
{
const struct date *d1, *d2;
d1 = (const struct date *)v1;
d2 = (const struct date *)v2;
return d1->d - d2->d;
}
int main(int argc, char *argv[])
{
int i, n,;
struct date date[] = {
{"January", 1, 01},
{"January", 1, 00},
{"February", 28, 99},
{"July", 17, 12},
{"September", 10, 12},
{"July", 1,00},
{"June", 30, 90},
{"August", 25, 06},
{"May", 27, 07},
{"October", 1, 03},
};
n = sizeof(date) / sizeof(date[0]);
qsort(date, n, sizeof(struct date), &cmp_day);
qsort(date, n, sizeof(struct date), (compfn)cmp_month);
qsort(date, n, sizeof(struct date), &cmp_year);
for (i = 0; i < n; ++i)
printf("%s %d %02d\n", date[i].m, date[i].d, date[i].y);
return 0;
}
if ((ret = (d1->y != d2->y)) != 0) {
if ((ret = (d1->y - d2->y)) != 0) {
/**
* @file datesort.c
* @brief
*/
struct date
{
char m[20]; /**< January 等等 */
int d; /**< 日 1 2 3 4 5 6.。。。。。 */
int y; /**< 年 90-99 00-12 */
};
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp_month(const char *m1, const char *m2)
{
int i, n, n1, n2;
static const char *months[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
};
n = sizeof(months) / sizeof(months[0]);
n1 = n2 = -1;
for (i = 0; i < n; ++i) {
if (n1 < 0 && strcmp(m1, months[i]) == 0)
n1 = i;
if (n2 < 0 && strcmp(m2, months[i]) == 0)
n2 = i;
if (n1 >= 0 && n2 >= 0)
break;
}
return n1 - n2;
}
int cmp_date(const void *v1, const void *v2)
{
int ret;
const struct date *d1, *d2;
d1 = (const struct date *)v1;
d2 = (const struct date *)v2;
if ((ret = (d1->y - d2->y)) != 0) {
if (d1->y >= 70 && d2->y >= 70)
return ret;
if (d1->y >= 70 && d2->y < 70)
return -1;
if (d1->y < 70 && d2->y < 70)
return ret;
if (d1->y < 70 && d2->y >= 70)
return +1;
}
if ((ret = cmp_month(d1->m, d2->m)) != 0)
return ret;
return d1->d - d2->d;
}
int main(int argc, char *argv[])
{
int i, n;
struct date *ptr;
struct date key = {"January", 01, 01};
struct date date[] = {
{"January", 1, 01},
{"January", 1, 00},
{"February", 28, 99},
{"July", 17, 12},
{"September", 10, 12},
{"July", 1,00},
{"June", 30, 90},
{"August", 25, 06},
{"May", 27, 07},
{"October", 1, 03},
};
n = sizeof(date) / sizeof(date[0]);
qsort(date, n, sizeof(struct date), &cmp_date);
for (i = 0; i < n; ++i)
printf("%s %d %d\n", date[i].m, date[i].d, date[i].y);
ptr = bsearch(&key, date, n, sizeof(struct date), &cmp_date);
if (ptr != NULL) {
printf("\n");
printf("date[%d]:\n", (int)(ptr - date));
printf("%s %d %d\n", ptr->m, ptr->d, ptr->y);
}
return 0;
}