if...else...面条代码,以及三种实现方式
由于本人从事业务层代码编写,每次都是if...else... 面条代码较多,本次随笔主要是记录三种写代码方式,满足a条件,调用执行a的函数,满足b条件,调用执行b的函数,满足c条件,调用执行c的函数
三种代码实现方式都是实现同一功能,代码可以直接运行;
1.采用挂钩子形式,开放注册接口,系统运行时每个模块依次判断条件并且执行该模块的操作,
有兴趣的可以将注册的函数挂载在全局变量下,这样便于各个共享模块注册,这里只是提供大概模型;
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXECUTE_FUNC_NUM 10
typedef int(*JUDGE_FUNC)(char para1, int para2, int para3, int para4);
typedef int(*EXECUTE_FUNC)(int para1, int para2, int para3, int para4);
int mount_judge_func(JUDGE_FUNC *judge_func, JUDGE_FUNC func1, JUDGE_FUNC func2, JUDGE_FUNC func3)
{
if((NULL == judge_func)
|| (NULL == func1)
|| (NULL == func2)
|| (NULL == func3))
{
return -1;
}
judge_func[0] = func1;
judge_func[1] = func2;
judge_func[2] = func3;
return 0;
}
int mount_execute_func(EXECUTE_FUNC *execute_func, EXECUTE_FUNC func1, EXECUTE_FUNC func2, EXECUTE_FUNC func3)
{
if((NULL == execute_func)
|| (NULL == func1)
|| (NULL == func2)
|| (NULL == func3))
{
return -1;
}
execute_func[0] = func1;
execute_func[1] = func2;
execute_func[2] = func3;
return 0;
}
int judge_func_a(char para1, int para2, int para3, int para4)
{
if(para1 == 'a')
{
return 0;
}
return -1;
}
int judge_func_b(char para1, int para2, int para3, int para4)
{
if(para1 == 'b')
{
return 0;
}
return -1;
}
int judge_func_c(char para1, int para2, int para3, int para4)
{
if(para1 == 'c')
{
return 0;
}
return -1;
}
int execute_func_a(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_a\n");
return 0;
}
int execute_func_b(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_b\n");
return 0;
}
int execute_func_c(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_c\n");
return 0;
}
int main()
{
int i = 0;
int ret = 0;
char para1 = 0;
int para2 = 0;
int para3 = 0;
int para4 = 0;
int para5 = 0;
int para6 = 0;
int para7 = 0;
int para8 = 0;
EXECUTE_FUNC execute_func[MAX_EXECUTE_FUNC_NUM + 1] = {NULL};
JUDGE_FUNC judge_func[MAX_EXECUTE_FUNC_NUM + 1] = {NULL};
scanf("%c %d %d %d", ¶1, ¶2, ¶3, ¶4);
//printf("%c, %d, %d, %d", para1, para2, para3, para4);
ret = mount_judge_func(judge_func, judge_func_a, judge_func_b, judge_func_c);
if(0 != ret)
{
printf("mount_judge_func register failed\n");
return -1;
}
ret = mount_execute_func(execute_func, execute_func_a, execute_func_b, execute_func_c);
if(0 != ret)
{
printf("mount_execute_func register failed\n");
return -1;
}
for(i = 0; i < MAX_EXECUTE_FUNC_NUM; i++)
{
if((NULL == judge_func[i])
|| (NULL == execute_func[i]))
{
continue;
}
if(0 == judge_func[i](para1, para2, para3, para4))
{
return execute_func[i](para5, para6, para7, para8);
}
}
printf("no func execute!\n");
return 0;
}
2.采用do...while(0), 和switch进行组合使用,也是一种使用技巧;
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXECUTE_FUNC_NUM 10
#define EXECUTE_FUNC_A 1
#define EXECUTE_FUNC_B 2
#define EXECUTE_FUNC_C 3
typedef int(*JUDGE_FUNC)(char para1, int para2, int para3, int para4);
typedef int(*EXECUTE_FUNC)(int para1, int para2, int para3, int para4);
int judge_func_a(char para1, int para2, int para3, int para4)
{
if(para1 == 'a')
{
return 0;
}
return -1;
}
int judge_func_b(char para1, int para2, int para3, int para4)
{
if(para1 == 'b')
{
return 0;
}
return -1;
}
int judge_func_c(char para1, int para2, int para3, int para4)
{
if(para1 == 'c')
{
return 0;
}
return -1;
}
int execute_func_a(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_a\n");
return 0;
}
int execute_func_b(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_b\n");
return 0;
}
int execute_func_c(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_c\n");
return 0;
}
int main()
{
int judge_ret = 0;
int execute_ret = 0;
char para1 = 0;
int para2 = 0;
int para3 = 0;
int para4 = 0;
int para5 = 0;
int para6 = 0;
int para7 = 0;
int para8 = 0;
scanf("%c %d %d %d", ¶1, ¶2, ¶3, ¶4);
do{
judge_ret = judge_func_a(para1, para2, para3, para4);
if(0 == judge_ret)
{
execute_ret = EXECUTE_FUNC_A;
break;
}
judge_ret = judge_func_b(para1, para2, para3, para4);
if(0 == judge_ret)
{
execute_ret = EXECUTE_FUNC_B;
break;
}
judge_ret = judge_func_c(para1, para2, para3, para4);
if(0 == judge_ret)
{
execute_ret = EXECUTE_FUNC_C;
break;
}
}while(0);
switch(execute_ret)
{
case EXECUTE_FUNC_A:
execute_func_a(para5, para6, para7, para8);
break;
case EXECUTE_FUNC_B:
execute_func_b(para5, para6, para7, para8);
break;
case EXECUTE_FUNC_C:
execute_func_c(para5, para6, para7, para8);
break;
default:
printf("no func execute!\n");
break;
}
return 0;
}
3.最常见的if...else...编写代码,随着if...else...嵌套变多,可理解性变差
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int judge_func_a(char para1, int para2, int para3, int para4)
{
if(para1 == 'a')
{
return 0;
}
return -1;
}
int judge_func_b(char para1, int para2, int para3, int para4)
{
if(para1 == 'b')
{
return 0;
}
return -1;
}
int judge_func_c(char para1, int para2, int para3, int para4)
{
if(para1 == 'c')
{
return 0;
}
return -1;
}
int execute_func_a(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_a\n");
return 0;
}
int execute_func_b(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_b\n");
return 0;
}
int execute_func_c(int para1, int para2, int para3, int para4)
{
printf("execute execute_func_c\n");
return 0;
}
int main()
{
int execute_ret = 0;
char para1 = 0;
int para2 = 0;
int para3 = 0;
int para4 = 0;
int para5 = 0;
int para6 = 0;
int para7 = 0;
int para8 = 0;
scanf("%c %d %d %d", ¶1, ¶2, ¶3, ¶4);
if(0 == judge_func_a(para1, para2, para3, para4))
{
execute_ret = execute_func_a(para5, para6, para7, para8);
}
else if(0 == judge_func_b(para1, para2, para3, para4))
{
execute_ret = execute_func_b(para5, para6, para7, para8);
}
else if(0 == judge_func_c(para1, para2, para3, para4))
{
execute_ret = execute_func_c(para5, para6, para7, para8);
}
else
{
printf("no func execute!\n");
}
return execute_ret;
}