void Manu()
{
printf("**************************************\n");
printf("********* 1.Add 2.Sub ******\n");
printf("********* 3.Nul 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 Nul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
void Calc(int (*pf)(int, int))
{
int x, y = 0;
printf("请输入两组操作数:");
scanf("%d%d", &x, &y);
printf("(x, y) = %d\n", pf(x, y));
}
int main()
{
int input = 0;
do
{
Manu();
printf("请选择:>");
scanf("%d", &input);
//int (*pfarr[])(int, int) = { 0,Add,Sub,Nul,Div };//转移表 函数指针数组
//if (input > 0 && input < 5)
//{
// printf("请输入两组操作数: ");
// scanf("%d%d", &x, &y);
// printf("%d\n", pfarr[input](x, y));
//}
//else if (input == 0)
// printf("退出\n");
//else
// printf("选择错误!\n");
switch (input)
{
case 1:
Calc(Add);//函数回调 解决代码冗余(重复)
break;
case 2:
Calc(Sub);
break;
case 3:
Calc(Nul);
break;
case 4:
Calc(Div);
break;
case 0:
printf("退出\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}