Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use callback function to realize Calculator in C language

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how C language uses callback function to realize calculator, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.

Callback function concept:

A callback function is a function called by a function pointer, that is, passing the address of the function as an argument to another function, and when the pointer comes back to call the function it points to, it is called a callback function.

The functions of making calculator this time:

1.add-addition

2.sub-subtraction

3.mul-multiplication

4.div-division

0.exit-exit

Specifically, it is explained through the code:

(1) first write a menu function to print the menu when running the program.

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) write four functions about addition, subtraction, multiplication and division.

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) write the main function and define input as the number you want to enter to select the function of the calculator; then use the do while loop to embed menu functions.

Int main () {int input = 0; do {menu (); printf ("Please select what you want to do:\ n"); scanf ("% d", & input);} while (input); return 0;}

(4) use the switch statement to define the function corresponding to each number; default represents other options; and the Calc function is the callback function that we will write next.

Int main () {int input = 0; do {menu (); printf ("Please choose what you want to do:\ 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 ("quit Calculator\ n"); break Default: printf ("wrong choice, please re-select! \ n "); break;}} while (input); return 0;}

(5) define the Calc function, use a function pointer as an argument to receive the address of the (Add, Sub, Mul, Div) function; pf as a function pointer directly points to the corresponding function; and then output the result; (the callback function csdn does not seem to recognize, the compiler can, so wrote in the form of comments, the following is the same).

Void Calc () / / parenthesis content: int (* pf) (int, int) {int x = 0; int y = 0; int ret = 0; printf ("Please enter 2 operands:\ n"); scanf ("% d% d", & x Magistry); ret = pf (XMagney); printf ("% d\ n", ret);}

Finally, send all the code to sum up:

# include 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 () / / parentheses content: int (* pf) (int, int) {int x = 0; int y = 0; int ret = 0; printf ("Please enter 2 operands:\ n"); scanf ("% d% d", & xQuery); ret = pf (x, y); printf ("% d\ n", ret) } int main () {int input = 0; do {menu (); printf ("Please select what you want to do:\ 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 ("exit Calculator\ n"); Default: printf ("wrong choice, please re-select! \ n "); break;}} while (input); return 0;} Thank you for reading this article carefully. I hope the article" how to use callback function to realize Calculator in C language "shared by the editor will be helpful to everyone. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report