当前位置:网站首页>利用函数指针数组实现计算器编写
利用函数指针数组实现计算器编写
2022-07-19 05:03:00 【发烧的CPU】
目录
一、函数指针
关于函数指针,先看看两段代码:
(* (void(*)())1)()
是不是晦涩难懂,其实要以1为突破口,1的前面实际上是void(*)()函数指针,这里发生了强制类型转换,之后再通过*访问,再进行传参调用。
void(* print(char, void(*)(double)))(double)
声明的print函数的第一个参数的类型是char,第二个参数的类型是函数指针类型void(*)(double),返回类型是void(*)(double)。
再来看看函数的地址传参:
int Add(int x, int y)
{
return x + y;
}
void print(int(*pAdd)(int, int))//用函数指针类型来作为形参接收
{
int a = 9;
int b = 9;
int ret = (*pAdd)(9, 9);//也可写为int ret = pAdd(9,9);pAdd作为指针变量,指向Add函数,给两个参数9和9进行计算
printf("%d\n", ret);
}
int main()
{
print(Add);//传参为Add函数的地址
return 0;
}
二、计算器的实现
函数指针数组,即把函数的地址存到数组中。
void(*arr[10])();
可以这样理解,void(*p)()不就是函数指针吗,把p换成arr[10]不就变成了函数指针数组吗。
常规方法实现计算器:
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
int input = 0;
int m = 0;
int n = 0;
int ret = 0;
printf("************************************\n");
printf("********** 1.add 2.sub **********\n");
printf("********** 3.mul 4.div **********\n");
printf("********** 0.exit *****************\n");
printf("************************************\n");
do
{
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入任意两个整数:");
scanf("%d %d", &m, &n);
ret = Add(m, n);
printf("%d\n", ret);
break;
case 2:
printf("请输入任意两个整数:");
scanf("%d %d", &m, &n);
ret = Sub(m, n);
printf("%d\n", ret);
break;
case 3:
printf("请输入任意两个整数:");
scanf("%d %d", &m, &n);
ret = Mul(m, n);
printf("%d\n", ret);
break;
case 4:
printf("请输入任意两个整数:");
scanf("%d %d", &m, &n);
ret = Div(m, n);
printf("%d\n", ret);
break;
case 0:
printf("退出程序\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
优化版(使用函数指针来实现):
#include<stdio.h>
void menu()
{
printf("*********************\n");
printf("*****1.Add 2.Sub*****\n");
printf("*****3.Mul 4.Div*****\n");
printf("*****0.退出程序******\n");
printf("*********************\n");
}
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
void print(int (*p)(int, int))
{
int a = 0;
int b = 0;
int ret = 0;
printf("请输入两个操作数:");
scanf("%d %d", &a, &b);
ret = p(a, b);
printf("%d\n", ret);
}
int main()
{
int input = 0;
menu();
do
{
printf("请选择运算种类:\n");
scanf("%d", &input);
switch (input)
{
case 1:
print(Add);
break;
case 2:
print(Sub);
break;
case 3:
print(Mul);
break;
case 4:
print(Div);
break;
case 0:
printf("退出程序\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
高阶版(使用函数指针数组来实现):
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
printf("*********************\n");
printf("*****1.Add 2.Sub*****\n");
printf("*****3.Mul 4.Div*****\n");
printf("*****0.退出程序******\n");
printf("*********************\n");
int x = 0;
int y = 0;
int input = 1;
int ret = 0;
int (*parr[5])(int x, int y) = { 0,Add,Sub,Mul,Div };//函数指针数组的用途:转移表
while (input)
{
printf("请选择:");
scanf("%d", &input);
if (input >= 1 && input <= 4)
{
scanf("%d %d", &x, &y);
ret = (*parr[input])(x, y);
printf("%d\n", ret);
}
else if (0 == input)
{
printf("退出程序\n");
}
else
{
printf("选择错误\n");
}
}
return 0;
}
边栏推荐
- When submitting, it shows that no matching host key type can be found.
- The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"
- STM32-使用定时器做延时函数时遇到的坑
- C language - linked list creation - merge - delete and other common retest operations
- 关于clean时,IDE提示Some problems were encountered while building the effective model for
- swing窗体打jar包后找不到图片的问题
- 第六十七篇:opencv中KeyPoint与point2f之间相互转换
- 多线程基础入门学习(带示例代码)
- 整數的分劃問題
- Input text to automatically generate images, it's so fun!
猜你喜欢
随机推荐
Rookie teaches you to repair USB flash disk
基于STM32F103,用蜂鸣器播放歌曲
Securityerror: (:) [] occurs when CMD executes the command, parentcontainserrorrecordexception
Common settings for Alfred
STM32-仿真调试时的SystemInit陷阱
Input text to automatically generate images, it's so fun!
An open source web drawing board is really convenient
MySQL获取当天,昨天,本周,上周,本月,上月的起始时间和结束时间的方法
There is a problem that the picture cannot be found after the jar package is printed on the swing form
基本页面状态码
微擎系统在生产运行异常
MySQL gets the start time and end time of the current day, yesterday, this week, last week, this month and last month
线性结构理解
Pytorch yolo4 training any training set
Openwrt manually installs the netdata plug-in
pytorch 实现数据增强分类 albumentations的使用
分享本周所学——Transformer模型详解
Express comment
STM32-基于汇编来分析延时
基于STM32F030的ADC功能实现