Get the App
SLTechnology News&Howtos  ›  Development  › 

The method and steps of realizing Calculator function by using function pointer Array in C language

Shulou Source: shulou.com Published: 2022-06-03 07:19:45 10月04日 Update

This article mainly explains "the methods and steps of C language using function pointer array to realize calculator function". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the methods and steps of C language using function pointer array to realize calculator function.

Let's review the concept first:

Pointer array-the array where pointers are stored

Function pointer-A pointer that stores the address of a function

Array of function pointers-an array of function pointers

Next, let's talk about the functions of the calculator to be made 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 program, and print the menu first 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, define input as the number you want to enter to select the function; define x and y as the operands you want to enter; and then call the menu function menu () in the main program.

Int main () {int x = 0; int y = 0; int input = 0; menu ();}

(4) use the do while loop, and the if else statement to establish the rules, exit the loop if the input input is 0, while (input) represents a non-zero input will continue the cycle; if the input between 1 and 4 corresponds to the calculator menu function, then you need to enter two numbers; if beyond this range then re-cycle.

Int main () {int x = 0; int y = 0; int input = 0; menu (); do {printf ("Please select what you want to do:\ n"); scanf ("% d", & input); if (input = = 0) {printf ("quit calculator!\ n");} else if (input > = 1 & input = 1 & input

Tags: Functions pointers arrays calculators selections inputs functions two numbers menus methods errors loops steps language next code representatives elements content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux MariaDB Xiaomi NVidia