一.函数指针数组
定义:函数指针放进数组中,就叫函数指针数组,准确的说,将一个函数的地址存到⼀个数组中
那这个数组就叫函数指针数组。
int (*pi[5])(int);
解读:pi先和[]结合,因此是数组,加int (* )(int)表示其是函数指针
重在理解!!!
例一:
实现计算器(转移表)
法一:(不用函数指针数组)
实现:加,减,乘,除,取余,左移,右移
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
void Menu()
{printf("***************************\n");printf("***** 0.quit ********\n");printf("***** 1.Add ********\n");printf("***** 2.Sub ********\n");printf("***** 3.Mul ********\n");printf("***** 4.Div ********\n");printf("***** 5.Mod ********\n");printf("***** 6.Lsh ********\n");printf("***** 7.Rsh ********\n");printf("***************************\n");
}
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;
}
int Mod(int x, int y)
{return x % y;
}
int Lsh(int x, int y)
{return x << y;
}
int Rsh(int x, int y)
{return x >> y;
}
int main()
{Menu();int x = 0;int y = 0;int input = 0;printf("请输入:\n");scanf("%d", &input);int ret = 0;do{switch (input){case 0:{printf("退出,欢迎下次使用\n");break;}case 1:{printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", Add(x, y));break;}case 2:{printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", Sub(x, y));break;}case 3:{printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", Mul(x, y));break;}case 4:{printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", Div(x, y));break;}case 5:{printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", Mod(x, y));break;}case 6:{printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", Lsh(x, y));break;}case 7:{printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", Rsh(x, y));break;}}} while (input);return 0;
}
法二:(利用函数指针数组)
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
void Menu()
{printf("***************************\n");printf("***** 0.quit ********\n");printf("***** 1.Add ********\n");printf("***** 2.Sub ********\n");printf("***** 3.Mul ********\n");printf("***** 4.Div ********\n");printf("***** 5.Mod ********\n");printf("***** 6.Lsh ********\n");printf("***** 7.Rsh ********\n");printf("***************************\n");
}
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;
}
int Mod(int x, int y)
{return x % y;
}
int Lsh(int x, int y)
{return x << y;
}
int Rsh(int x, int y)
{return x >> y;
}
int main()
{Menu();int x = 0;int y = 0;int input = 0;int (*pi[8])(int x, int y) = { 0,Add ,Sub,Mul,Div,Mod,Lsh,Rsh};printf("请输入:\n");scanf("%d", &input);int ret = 0;if (0 == input){printf("退出,欢迎下次使用\n");}if (1 <= input && input <= 8){printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", (*pi[input])(x, y));}return 0;
}
结果:
二.回调函数.
把函数的指针(地址)作为参数传递给另⼀个函数,当这个指针被⽤来调⽤其所指向的函数时,被调⽤的函数就是回调函数。回调函数不是由该函数的实现⽅直接调⽤,⽽是在特定的事件或条件发⽣时由另外的⼀⽅调⽤的,⽤于对该事件或条件进⾏响应.本质上回调函数就是⼀个通过函数指针调⽤的函数。
对例题一:
我们通过回调函数来实现它
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
void Menu()
{printf("***************************\n");printf("***** 0.quit ********\n");printf("***** 1.Add ********\n");printf("***** 2.Sub ********\n");printf("***** 3.Mul ********\n");printf("***** 4.Div ********\n");printf("***** 5.Mod ********\n");printf("***** 6.Lsh ********\n");printf("***** 7.Rsh ********\n");printf("***************************\n");
}
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;
}
int Mod(int x, int y)
{return x % y;
}
int Lsh(int x, int y)
{return x << y;
}
int Rsh(int x, int y)
{return x >> y;
}
void cala(int(*pi)(int , int ))
{int x = 0;int y = 0;printf("请输入:\n");scanf("%d %d", &x, &y);printf("%d\n", pi(x, y) );
}
int main()
{Menu();int x = 0;int y = 0;int input = 0;printf("请输入:\n");scanf("%d", &input);do{switch (input){case 0:{printf("退出,欢迎下次使用\n");break;}case 1:{cala(Add);break;}case 2:{cala(Sub);break;}case 3:{cala(Mul);break;}case 4:{cala(Div);break;}case 5:{cala(Mod);break;}case 6:{cala(Lsh);break;}case 7:{cala(Rsh);break;}}} while (input);return 0;
}
三.sqort函数
1.头文件为#include <stdlib.h>
2.里面有四个形参,表里介绍的很详细了
3.作用:其可以排序各种数据类型。
我们先来实现其排序整型:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compare(const void* a, const void* b)
{return (*(int*)a - *(int*)b);
}
int main()
{int arr[] = { 1,3,5,7,9,0,8,6,4,2 };qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]),compare);for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++){printf("%d ", *(arr + i));}return 0;
}
结果:
排序结构体:
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct stduent
{char name[20];int age;double score;};
int compare1(const void* a, const void* b)
{return strcmp(((struct stduent*)a)->name, ((struct stduent*)b)->name);
}
int compare2(const void* a, const void* b)
{return (*(int*)a-*(int*)b);
}
int compare3(const void* a, const void* b)
{return (*(int*)a - *(int*)b);
}
void test1()
{struct stduent arr1[] = {{"zhangsan",18,90.5},{"lisi",17,91.7},{"wangwu",20,100.0}};qsort(arr1, sizeof(arr1) / sizeof(arr1[0]), sizeof(arr1[0]), compare1);//比名字for (int i = 0; i < 3; i++){printf("%-10s %-5d %8f\n", arr1[i].name, arr1[i].age, arr1[i].score);}printf("结束\n");
}
void test2()
{struct stduent arr1[] = { {"zhangsan",18,90.5},{"lisi",17,91.7},{"wangwu",20,100.0} };qsort(arr1, sizeof(arr1) / sizeof(arr1[0]), sizeof(arr1[0]), compare2);//比年龄for (int i = 0; i < 3; i++){printf("%-10s %-5d %8f\n", arr1[i].name, arr1[i].age, arr1[i].score);}printf("结束\n");
}
void test3()
{struct stduent arr1[] = { {"zhangsan",18,90.5},{"lisi",17,91.7},{"wangwu",20,100.0} };qsort(arr1, sizeof(arr1) / sizeof(arr1[0]), sizeof(arr1[0]), compare1);//比成绩for (int i = 0; i < 3; i++){printf("%-10s %-5d %8f\n", arr1[i].name, arr1[i].age, arr1[i].score);}printf("结束\n");}
int main()
{test1();test2();test3();return 0;
}
结果:
实现sqort函数:
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int compare(const void* a, const void* b){return (*((int*)a) - *((int*)b));}void swap(const void* x, const void* y, int size){for (int i = 0; i < size; i++){char temp = *((char*)x+i);*((char*)x + i) = *((char*)y + i);*((char*)y + i) = temp;}}void Bubble(void* arr, int count, int b, int compare(void*, void*)){for (int i = 0; i < count - 1; i++){for (int j = 0; j < count - 1 - i; j++){if (compare((char*)arr + j * b, (char*)arr + (j + 1) * b) > 0){swap((char*)arr + j * b, (char*)arr + (j + 1) * b, b);}}}}int main(){int arr[10] = { 0 };int sz = sizeof(arr) / sizeof(arr[0]);for (int i = 0; i < sz; i++){scanf("%d",arr+i);}Bubble(arr, sz, sizeof(arr[0]), compare);//输出for (int i = 0; i < sz; i++){printf("%d ", *(arr + i));}return 0;}
最后,祝福各位学习天天进步!!!