#include
double add(double x, double y)//加
{
return x + y;
}
double sub(double x, double y)//减
{
return x - y;
}
double mul(double x, double y)//乘
{
return x*y;
}
double div(double x, double y)//除
{
if (y != 0)
{
return x/y;
}
else
{
printf("输入错误");
return -1;
}
}
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 = 1;
double a = 0;
double b = 0;
double ret;
double(*p[5])(double a, double b) = { 0, add, sub, mul, div };
while (input)
{
menu();
printf("请输入需要的操作:\n");
scanf("%d",&input);
if (input > 0 && input <= 4)
{
printf("请输入操作数:\n");
scanf("%lf%lf",&a,&b);
ret = (*p[input])(a, b);
printf("%.2lf\n", ret);
}
else if (0==input)
{
break;
}
else
{
printf("输入错误");
}
}
return 0;
}
网站标题:加减乘除法运算(函数数组指针法)
URL网址:
http://cdxtjz.cn/article/jsheeo.html