// // Created by pipi on 2021/9/7. // #include <stdio.h> void menu() { printf("*** 1.Add 2.Sub 3.Mul 4.Div 0.exit ***\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; printf("请输入2个整数\n"); scanf("%d%d",&x,&y); printf("%d\n",pf(x,y)); } /* int main() { int input = 0; int x = 0; int y = 0; int (*parr[])(int, int) ={0, Add, Sub, Mul, Div}; int sz = sizeof (parr)/sizeof (parr[0]); // printf("%d\n",sz); do { menu(); printf("请选择\n"); scanf("%d", &input); if (input == 0) { printf("退出\n"); break; } else if (input < 0 || input > sz-1) { printf("输入错误,请重新选择\n"); } else { printf("请输入两个整数\n"); scanf("%d%d", &x, &y); printf("%d\n", parr[input](x, y)); } } while (input); return 0; } */ /* int main() { int input = 0; int x = 0; int y = 0; do { menu(); printf("请选择\n"); scanf("%d", &input); if (!input) { printf("退出\n"); break; } printf("请输入2个整数的数字\n"); scanf("%d%d",&x,&y); switch (input) { case 1: printf("%d\n", Add(x,y)); break; case 2: printf("%d\n", Sub(x,y)); break; case 3: printf("%d\n", Mul(x,y)); break; case 4: printf("%d\n", Div(x,y)); break; case 0: printf("退出\n"); break; default: printf("选择错误,请重新选择\n"); } } while (input); return 0; }*/ /* 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: printf("选择错误,请重新选择\n"); } } while (input); return 0; }*/
函数指针数组:初阶
最新推荐文章于 2024-09-09 11:18:04 发布