C Language sets up a function process to implement different functions when calling it ., Function process
// Kailu garji-blog Park Co., http://www.cnblogs.com/kailugaji/.
Enter a, B, the first call of process to find the maximum value, the second call of process to find the minimum value, the third call of the sum.
Method 1:
1 # include <stdio. h> 2 // calculate the maximum value of 3 int max (int x, int y) {4 return x> y? X: y; 5} 6 // minimum 7 int min (int x, int y) {8 return x <y? X: y; 9} 10 // sum 11 int add (int x, int y) {12 return x + y; 13} 14 // 15 void process (int x, int y, int (* fun) {16 int z; 17 if (fun = max) 18 z = max (x, y); 19 if (fun = min) 20 z = min (x, y); 21 if (fun = add) 22 z = add (x, y); 23 printf ("% d \ n", z); 24} 25 26 void main () {27 int a, B; 28 printf ("Please input a and B: \ n"); 29 scanf ("% d", & a, & B ); 30 printf ("max ="); 31 process (a, B, max); 32 printf ("min ="); 33 process (a, B, min ); 34 printf ("sum ="); 35 process (a, B, add); 36}
Method 2:
1 # include <stdio. h> 2 // calculate the maximum value of 3 int max (int x, int y) {4 return x> y? X: y; 5} 6 // minimum 7 int min (int x, int y) {8 return x <y? X: y; 9} 10 // sum 11 int add (int x, int y) {12 return x + y; 13} 14 15 void main () {16 int, b; 17 int (* process) (int, int); 18 19 printf ("Please input a and B: \ n"); 20 scanf ("% d ", & a, & B); 21 22 process = max; 23 printf ("max = % d \ n", process (a, B); 24 25 process = min; 26 printf ("min = % d \ n", process (a, B); 27 28 process = add; 29 printf ("sum = % d \ n ", process (a, B); 30}
Result: