C语言学习5

设一个函数process,在调用它的时候,每次实现不同的功能。

输入a和b两个数,第一次调用process时找出a和b中大者,第二次找出其中的小者,第三次求a与b之和。

 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     int max(int, int);
 6     int min(int, int);
 7     int add(int, int);
 8 
 9     void process(int, int, int(*fun)() );
10 
11     int a, b;
12 
13     printf("Enter a and b: ");
14     scanf("%d %d", &a, &b);
15 
16     printf("max = ");
17     process(a, b, max);
18 
19     printf("min = ");
20     process(a, b, min);
21 
22     printf("sum = ");
23     process(a, b, add);
24 }
25 
26 int max(int x, int y)
27 {
28     int z;
29     if(x>y)
30     {
31         z = x;
32     }
33     else
34     {
35         z = y;
36     }
37     return(z);
38 }
39 
40 int min(int x, int y)
41 {
42     int z;
43     if(x>y)
44     {
45         z = y;
46     }
47     else
48     {
49         z = x;
50     }
51     return(z);
52 }
53 
54 int add(int x, int y)
55 {
56     int z;
57     z = x + y;
58     return(z);
59 }
60 
61 void process(int x, int y, int(*fun)() )
62 {
63     int result;
64     result = (*fun)(x, y);
65     printf("%d\n", result);
66 }

猜你喜欢

转载自www.cnblogs.com/wangkeqi/p/9354978.html