- 🌈🌈🌈这里是say-fall分享,感兴趣欢迎三连与评论区留言
- 🔥🔥🔥专栏:《C语言入门知识点》、《C语言底层》
- 💪💪💪格言:今天多敲一行代码,明天少吃一份苦头
前言:
最近的五篇文章都在了解指针的类型,各种变量的指针以及什么时候用指针好一点,本节我们就来了解一下回调函数以及用冒泡排序法来模拟一下库函数里面的qsort函数,以便我们更深刻地认识指针,感兴趣的小伙伴快来围观~
正文:
1. 回调函数
1.1 回调函数是什么?
- 回调函数(Callback Function) 是一种特殊的函数:它由开发者定义,但不是由开发者直接调用,而是作为参数(地址)传递给另一个函数,由这个函数在特定时机(满足特定条件时)自动调用。
1.2 回调函数举例
还记得我们模拟计算器的代码吗?
int add(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int mul(int x, int y)
{
return x * y;
}
int div(int x, int y)
{
return x / y;
}
void mune()
{
printf("*************************\n");
printf("******** 1.add *******\n");
printf("******** 2.sub *******\n");
printf("******** 3.mul *******\n");
printf("******** 4.div *******\n");
printf("******** 0.exit *******\n");
printf("*************************\n");
}
int main()
{
int input = 0;
do
{
int x, y, ret = 0;
mune();
printf("please enter your choice:\n");
scanf("%d", &input);
switch (input)
{
case 1:
{
printf("please enter two oprands,separated by a apace:\n");
scanf("%d %d", &x, &y);
ret = add(x,y);
printf("%d\n", ret);
break;
}
case 2:
{
printf("please enter two oprands,separated by a apace:\n");
scanf("%d %d", &x, &y);
ret = add(x, y);
printf("%d\n", ret);
break;
}
case 3:
{
printf("please enter two oprands,separated by a apace:\n");
scanf("%d %d", &x, &y);
ret = add(x, y);
printf("%d\n", ret);
break;
}
case 4:
{
printf("please enter two oprands,separated by a apace:\n");
scanf("%d %d", &x, &y);
ret = add(x, y);
printf("%d\n", ret);
break;
}
case 0:
{
printf("exit the program!\n");
break;
}
defult:
{
printf("enter error!please re-enter!\n");
break;
}
}
}
while (input);
return 0;
}
这段代码看上去很多重复的地方,在上一节指针知识我们已经把它用转移表化简过了,既然本节是介绍回调函数的知识的文章,我们试试用回调函数来化简他:
int add(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int mul(int x, int y)
{
return x * y;
}
int div(int x, int y)
{
return x / y;
}
void mune()
{
printf("*************************\n");
printf("******** 1.add *******\n");
printf("******** 2.sub *******\n");
printf("******** 3.mul *******\n");
printf("******** 4.div *******\n");
printf("******** 0.exit *******\n");
printf("*************************\n");
}
上面这些内容不变,对下面进行改善。我们发现,重复的部分其实只有这一部分:
printf("please enter two oprands,separated by a apace:\n");
scanf("%d %d", &x, &y);
ret = add(x,y);
printf("%d\n", ret);
break;
我们有什么办法能让他们只出现一次呢?
其实我们可以写一个函数
void caculate(int (*pcac)(int x, int y))//pass yhe pointer of each function in.
//将每个函数的指针传进来
{
//重复的部分
int ret = 0;
int x, y = 0;
printf("please enter two oprands,separated by a apace:\n");
scanf("%d %d", &x, &y);
ret = pcac(x, y);//ues a pointer instead of a function name
printf("%d\n", ret);
}
接着把剩下的部分放回来,case里面的内容用caculate函数代替
完整代码:
//callbak function simplify counter
//这里我们用回调函数化简
int add(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int mul(int x, int y)
{
return x * y;
}
int div(int x, int y)
{
return x / y;
}
void mune()
{
printf("*************************\n");
printf("******** 1.add *******\n");
printf("******** 2.sub *******\n");
printf("******** 3.mul *******\n");
printf("******** 4.div *******\n");
printf("******** 0.exit *******\n");
printf("*************************\n");
}
void caculate(int (*pcac)(int x, int y))//pass yhe pointer of each function in.
//将每个函数的指针传进来
{
//重复的部分
int ret = 0;
int x, y = 0;
printf("please enter two oprands,separated by a apace:\n");
scanf("%d %d", &x, &y);
ret = pcac(x, y);//ues a pointer instead of a function name
printf("%d\n", ret);
}
//接着把剩下的部分放回来,case里面的内容用caculate函数代替
int main()
{
int input = 0;
do
{
int x, y, ret = 0;
mune();
printf("please enter your choice:\n");
scanf("%d", &input);
switch (input)
{
case 1:
{
caculate(add);
break;
}
case 2:
{
caculate(sub);
break;
}
case 3:
{
caculate(mul);
break;
}
case 4:
{
caculate(div);
break;
}
case 0:
{
printf("exit the program!\n");
break;
}
defult:
{
printf("enter error!please re-enter!\n");
break;
}
}
} while (input);
return 0;
}
这样我们就用回调函数实现了模拟简单计算器的化简
这里的caculate函数里面的四种运算函数就是回调函数,根据使用者所传入的指针决定调用哪个函数
2.qsort函数
2.1 qsort函数的规范和使用
头文件
#include <stdlib>
标准输入
void qsort (void* base, size_t num, size_t size,
int (*compar)(const void*,const void*));
解释:
//void* base是Pointer to the first object of the array to be sorted, converted to a void*.
//指向待排列数组(base)第一个元素的指针,已经转化为void*类型
//size_t num是Number of elements in the array pointed to by base.
//base所指向的数组中的元素数量
//size_t size是Size in bytes of each element in the array.
//base每个元素的大小
//int (*compar)(const void*,const void*)是Pointer to a function that compares two elements.
//用来反复比较两个元素的函数的指针
我们来看一个qsort的实例:
#include <stdio.h>
#include <stdlib.h>
int comp(const void* p1,const void* p2)
{
return *((int*)p1) - *((int*)p2);
}
int test1()
{
int i = 0;
int base[] = { 0,2,5,8,4,1,7,9,3,6 };
int sz = sizeof(base) / sizeof(base[0]);
for (i = 0;i < sz;i++)
{
printf("%d ", base[i]);
}
printf("\n");
qsort(base,sz,sizeof(base[0]),comp);
for (i = 0;i < sz;i++)
{
printf("%d ",base[i]);
}
printf("\n");
return 0;
}
int main()
{
test1();
return 0;
}
运行结果:
0 2 5 8 4 1 7 9 3 6
0 1 2 3 4 5 6 7 8 9
这是对整型的排序
看看字符类型:
#include <stdio.h>
#include <stdlib.h>
int comp_char(const void* p1, const void* p2)
{
return *(char*)p1-*(char*)p2;
}
int test1()
{
int i = 0;
char base[] = "dsedfag";
int sz = sizeof(base) / sizeof(base[0]);
for (i = 0;i < sz;i++)
{
printf("%c ", base[i]);
}
printf("\n");
qsort(base, sz, sizeof(base[0]), comp_char);
for (i = 0;i < sz;i++)
{
if (base[i] != '\0')
{
printf("%c ", base[i]);
}
}
printf("\n");
return 0;
}
int main()
{
test1();
return 0;
}
2.2 用冒泡排序对qsort函数模拟
看懂规范和使用以后,我们对qsort函数有一个大概的了解了
假设这部分是我们用冒泡排序对qsort函数的模拟
首先框架是不变的,都是以冒泡的方式排序
void bubble_sort(void* arr, size_t sz, size_t size,int comp(const void* p1, const void* p2))
//size是单个元素的字节大小
{
int i, j = 0;
//确定趟数
for (i = 0; i < sz - 1;i++)
{
//确定每次每趟交换几次
for (j = 0;j < sz - i - 1;j++)
{
if (comp_int((char*)arr+j*size, (char*)arr+(j+1)*size) > 0)
//判断交换条件
{
swap((char*)arr + j * size, (char*)arr + (j + 1) * size, size);
//进行交换操作
}
}
}
}
这里判断的部分写一段函数,这段一般交给用户自己写,方便判断什么类型
int comp_int(const void* p1, const void* p2)
{
return *(int*)p1 - *(int*)p2;
}
关于交换的部分,我们以字节为单位交换
那就肯定要确定参数,这里我们传入指针变量并且用*void接收
然鹅不同的变量实际上的字节大小不同,所以我们传入变量字节大小
void swap(void* p1,void* p2,int size)
{
int i = 0;
for (i = 0;i < size;i++)
{
char temp = *((char*)p1+i);
*((char*)p1+i) = *((char*)p2+i);
*((char*)p2+i) = temp;
}
}
这里我们用一个一个的字节交换,强制转换为
char*的原因是char*大小是一个字节
接下来是主函数部分,我们用int类型测试一遍
int main()
{
int base[] = { 0,2,5,8,6,3,9,1,4,7 };
int sz = sizeof(base) / sizeof(base[0]);
int i = 0;
for (i = 0;i < sz;i++)
{
printf("%d ", base[i]);
}
printf("\n");
bubble_sort(base, sz, sizeof(base[0]), comp_int);
for (i = 0;i < sz;i++)
{
printf("%d ", base[i]);
}
printf("\n");
return 0;
}
运行结果:
0 2 5 8 6 3 9 1 4 7
0 1 2 3 4 5 6 7 8 9
假设我们要排序字符串:
#include <stdio.h>
//int类型比较
int comp_int(const void* p1, const void* p2)
{
return *(int*)p1 - *(int*)p2;
}
//char类型比较
int comp_char(const void* p1, const void* p2)
{
return *(char*)p1-*(char*)p2;
}
//交换函数
void swap(void* p1,void* p2,int size)
{
int i = 0;
for (i = 0;i < size;i++)
{
char temp = *((char*)p1+i);
*((char*)p1+i) = *((char*)p2+i);
*((char*)p2+i) = temp;
}
}
//bubble_sort函数
void bubble_sort(void* arr, size_t sz, size_t size,int comp(const void* p1, const void* p2))
//size是单个元素的字节大小
{
int i, j = 0;
//确定趟数
for (i = 0; i < sz - 1;i++)
{
//确定每次每趟交换几次
for (j = 0;j < sz - i - 1;j++)
{
if (comp_char((char*)arr+j*size, (char*)arr+(j+1)*size) > 0)
{
swap((char*)arr + j * size, (char*)arr + (j + 1) * size, size);
}
}
}
}
void test_int(int(*comp_int)(const void* p1,const void* p2))
{
int base[] = { 0,2,5,8,6,3,9,1,4,7 };
int sz = sizeof(base) / sizeof(base[0]);
int i = 0;
for (i = 0;i < sz;i++)
{
printf("%d ", base[i]);
}
printf("\n");
bubble_sort(base, sz, sizeof(base[0]), comp_int);
for (i = 0;i < sz;i++)
{
printf("%d ", base[i]);
}
printf("\n");
}
void test_char(int(*comp_char)(const void* p1, const void* p2))
{
char base[] = "abhsdgd";
int sz = sizeof(base) / sizeof(base[0]);
int i = 0;
for (i = 0;i < sz;i++)
{
printf("%c ", base[i]);
}
printf("\n");
bubble_sort(base, sz, sizeof(base[0]), comp_char);
for (i = 0;i < sz;i++)
{
if (base[i] != '\0')
{
printf("%c ", base[i]);
}
}
printf("\n");
}
int main()
{
//test_int(comp_int);
test_char(comp_char);
return 0;
}
通过更改变量的类型就可以对不同变量进行排序
-
最后一段代码其实还能用回调函数简化,谁来试试~
-
本节完…
回调函数与qsort模拟详解
66万+





