compare function overload

来源:互联网 发布:mac 设置分辨率 编辑:程序博客网 时间:2024/05/22 01:43

#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;
}

原创粉丝点击