目录
1. 回调函数
2. qsort使用举例
1)排序整型数据
2)排序结构数据
3. qsort函数的模拟实现(冒泡)
1. 回调函数
回调函数就是一个通过函数指针调用的函数
函数的指针(地址)作为参数传递给另一个函数后,当这个指针被用来调用其所指向的函数时,被调用的函数就是回调函数,并且不是由该函数直接调用,而是在特定的事件或条件发生时间接调用
通过一个例子(转移表)来呈现回调函数的功能:
//使用回调函数改造前
#include <stdio.h>int add(int a, int b)
{return a + b;
}
int sub(int a, int b)
{return a - b;
}
int mul(int a, int b)
{return a * b;
}
int div(int a, int b)
{return a / b;
}int main()
{int x, y;int input = 1;int ret = 0;do{printf("*************************\n");printf(" 1:add 2:sub \n");printf(" 3:mul 4:div \n");printf(" 0:exit \n");printf("*************************\n");printf("请选择:");scanf("%d", &input);switch (input){case 1:printf("输入操作数:");scanf("%d %d", &x, &y)ret = add(x, y);printf("ret = %d\n", rbreak;case 2:printf("输入操作数:");scanf("%d %d", &x, &y)ret = sub(x, y);printf("ret = %d\n", rbreak;case 3:printf("输入操作数:");scanf("%d %d", &x, &y)ret = mul(x, y);printf("ret = %d\n", rbreak;case 4:printf("输入操作数:");scanf("%d %d", &x, &y)ret = div(x, y);printf("ret = %d\n", rbreak;case 0:printf("退出程序\n");break;default:printf("选择错误\n");break;}} while (input);return 0;
}
//使用回到函数改造后#include <stdio.h>int add(int a, int b)
{return a + b;
}
int sub(int a, int b)
{return a - b;
}
int mul(int a, int b)
{return a * b;
}
int div(int a, int b)
{return a / b;
}void calc(int(*pf)(int, int))
{int ret = 0;int x, y;printf("输入操作数:");scanf("%d %d", &x, &y);ret = pf(x, y);printf("ret = %d\n", ret);
}int main()
{int input = 1;do{printf("*************************\n");printf(" 1:add 2:sub \n");printf(" 3:mul 4:div \n");printf(" 0:exit \n");printf("*************************\n");printf("请选择:");scanf("%d", &input);switch (input){case 1:calc(add);break;case 2:calc(sub);break;case 3:calc(mul);break;case 4:calc(div);break;case 0:printf("退出程序\n");break;default:printf("选择错误\n");break;}} while (input);return 0;
}
2. qsort使用举例
1)排序整型数据
#include <stdio.h>
//qosrt函数的使用者得实现一个比较函数int int_cmp(const void * p1, const void * p2)
{return (*( int *)p1 - *(int *) p2);
}int main()
{int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };int i = 0;qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof (int), int_cmp);for (i = 0; i< sizeof(arr) / sizeof(arr[0]); i++){printf( "%d ", arr[i]);}printf("\n");return 0;
}
2)排序结构数据
struct Stu //学生
{char name[20];//名字int age;//年龄
};//假设按照年龄来比较
int cmp_stu_by_age(const void* e1, const void* e2)
{return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}//strcmp - 是库函数,是专门用来比较两个字符串的大小的
//假设按照名字来比较
int cmp_stu_by_name(const void* e1, const void* e2)
{return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}
//按照年龄来排序
void test2()
{struct Stu s[] = { {"zhangsan", 20}, {"lisi", 30}, {"wangwu", 15} };int sz = sizeof(s) / sizeof(s[0]);qsort(s, sz, sizeof(s[0]), cmp_stu_by_age);
}//按照名字来排序
void test3()
{struct Stu s[] = { {"zhangsan", 20}, {"lisi", 30}, {"wangwu", 15} };int sz = sizeof(s) / sizeof(s[0]);qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
}
//void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
int main()
{test2();test3();return 0;
}
3. qsort函数的模拟实现(冒泡)
#include <stdio.h>int int_cmp(const void * p1, const void * p2)
{return (*( int *)p1 - *(int *) p2);//强制类型转换 int* 一次访问4个字节
}void _swap(void *p1, void * p2, int size)
{int i = 0;for (i = 0; i< size; i++){char tmp = *((char *)p1 + i);//每次交换1字节 交换size字节*(( char *)p1 + i) = *((char *) p2 + i);*(( char *)p2 + i) = tmp;}
}void bubble(void *base, int count , int size, int(*cmp )(void *, void *))
{int i = 0;int j = 0;for (i = 0; i< count - 1; i++){for (j = 0; j<count-i-1; j++) //(char *) base + j*size 访问base往后 j*size 字节{if (cmp ((char *) base + j*size , (char *)base + (j + 1)*size) > 0){_swap(( char *)base + j*size, (char *)base + (j + 1)*size, size);}}}
}int main()
{int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };int i = 0;bubble(arr, sizeof(arr) / sizeof(arr[0]), sizeof (int), int_cmp);for (i = 0; i< sizeof(arr) / sizeof(arr[0]); i++){printf( "%d ", arr[i]);}printf("\n");return 0;
}
谢谢观看