利用函数指针数组写一个计算器
作者:互联网
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void muen(){
printf("=======================\n");
printf("==1.加 2.减 3.乘 4.除==\n");
printf("==========0.退出=======\n");
printf("=======================\n");
}
int jia(int x, int y){
return x + y;
}
int jian(int x, int y){
return x - y;
}
int chen(int x, int y){
return x * y;
}
int chu(int x, int y){
return x / y;
}
int main(){
int input = 0;
int x = 0;
int y = 0;
int(*arr[])(int, int) = {0,jia,jian,chen,chu};
//创建一个函数指针数组
do
{
muen();
printf("请输入:>");
scanf("%d", &input);
if (input >= 1 && input < 5)
{
printf("请输入两个操作数:>");
scanf("%d%d", &x, &y);
int ret=arr[input](x, y);
printf("%d\n", ret);
}
else if (input == 0)
{
printf("退出\n");
}
else
{
printf("输入有误\n");
}
} while (input);
return 0;
}
标签:arr,return,jian,int,计算器,数组,printf,input,函数指针 来源: https://blog.51cto.com/14893161/2540399