请问:关于C语言bsearch的用法
请教:关于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)