C++primer 6.7节练习

练习6.54

1 int fun(int, int);
2 typedef int (*pf) (int, int);
3 vector<pf> a;
 1 int funAdd(int i, int j)
 2 {
 3     return i + j;
 4 }
 5 int funDel(int i, int j)
 6 {
 7     return i - j;
 8 }
 9 int funMul(int i, int j)
10 {
11     return i * j;
12 }
13 int funDiv(int i, int j)
14 {
15     return i / j;
16 }
17 
18 
19 int main()
20 {
21     typedef int (*pf) (int, int);
22     vector<pf> a(2);
23     a[0] = funAdd;
24     a[1] = funDel;
25     a[2] = funMul;
26     a[3] = funDiv;
27     cout << a[0](1, 2) << endl;
28     cout << a[1](8, 3) << endl;
29     system("pause");
30     return 0;
31 }

练习6.56

 1 #include <cstring>
 2 #include <vector>
 3 #include <stdexcept>
 4 #include "factmain.h"
 5 using namespace std;
 6 
 7 int funAdd(int i, int j)
 8 {
 9     return i + j;
10 }
11 int funDel(int i, int j)
12 {
13     return i - j;
14 }
15 int funMul(int i, int j)
16 {
17     return i * j;
18 }
19 int funDiv(int i, int j)
20 {
21     return i / j;
22 }
23 
24 
25 int main()
26 {
27     typedef int (*pf) (int, int);
28     vector<pf> a(4);
29     a[0] = funAdd;
30     a[1] = funDel;
31     a[2] = funMul;
32     a[3] = funDiv;
33     cout << a[0](1, 2) << endl;
34     cout << a[1](8, 3) << endl;
35     cout << a[2](2, 3) << endl;
36     cout << a[3](144, 12) << endl;
37     system("pause");
38     return 0;
39 }
posted @ 2017-08-04 15:36  五月份小姐  阅读(370)  评论(0编辑  收藏  举报