当前位置:网站首页>Using C language to complete a simple calculator (function pointer array and callback function)
Using C language to complete a simple calculator (function pointer array and callback function)
2022-07-06 08:47:00 【C tie Zhu】
This article mainly uses the method of function pointer array and callback function to write a simple calculator .
Function pointer method
#include <stdio.h>
void menu() // Print menu functions
{
printf("*******************\n");
printf("****1.add 2.sub****\n");
printf("****3.mul 4.div****\n");
printf("***** 0.exit ******\n");
printf("*******************\n");
}
int ADD(int x, int y) // Addition function
{
return x + y;
}
int SUB(int x, int y) // Subtraction function
{
return x - y;
}
int MUL(int x, int y) // Multiplication function
{
return x * y;
}
int DIV(int x, int y) // Division function
{
return x / y;
}
int main()
{
int a, b, ret,input;
int(*p[5])(int, int) = { 0,ADD,SUB,MUL,DIV };// Define an array of function pointers , Pass in the addresses of the four functions ,
do // In order to match the subscript of the calling function with the input value , Let's open up another space in front .
{
menu();
printf(" Please enter the option :\n");
scanf("%d", &input);
if (input == 0)
printf(" Exit calculator \n");
else if (input > 0 && input < 5)
{
printf(" Please enter two operands :\n");
scanf("%d%d", &a, &b);
ret = p[input](a, b); // Call the function through the pointer in the function pointer array .
printf(" The calculation result is :%d\n", ret);
}
else
printf(" Input error , Please re-enter !\n");
} while (input);
return 0;
}
When writing this code , We must first think about how to implement this program , First we need to print the menu of this calculator , Then according to the input number to determine what operation we do , After the calculation, we need to go back and choose again , And then calculate , repeat , So we can write one first do……while loop , Then enter the option , According to the input content, write what the program will execute , You can use it if Statement to implement , And when we choose to calculate , First, we need to input the two operands to be calculated , And then calculate , We found that , The parameter types and return types of these four functions are the same , Then we can create an array of function pointers , Put the addresses of the four functions in , You can then dereference to use these functions , Then we will write some measures if the input is wrong , It completes such a function .
Callback function method
#include <stdio.h>
void menu() // Print menu functions
{
printf("*******************\n");
printf("****1.add 2.sub****\n");
printf("****3.mul 4.div****\n");
printf("***** 0.exit ******\n");
printf("*******************\n");
}
int ADD(int x, int y) // Addition function
{
return x + y;
}
int SUB(int x, int y) // Subtraction function
{
return x - y;
}
int MUL(int x, int y) // Multiplication function
{
return x * y;
}
int DIV(int x, int y) // Division function
{
return x / y;
}
void calat(int(*pa)(int, int)) //calat The function parameter of is the address of the function ,
{ // Different function addresses , Call different functions
int ret,x,y;
printf(" Please enter two operands :");
scanf("%d%d", &x, &y);
ret = pa(x, y);
printf(" The calculation result is :%d\n", ret);
}
int main()
{
int a, b, ret, input;
do
{
menu();
printf(" Please enter the option :\n");
scanf("%d", &input);
switch (input)
{
case 0:
printf(" Exit calculation !\n");
break;
case 1:
calat(ADD); // Different input values , to calat Functions pass different function addresses .
break;
case 2:
calat(SUB);
break;
case 3:
calat(MUL);
break;
case 4:
calat(DIV);
break;
default:
printf(" Input error , Please re-enter !\n");
break;
}
} while (input);
return 0;
}
When you first enter the function , The steps are the same as the above code , But here we use switch case Statement to realize the selection of calculation items , And we can write a function , Used to realize calculation , The parameter of this function can use the function address , When we enter different options , Give different function addresses to the calculation function , Then the calculation function calls a function to calculate according to the passed function address , This is a simple callback function .
边栏推荐
- Unified ordering background interface product description Chinese garbled
- The network model established by torch is displayed by torch viz
- Roguelike游戏成破解重灾区,如何破局?
- Computer cleaning, deleted system files
- What is CSRF (Cross Site Request Forgery)?
- Problems in loading and saving pytorch trained models
- Sublime text using ctrl+b to run another program without closing other runs
- Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Tcp/ip protocol
猜你喜欢
Chrome浏览器的crash问题
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
Crash problem of Chrome browser
Precise query of tree tree
Computer graduation design PHP Zhiduo online learning platform
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
visdom可视化实现与检查介绍
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
个人电脑好用必备软件(使用过)
随机推荐
egg. JS getting started navigation: installation, use and learning
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
Tcp/ip protocol
hutool优雅解析URL链接并获取参数
How to conduct interface test? What are the precautions? Nanny level interpretation
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
游戏解包的危害及资源加密的重要性
LeetCode:劍指 Offer 42. 連續子數組的最大和
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
被破解毁掉的国产游戏之光
View computer devices in LAN
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
TDengine 社区问题双周精选 | 第三期
sublime text的编写程序时的Tab和空格缩进问题
LeetCode:673. 最长递增子序列的个数
【嵌入式】使用JLINK RTT打印log
Deep analysis of C language data storage in memory
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
LeetCode:26. 删除有序数组中的重复项
生成器参数传入参数