Use callback functions to implement generic algorithms

Source: Internet
Author: User

Another typical function of callback functions is to implement generic algorithms similar to C ++. Such as the implementation of qsort.

The following is a generic function that I write using the features of the callback function. This function is used to find its maximum value among any group of objects. This object can be of the char type, it can also be int ...... let's just look at the program:

 

# Include <stdio. h>

 

Typedef int (* cmp_t) (void *, void *);

 

/* Callback function 1: Compares char objects */

Int cmp_char_data (void * a, void * B)

{

Char para1 = * (char *) );

Char para2 = * (char *) B );

If (para1> para2)

Return 1;

Else if (para1 = para2)

Return 0;

Else

Return-1;

}

 

/* Callback function 2: Compares int objects */

Int cmp_int_data (void * a, void * B)

{

Int para1 = * (int *) );

Int para2 = * (int *) B );

If (para1> para2)

Return 1;

Else if (para1 = para2)

Return 0;

Else

Return-1;

}

 

/* Callback function 3: Compares double objects */

Int cmp_double_data (void * a, void * B)

{

Double para1 = * (double *) );

Double para2 = * (double *) B );

If (para1> para2)

Return 1;

Else

Return-1;

}

 

/* Callback function 4: Compare struct objects */

Typedef struct student

{

Char * Name;

Int score;

} Student_t;

 

Int cmp_struct_data (void * a, void * B)

{

Student_t * para1 = (student_t *);

Student_t * para2 = (student_t *) B;

If (para1-> score> para2-> score)

Return 1;

Else if (para1-> score = para2-> score)

Return 0;

Else

Return-1;

}

 

/* Implement function max */

Void * max (void * data, int num, int size, cmp_t CMP)

{

Void * temp = data;

Int I;

For (I = 1; I <num; I ++)

{

If (CMP (temp, Data + size * I) <0)

{

Temp = Data + size * I;

}

}

Return temp;

}

 

/* Main function */

Int main ()

{

Char c_data [5] = {'A', 'n', 'k', 'P', 'C '};

Int I _data [5] = {1, 20, 50,300, 12 };

Double d_data [5] = {20.2, 130.5, 100.12, 200.23, 45.6 };

Student_t st_data [5] = {"stu1", 56 },{ "stu1", 85 },{ "stu1", 65 },{ "stu1", 96 }, {"stu1", 72 }};

Char max1 = * (char *) max (c_data, 5, sizeof (char), cmp_char_data ));

Int max2 = * (int *) max (I _data, 5, sizeof (INT), cmp_int_data ));

Double max3 = * (double *) max (d_data, 5, sizeof (double), cmp_double_data ));

Student_t * max4 = (student_t *) max (st_data, 5, sizeof (student_t), cmp_struct_data );

Printf ("% C \ t % d \ t % lf \ t % d \ n", max1, max2, max3, max4-> score );

Getchar ();

Return 0;

}

Conversion from http://hi.baidu.com/s_rlzheng/blog/item/ee99512a6cf145315243c1e4.html#0)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.