1.设置回调函数类型,定义模板结构体
2.设置执行函数单元
3.设置预执行函数及对应参数
4.main函数,执行测试用例
5.运行结果
6.实例源码
#include <stdio.h>
#include <stdint.h>
typedef int (*Calc_Math)(int param1,int param2);
typedef struct {
char* lable;
uint8_t flag;
uint8_t offset;
Calc_Math func;
} FuncInfo;
int add_calc(int data1,int data2)
{
return data1+data2;
}
int sub_calc(int data1,int data2)
{
return data1-data2;
}
int mul_calc(int data1,int data2)
{
return data1 * data2;
}
int div_calc(int data1,int data2)
{
return data1 / data2;
}
FuncInfo test_func[4] = {
{"add", 0, 3, add_calc},
{"sub", 1, 2, sub_calc},
{"mul", 2, 1, mul_calc},
{"div", 3, 0, div_calc},
};
int main()
{
int result[5]={0x0};
for(int i=0;i<4;i++)
{
result[test_func[i].flag] = test_func[i].func(12,4);
printf("[do %s opt] [flag:%d] [offset:%d]\n",test_func[i].lable,test_func[i].flag,test_func[i].offset);
}
for(int i = 0;i<5;i++)
{
printf("result[%d]=%d ",i,result[i]);
}
printf("\n");
return 0;
}