类型区分:
指针:int *
数组指针:int (* )[ ]
指针数组:int * [ ]
函数指针:int (*)(参数类型)
函数指针数组:int (* [ ])(参数类型)
#define _CRT_SECURE_NO_WARNINGS 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, y = 0;
int s = 0;
int (*arr[5])(int, int) = { 0,Add,Sub,Mul,Div };
//一个数组arr包含4个函数
do
{
menu();
printf("请选择:");
scanf("%d", &input);
if (input > 0 && input < 5)
{
printf("请输入两个数:");
scanf("%d%d", &x, &y);
int s = arr[input](x, y);
printf("%d\n", s);
}
else if (input == 0)
{
printf("退出游戏\n");
}
else
{
printf("输入错误\n");
}
} while (input);
return 0;
}
若需添加功能,只需增加函数即可