#define _CRT_SECURE_NO_WARNINGS#include #include #include #include typedef struct//定义学生结构体{ char name[20]; char number[20]; int score;}Student;int struct_cmp(const void *elem1, const void *elem2){ assert(elem1); assert(elem2);//断言 return ((*(Student*)elem1).score - (*(Student*)elem2).score);//通过比较student的分数确定大小}int int_cmp(const void *elem1, const void *elem2)//整型比较函数{ assert(elem1); assert(elem2);//断言 return (*(int *)elem1 - *(int *)elem2); //elem1和elem2为两个int变量的地址,所以将elem1和elem2强制类型转换为int*类型, //然后截引用,访问两个int 变量,返回值为两个int 变量的差,如果第一个数大于第二个 //返回整数,相等返回0,小于返回负数。}int str_cmp(const void *elem1, const void *elem2){ assert(elem1); assert(elem2);//断言 return strcmp((char*)*(int *)elem1,(char*)*(int *)elem2); //elem1和elem2为存放字符串地址的字符指针,所以需要访问4个字节, //将其强制类型转换为int*,然后截引用访问到字符串指针,最后将其强制类型转换为(char *).}void swp(void *elem1, void *elem2, size_t width)//size_t width为类型所占的空间,由于不知道交换的是什么类型, //所以需要多传入一个参数(表示类型的大小),然后以字节为单位逐个交换{ assert(elem1); assert(elem2);//断言 size_t i = 0; //逐字节的交换,进而交换两个变量 for (i = 0; i < width; i++) { char tmp = *((char*)elem1 + i); *((char*)elem1 + i) = *((char*)elem2 + i); *((char*)elem2 + i) = tmp; }}void bubble(void *arr, size_t num, size_t width, int(*compare)(const void *elem1, const void *elem2)) //int(*compare)(const void *elem1, const void *elem2为一个比较函数指针{ assert(arr); assert(compare);//断言 size_t i = 0, j = 0; for (i = 0; i < num - 1; i++) { for (j = 0; j < num - 1 - i; j++) { if (compare(((char*)arr + j*width), (char*)arr + (j + 1)*width)>0)//由于不知道需要比较的函数类型,所以利用类型的大小作为函数参数, //并统一转换为(char *)进行逐字节的转换(char*)arr + j*width { swp((char*)arr + j*width, (char*)arr + (j + 1)*width, width); } } }}int main(){ char *arr1[] = { "amdgdj","lsansk","aaaaa","ueojjfdh","dcsaouid" }; int sz = sizeof(arr1) / sizeof(arr1[0]); bubble(arr1, sz, sizeof(arr1[0]), str_cmp); int i = 0; for (i = 0; i < sz; i++) { printf("%s\n", arr1[i]); } int arr2[] = { 1,3,7,7,6,4,9,4,3,8,5 }; sz = sizeof(arr2) / sizeof(arr2[0]); bubble(arr2, sz, sizeof(arr2[0]), int_cmp); for (i = 0; i < sz; i++) { printf("%d ", arr2[i]); } printf("\n"); Student arr3[] = { {"张三","201312030120",94},{"李四","201312030118",98},{"王麻子","201312030116",96} }; sz = sizeof(arr3) / sizeof(arr3[0]); bubble(arr3, sz, sizeof(arr3[0]), struct_cmp); for (i = 0; i < sz; i++) { printf("%s %s %d\n", arr3[i].name, arr3[i].number, arr3[i].score); } system("pause"); return 0;}