#include <stdio.h> #include <assert.h>
int cmp_int(const void *,const void *);
int cmp_doubl(const void *,const void *);
int cmp_char(const void *,const void *);
int cmp_str(const void *,const void *);
int cmp_function(int (*function)(const void *,const void *),/
const void *one,const void *two);
int main(void)
{
int a = 5;
int b = 4;
double a_double = 9.00;
double b_double = 20.00;
char a_char = '5';
char b_char = '5';
char *a_str = "abcdg";
char *b_str = "abcdf";
if ((cmp_function(cmp_int,&a,&b)) == 1)
printf("a > b/n");
else
printf("a <= b/n");
if ((cmp_function(cmp_doubl,&a_double,&b_double)) == 1)
printf("a_double > b_double/n");
else
printf("a_double <= b_double/n");
if ((cmp_function(cmp_char,&a_char,&b_char)) == 1)
printf("a_char > b_char/n");
else
printf("a_char <= b_char/n");
if ((cmp_function(cmp_str,a_str,b_str)) == 1)
printf("a_str > b_str/n");
else
printf("a_str <= b_str/n");
return 0;
}
int cmp_function(int (*function)(const void *,const void*),/
const void *one,const void *two)
{
return function(one,two);
}
int cmp_int(const void *one,const void *two)
{
if ((*((int *)one)) > (*((int *)two)))
return 1;
else
return 0;
}
int cmp_doubl(const void *one,const void *two)
{
if ((*((double *)one)) > (*((double *)two)))
return 1;
else
return 0;
}
int cmp_char(const void *one,const void *two)
{
if ((*((char *)one)) > (*((char *)two)))
return 1;
else
return 0;
}
int cmp_str(const void *one,const void *two)
{
char *temp_one;
char *temp_two;
temp_one = (char *)one;
temp_two = (char *)two;
while ((*temp_one != '/0') && (*temp_two != '/0')){
if (*temp_one > *temp_two)
return 1;
temp_one ++;
temp_two ++;
}
return 0;
}
compare function overload
最新推荐文章于 2024-03-11 00:56:41 发布