#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int cv(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int ret = 0;
int input = 1;
int(*p[5])(int x, int y) = { 0, add, sub, mul, cv };
while (input)
{
printf("请选择:\n");
printf("1. add 2.sub\n");
printf("3. mul 4.div\n");
scanf("%d", &input);
if (input > 0 && input < 5)
{
printf("请输入操作数:");
scanf("%d %d", &x, &y);
ret = (*p[input])(x, y);
}
else
{
printf("重新选择:");
}
printf("结果是:%d\n", ret);
}
system("pause");
return 0;
}
简易计算器(利用函数指针数组)
最新推荐文章于 2022-03-12 16:13:01 发布