简单版
//模拟计算器: void menu() { printf("*****************************************\n"); printf("************ 1.add 2.sub ***********\n"); printf("************ 3.mul 4.div ***********\n"); printf("************ 0.exit ***********\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 main() { int input = 0; int x = 0; int y = 0; int ret = 0; do { menu(); printf("请选择:>\n"); scanf("%d", &input); switch (input) { case 1: printf("请输入两个操作数\n"); scanf("%d %d",&x, &y); ret = Add(x, y); printf("ret = %d\n", ret); break; case 2: printf("请输入两个操作数\n"); scanf("%d %d", &x, &y); ret = Sub(x, y); printf("ret = %d\n", ret); break; case 3: printf("请输入两个操作数\n"); scanf("%d %d", &x, &y); ret = Mul(x, y); printf("ret = %d\n", ret); break; case 4: printf("请输入两个操作数\n"); scanf("%d %d", &x, &y); ret = Div(x, y); printf("ret = %d\n", ret); break; case 0: printf("退出计算器\n"); break; default: break; } } while (input); return 0; }
运行结果:
//模拟计算器简单改良版 void menu() { printf("*****************************************\n"); printf("************ 1.add 2.sub ***********\n"); printf("************ 3.mul 4.div ***********\n"); printf("************ 0.exit ***********\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 main() { int input = 0; int x = 0; int y = 0; int ret = 0; do { menu(); printf("请选择:>\n"); scanf("%d", &input); printf("请输入两个操作数\n"); scanf("%d %d", &x, &y); switch (input) { case 1: ret = Add(x, y); printf("ret = %d\n", ret); break; case 2: ret = Sub(x, y); printf("ret = %d\n", ret); break; case 3: ret = Mul(x, y); printf("ret = %d\n", ret); break; case 4: ret = Div(x, y); printf("ret = %d\n", ret); break; case 0: printf("退出计算器\n"); break; default: break; } } while (input); return 0; }
//函数指针数组的应用 //case语句越来越长的增强: void menu() { printf("*****************************************\n"); printf("************ 1.add 2.sub ***********\n"); printf("************ 3.mul 4.div ***********\n"); printf("************ 0.exit ***********\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 main() { int input = 0; int x = 0; int y = 0; int ret = 0; do { menu(); printf("请选择:>\n"); scanf("%d", &input); //创建一个函数指针数组 //函数指针数组 转移表 int (*pfArr[])(int, int) = { NULL,Add,Sub,Mul,Div }; // 0 1 2 3 4 if (0 == input) { printf("退出计算器\n"); } else if (input >= 1 && input <= 4) { printf("请输入两个操作数\n"); scanf("%d %d", &x, &y); ret = pfArr[input](x, y); printf("ret=%d\n", ret); } else { printf("选择错误,重新选择!\n"); } } while (input); return 0; }
最终版 回调函数 使得函数更加通用,通过参数不同输入,相应不同的函数
void menu() { printf("*****************************************\n"); printf("************ 1.add 2.sub ***********\n"); printf("************ 3.mul 4.div ***********\n"); printf("************ 0.exit ***********\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; } void calc(int (*pf)(int, int))//函数指针 { int x = 0; int y = 0; int ret = 0; printf("请输入两个操作数\n");//没有直接调用函数,将地址传给这个函数,在这个函数内部调用 scanf("%d %d", &x, &y);//这里的加减乘除就是回调函数 ret = pf(x, y);//调用函数并返回 printf("ret = %d\n", ret); } int main() { int input = 0; do { menu(); printf("请选择:>\n"); scanf("%d", &input); switch (input) { case 1: calc(*Add); break; case 2: calc(*Sub); break; case 3: calc(*Mul); break; case 4: calc(*Div); break; case 0: printf("退出计算器\n"); break; default: break; } } while (input); return 0; }