intAdd(int x, int y) { return x + y; } intSub(int x, int y) { return x - y; } intMul(int x, int y) { return x * y; } intDiv(int x, int y) { return x / y; }
intmain() { int (*pfArr[4])(int, int) = {Add, Sub, Mul, Div};//函数指针数组 int i = 0; for (i = 0; i < 4; i++) { //int ret = (*pfArr[i])(8, 4); int ret = pfArr[i](8, 4);
intAdd(int x, int y) { return x + y; } intSub(int x, int y) { return x - y; } intMul(int x, int y) { return x * y; } intDiv(int x, int y) { return x / y; }
voidmenu() { printf("**********************************\n"); printf("***** 1. add 2. sub *****\n"); printf("***** 3. mul 4. div *****\n"); printf("***** 0. exit *****\n"); printf("**********************************\n"); }
intmain() { int input = 0; int x = 0; int y = 0; int ret = 0; do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case1: printf("输入2个操作数:>"); scanf("%d %d", &x, &y); ret = Add(x, y); printf("ret = %d\n", ret); break; case2: printf("输入2个操作数:>"); scanf("%d %d", &x, &y); ret = Sub(x, y); printf("ret = %d\n", ret); break; case3: printf("输入2个操作数:>"); scanf("%d %d", &x, &y); ret = Mul(x, y); printf("ret = %d\n", ret); break; case4: printf("输入2个操作数:>"); scanf("%d %d", &x, &y); ret = Div(x, y); printf("ret = %d\n", ret); break; case0: printf("退出计算器\n"); break; default: printf("选择错误\n"); break; } } while (input);