标签:printf *** ble break can 数组 main 指针 scanf
#include<stdio.h>
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;
}
标签:printf *** ble break can 数组 main 指针 scanf
原文地址:https://blog.51cto.com/14233078/2393324