本文實例為大家分享了C語言運用回調(diào)函數(shù)實現(xiàn)計算器的具體代碼,供大家參考,具體內(nèi)容如下
回調(diào)函數(shù)概念:
回調(diào)函數(shù)是一個通過函數(shù)指針調(diào)用的函數(shù),也就是將函數(shù)的地址作為參數(shù)傳遞給另一個函數(shù),當(dāng)這個指針回來調(diào)用其指向的函數(shù)時,稱為回調(diào)函數(shù)。
本次制作計算器的功能:
1.add —— 加法
2.sub —— 減法
3.mul —— 乘法
4.div —— 除法
0.exit —— 退出
具體來通過代碼講解:
(1)首先寫一個菜單函數(shù),在運行程序時打印菜單
1
2
3
4
5
6
7
8
9
10
|
void menu() { printf ( "*************************\n" ); printf ( "******* 1.add *******\n" ); printf ( "******* 2.sub *******\n" ); printf ( "******* 3.mul *******\n" ); printf ( "******* 4.div *******\n" ); printf ( "******* 0.exit *******\n" ); printf ( "*************************\n" ); } |
(2)寫好四個關(guān)于加、減、乘、除操作的函數(shù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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; } |
(3)寫主函數(shù),定義input是你要輸入的數(shù),用來選擇計算器的功能;然后使用do while循環(huán),內(nèi)嵌菜單函數(shù)。
1
2
3
4
5
6
7
8
9
10
11
|
int main() { int input = 0; do { menu(); printf ( "請選擇你要進行的操作:\n" ); scanf ( "%d" ,&input); } while (input); return 0; } |
(4)用switch語句定義每個數(shù)字相對應(yīng)的功能;default代表其他選項;Calc函數(shù)是我們接下來要寫的回調(diào)函數(shù)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
int main() { int input = 0; do { menu(); printf( "請選擇你要進行的操作:\n" ); scanf( "%d" ,&input); switch (input) { case 1: Calc( Add ); break; case 2: Calc(Sub); break; case 3: Calc(Mul); break; case 4: Calc(Div); break; case 0: printf( "退出計算器\n" ); break; default : printf( "選擇錯誤,請重新選擇!\n" ); break; } } while (input); return 0; } |
(5)定義Calc函數(shù),用一個函數(shù)指針作為參數(shù)接收(Add、Sub、Mul、Div)函數(shù)的地址;pf作為函數(shù)指針直接指向相應(yīng)函數(shù);然后輸出結(jié)果;(這個回調(diào)函數(shù)csdn似乎識別不了,編譯器是可以的,所以就以注釋的形式寫了,后面也是一樣)。
1
2
3
4
5
6
7
8
9
10
|
void Calc() //括號內(nèi)容:int(*pf)(int, int) { int x = 0; int y = 0; int ret = 0; printf ( "請輸入2個操作數(shù):\n" ); scanf ( "%d %d" ,&x,&y); ret = pf(x,y); printf ( "%d\n" ,ret); } |
最后送上全部的代碼小小總計一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include <stdio.h> void menu() { printf ( "*************************\n" ); printf ( "******* 1.add *******\n" ); printf ( "******* 2.sub *******\n" ); printf ( "******* 3.mul *******\n" ); printf ( "******* 4.div *******\n" ); printf ( "******* 0.exit *******\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 Calc() //括號內(nèi)容:int(*pf)(int, int) { int x = 0; int y = 0; int ret = 0; printf ( "請輸入2個操作數(shù):\n" ); scanf ( "%d %d" ,&x,&y); ret = pf(x, y); printf ( "%d\n" , ret); } int main() { int input = 0; do { menu(); printf ( "請選擇你要進行的操作:\n" ); scanf ( "%d" ,&input); switch (input) { case 1: Calc(Add); break ; case 2: Calc(Sub); break ; case 3: Calc(Mul); break ; case 4: Calc(Div); break ; case 0: printf ( "退出計算器\n" ); break ; default : printf ( "選擇錯誤,請重新選擇!\n" ); break ; } } while (input); return 0; } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/m0_59063052/article/details/120252889