Personal implementation example:
# Include <stdio. h> <br/> # include <string. h> <br/> # define M 4 <br/> int add (int A, int B); <br/> int sub (int A, int B ); <br/> int MUL (int A, int B); <br/> int Div (int A, int B); <br/> int (* oper_func []) (INT, INT) ={< br/> Add, sub, Mul, div <br/> }; <br/> char oper_sequence [m] [10] ={< br/> "add", "sub", "Mul", "Div" <br/> }; <br/> int main () <br/> {<br/> char sequence [10]; <br/> int seq; <br/> int A, B; <br/> int result; <br/> int I; <br/> printf ("OPERATOR:"); <br/> scanf ("% s", callback ); <br/> printf ("A:"); <br/> scanf ("% d", & A); <br/> printf ("B :"); <br/> scanf ("% d", & B); <br/> for (I = 0; I <m; I ++) <br/> {<br/> If (strncmp (oper_sequence [I], sequence, 3) = 0) <br/> seq = I; <br/>}< br/> result = oper_func [seq] (a, B); <br/> printf ("result is % d/N", result ); <br/> return 0; <br/>}< br/> int add (int A, int B) <br/>{< br/> return A + B; <br/>}< br/> int sub (int A, int B) <br/>{< br/> return A-B; <br/>}< br/> int MUL (int A, int B) <br/>{< br/> return a * B; <br/>}< br/> int Div (int A, int B) <br/>{< br/> return A/B; <br/>}</P> <p>
<C and pointer> original:
Conversion table (jump table)
It is best to use an example to explain the transfer table. The following code snippet is taken from a program to implement a pocket calculator. The other part of the program has read two numbers (OP1 and OP2) and one operator (operator ). The following code tests the operators and determines which function to call.
Switch)
{
Case Add: Result = add (OP1, OP2); break;
Case Sub: Result = sub (OP1, OP2); break;
Case Mul: result = MUL (OP1, OP2); break;
Case Div: Result = div (OP1, OP2); break;
......
}
For a novel calculator with hundreds of operators, this switch statement will be very long. Why do I need to call a function to perform these operations? Separating specific operations from the code for selecting operations is a good design. More complex operations will certainly be implemented using independent functions, because they may be long in length. However, even simple operations may have side effects, such as saving a constant value for future operations.
To use the switch statement, the code of the operator must be an integer. If they are continuous integers starting from scratch, we can use a conversion table to implement the same task. A conversion table is an array of function pointers.
Two steps are required to create a conversion table. First, declare and initialize a function pointer array. The only thing to note is that the prototype of these functions appears before the declaration of this array.
Double add (double, double );
Double sub (double, double );
Double MUL (double, double );
Double Div (double, double );
Double (* oper_func []) (double, double) = {Add, sub, Mul, Div ,...};
The correct order of function names in the initialization list depends on the integer code used in the program to represent each operator. This example assumes that add is 0, sub is 1, mul is 2, and so on.
The second step is to replace the previous switch statement with the following statement!
Result = oper_func [condition] (OP1, OP2 );
The handler selects the correct function pointer from the array, and the function call operator will execute this function.