1.其实代码段大致还是跟上篇博文内容差不多类似！下面笔者对于相似的部分进行简单的引用，在此，笔者就不再进行过多的讲述（当然，建议有想法的读者去看一下笔者的上篇博客）！

//简化一下代码 #include <stdio.h> 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 menu() {
printf("***************************\n"); printf("***** 1.add 2. sub ****\n");
printf("***** 3.mul 4. div ****\n"); printf("***** 0.exit ****\n");
printf("***************************\n"); }

int main() { int input = 0; int x = 0; int y = 0; int ret = 0; //函数指针数组 - 转移表
int (*pfArr[])(int, int) = { 0, Add, Sub, Mul, Div }; do { menu();
printf("请选择:>"); scanf_s("%d", &input); if (input == 0) { printf("退出计算器\n");
break; } if (input >= 1 && input <= 4) { printf("请输入2个操作数:>"); scanf_s("%d %d",
&x, &y); ret = pfArr[input](x, y); printf("%d\n", ret); } else {
printf("选择错误\n"); } } while (input); return 0; }

4，对于判断的结果，将决定选择那个进行打印！

input又当作了数组的下标！然后调用： int (*pfArr[])(int, int) = { 0, Add, Sub, Mul, Div };

if (input >= 1 && input <= 4) { printf("请输入2个操作数:>"); scanf_s("%d %d", &x,
&y); ret = pfArr[input](x, y); printf("%d\n", ret); } else { printf("选择错误\n"); }

//简化一下代码 #include <stdio.h> 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 menu() {
printf("***************************\n"); printf("***** 1.add 2. sub ****\n");
printf("***** 3.mul 4. div ****\n"); printf("***** 0.exit ****\n");
printf("***************************\n"); } int main() { int input = 0; int x =
0; int y = 0; int ret = 0; //函数指针数组 - 转移表 int (*pfArr[])(int, int) = { 0, Add,
Sub, Mul, Div }; do { menu(); printf("请选择:>"); scanf_s("%d", &input); if (input
== 0) { printf("退出计算器\n"); break; } if (input >= 1 && input <= 4) {
printf("请输入2个操作数:>"); scanf_s("%d %d", &x, &y); ret = pfArr[input](x, y);
printf("%d\n", ret); } else { printf("选择错误\n"); } } while (input); return 0; }
#include <stdio.h> void menu() { printf("***************************\n");
printf("***** 1.add 2. sub ****\n"); printf("***** 3.mul 4. div ****\n");
printf("***** 0.exit ****\n"); printf("***************************\n"); } int
main() { menu(); return 0; }

GitHub

Gitee