//类比
int *arr1[5]; //整形指针数组
char *arr2[5]; //字符指针数组
//函数指针数组 - 数组的每个元素是函数指针类型
#include
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(* pfArr[4])(int, int) = {Add, Sub, Mul, Div};
return 0;
}
#include
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[5])(int, int) = {NULL,Add, Sub, Mul, Div};
// 0 1 2 3 4
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
if (input >= 1 && input <= 4)
{
printf("请输入两个数:");
scanf("%d %d", &x, &y);
ret = pfArr[input](x, y);
printf("ret = %d\n", ret);
}
else if (input == 0)
{
printf("退出计算器\n");
}
else
{
printf("选择错误,重新输入\n");
}
} while (input);
return 0;
}
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;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入两个数:");
scanf("%d %d",&x,&y);
ret = Add(x, y);
printf("ret = %d\n",ret);
break;
case 2:
printf("请输入两个数:");
scanf("%d %d", &x, &y);
ret = Sub(x, y);
printf("ret = %d\n", ret);
break;
case 3:
printf("请输入两个数:");
scanf("%d %d", &x, &y);
ret = Mul(x, y);
printf("ret = %d\n", ret);
break;
case 4:
printf("请输入两个数:");
scanf("%d %d", &x, &y);
ret = Div(x, y);
printf("ret = %d\n", ret);
break;
case 0:
break;
printf("退出计算器\n");
default:
printf("输入错误,请重新输入\n");
break;
}
} while(input);
return 0;
}
int (*pf) (int, int); //函数指针
int (*pfArr[4]) (int, int); //函数指针数组
int (*(*p)[4]) (int,int) = &pfArr; //函数指针数组的地址
void test(const char* str)
{
printf("%s\n", str);
}
int main()
{
void (*pf) (const char* ) = test; //函数指针
void (*pfArr[10])(const char*); //pfArr函数指针数组
void (*(*p)[10])(const char*) = &pfArr; //指向函数指针数组的指针
return 0;
}
#include
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");
}
void Calc(int (*pf)(int, int))
{
int x = 0;
int y = 0;
int ret = 0;
printf("请输入两个数:");
scanf("%d %d", &x, &y);
ret = pf(x, y);
printf("ret=%d\n",ret);
}
int main()
{
int input = 0;
int x = 0;
int y = 0;
int ret = 0;
do
{
menu();
printf("请选择:>");
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");
break;
}
} while(input);
return 0;
}