網站首頁 編程語言 正文
前言
前面我們學習了各種各樣的指針類型,有些指針可以說是稀奇百怪,特別是函數指針,有些朋友可能覺得,函數指針有些多余,調用函數為什么要用指針調用,直接調用不好嗎?
接下來我們從具體的實例來回答同學們的問題,加深對函數指針的理解。
計算器的例子
接下來我們寫一個簡單的計算器程序,完成不同的計算功能比如加減乘除:
#include <stdio.h>
//相加函數
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 div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
do
{
printf("*************************\n");
printf(" 1:add 2:sub \n");
printf(" 3:mul 4:div \n");
printf("*************************\n");
printf("請選擇:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("輸入操作數:");
scanf("%d %d", &x, &y);
ret = add(x, y);
printf("ret = %d\n", ret);
break;
case 2:
printf("輸入操作數:");
scanf("%d %d", &x, &y);
ret = sub(x, y);
printf("ret = %d\n", ret);
break;
case 3:
printf("輸入操作數:");
scanf("%d %d", &x, &y);
ret = mul(x, y);
printf("ret = %d\n", ret);
break;
case 4:
printf("輸入操作數:");
scanf("%d %d", &x, &y);
ret = div(x, y);
printf("ret = %d\n", ret);
break;
case 0:
printf("退出程序\n");
break;
default:
printf("選擇錯誤\n");
break;
}
} while (input);
return 0;
}
我們很明顯地感覺到,是不是代碼實在是有些繁瑣,不同的功能之間有著高度的相似性,比如說進去之后都需要對x和y賦值,最后又都需要打印一個返回值,但在當前的框架下似乎又很難進行簡化。
這時,我們就進入了今天的主題,如何使用函數指針來給程序瘦瘦身。
回調函數
我們先來看看使用回調函數修改之后的程序。
首先我們在原來的基礎上構造了一個新的函數,函數的形參就是一個函數指針的形式:
//使用回調函數
void cumulate_numbers(int (*cumulate)(int, int))
{
int x, y;
int ret = 0;
printf("輸入操作數:");
scanf("%d %d", &x, &y);
ret = cumulate(x, y);
printf("ret = %d\n", ret);
}
然后我們來看看調用函數方面有什么改變:
scanf("%d", &input);
switch (input)
{
case 1:
cumulate_numbers(add);
break;
case 2:
cumulate_numbers(sub);
break;
case 3:
cumulate_numbers(mul);
break;
case 4:
cumulate_numbers(div);
break;
case 0:
printf("退出程序\n");
break;
default:
printf("選擇錯誤\n");
break;
}
} while (input);
我們可以清楚地認識到每次實現功能,通過函數指針使一開始的
printf("輸入操作數:");
scanf("%d %d", &x, &y);
ret = div(x, y);
printf("ret = %d\n", ret);
轉變為只要調用一個函數,一行代碼就搞定了。我們這些功能比較簡單,功能的數量也不多,使四行代碼變成了一行,如果功能復雜一些,功能的數量再多一些,對于代碼的簡化量還是非常可觀的。
那么接下來能否讓我們更進一步呢?
轉移表
我們再來看看更加簡潔的代碼:
int (*p[5])(int, int) = { 0, add, sub, mul, div };
do
{
printf("*************************\n");
printf(" 1:add 2:sub \n");
printf(" 3:mul 4:div \n");
printf("*************************\n");
printf("請選擇:");
scanf("%d", &input);
if ((input <= 4 && input >= 1))
{
printf("輸入操作數:");
scanf("%d %d", &x, &y);
ret = (*p[input])(x, y);
printf("ret = %d\n", ret);
}
else if(input != 0)
printf("輸入有誤\n");
} while (input);
運用了指針數組,這個方法叫做轉移表。
原文鏈接:https://blog.csdn.net/JDSZGLLL/article/details/125271852
相關推薦
- 2022-06-25 Python利用format函數實現對齊打印(左對齊、右對齊與居中對齊)_python
- 2023-04-18 go實現服務優雅關閉的示例_Golang
- 2022-04-11 jackson中對null的處理
- 2022-09-23 Go語言結構體Go?range的學習教程_Golang
- 2023-03-28 python中向二維數組中添加整行或者增列元素問題_python
- 2022-07-04 Android實現顏色漸變動畫效果_Android
- 2022-11-17 有意思的數據結構默克樹?Merkle?tree應用介紹_其它綜合
- 2022-08-13 采用python開發sparkstreming全流程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支