当前位置:网站首页>[C language] use function pointers to make a different calculator
[C language] use function pointers to make a different calculator
2022-07-28 20:18:00 【White Hibiscus KK】
List of articles
A function pointer
A function pointer is a pointer that stores the address of a function .
Important content of function pointer :
1. The use of function pointers : Type of function ( The pointer )( The formal parameter type of the function )= Function name ; Be careful : There are variable names that do not need formal parameters of functions ., If a function has no parameters , There is no need to fill in anything ,void Not good either. . After using this , This pointer is a function pointer .
2. Function pointer calls the method of the function :****( The pointer )( Actual parameters ); Be careful : There are two ways to express the pointer here , For example, it defines a name called *p Function pointer of , The return type of the function is null , No formal parameters . The call here can be written as (*p)(); Or you could write it as p(); These two ways can be written , But for convenience , The first one is not recommended .
3. Function pointers can be used as formal parameters of functions , If you need to pass arguments , You can directly pass the function name .
Realize the idea of calculator program
First of all, calculators should have the most basic operations , That is to say Add, subtract, multiply and divide , Secondly, we should write a simple menu , Then let the user choice , After the selection is Input number Conduct Calculate and print come out , Considering that users may have to Multiple use , So you can add some circular statements , Let users use it many times .
Basically, the problems we need to consider in implementing calculators are all here , In this program , The most important thing is to use array pointers to call the functions of addition, subtraction, multiplication and division , The other parts are simple , Now let's implement this part of the function .
Call the addition, subtraction, multiplication and division function with the function pointer
First look at the code :
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 exc(int x, int y)
{
return x / y;
}
void func(int *(pf)(int, int y))
{
int x = 0;
int y = 0;
printf(" Please enter two numbers :");
scanf("%d%d", &x, &y);
int ret = pf(x, y);
printf(" The result is :%d\n", ret);
}
stay func In this function , A formal parameter is a function pointer , because The return value of addition, subtraction, multiplication and division , The formal parameters are the same , So just pass different functions in , As mentioned earlier, formal parameters are function pointers that can be directly Pass function name , That's right. func It can realize the operation of the corresponding function and print it out .
Complete code + Run a screenshot
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void menu()
{
printf("|-------------------|\n");
printf("|-------1. Add --------|\n");
printf("|-------2. reduce --------|\n");
printf("|-------3. ride --------|\n");
printf("|-------4. except --------|\n");
printf("|-------0. sign out ------|\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 exc(int x, int y)
{
return x / y;
}
void func(int *(pf)(int, int y))
{
int x = 0;
int y = 0;
printf(" Please enter two numbers :");
scanf("%d%d", &x, &y);
int ret = pf(x, y);
printf(" The result is :%d\n", ret);
}
int main()
{
int n = 0;
int flag = 1;
while (flag)
{
menu();
printf(" Please select the operation you want :");
scanf("%d", &n);
switch (n)
{
case 1:
func(add);
break;
case 2:
func(sub);
break;
case 3:
func(mul);
break;
case 4:
func(exc);
break;
case 0:
flag = 0;
break;
default:
printf(" Wrong choice , Please reselect !\n");
break;
}
}
return 0;
}


边栏推荐
- What is the variance?
- 一文读懂如何部署具有外部数据库的高可用 K3s
- NEIL: Extracting Visual Knowledge from Web Data
- 软考中级(系统集成项目管理工程师)高频考点
- adb remount of the / superblock failed: Permission denied
- Article translation software - batch free translation software supports major translation interfaces
- [C language] shutdown game [loop and switch statement]
- How can Plato obtain premium income through elephant swap in a bear market?
- 中国能否在元宇宙的未来发展中取得突破,占领高地?
- Digital filter design matlab
猜你喜欢

为什么客户支持对SaaS公司很重要?

C语言数据 3(2)

“中国网事·感动2022”二季度网络感动人物评选结果揭晓
![[C language] random number generation and `include < time. H > 'learning](/img/bb/3e47bf2e3b25653d9048884d65cda3.png)
[C language] random number generation and `include < time. H > 'learning

Source insight project import and use tutorial

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation

83.(cesium之家)cesium示例如何运行

Basic usage of docker

83. (cesium home) how the cesium example works

zfoo增加类似于mydog的路由
随机推荐
Quick sort template
Handan, Hebei: expand grassroots employment space and help college graduates obtain employment
CDGA|工业互联网行业怎么做好数据治理?
Andorid system layout, values, drawable adaptation
Sequential linear table - practice in class
local/chain/run_ tdnn.sh:
Array method added in ES6
Reverse string
The privatized instant messaging platform protects the security of enterprise mobile business
JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs
What is the variance?
Store and guarantee rancher data based on Minio objects
[C language] Gobang game [array and function]
Array out of bounds
9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
Using typedef in C language to change the name of data type
1、 Relationship among CPU, memory and hard disk
Find the memory occupied by the structure
河北邯郸:拓展基层就业空间 助力高校毕业生就业
How to automatically store email attachments in SharePoint