table - driven
C++语言: Codee#25728
01 /*
02 table-driven example from <<thinking in cpp>>
03 */
04
05
06 #include <iostream>
07 using namespace std;
08
09 #define DF(N) void N(){\
10 cout << "function"#N " called..."<<endl;}
11
12 DF(a);
13 DF(b);
14 DF(c);
15 DF(d);
16
17 void (*func_table[])() = {a, b, c, d};
18
19 int main()
20 {
21 while(1)
22 {
23 cout << "press a key from 'a' to 'd'\
24 or 'q' to quit" << endl;
25 char c, cr;
26 cin.get(c);
27 cin.get(cr);
28 if(c == 'q')
29 break;
30 if(c < 'a' || c > 'd')
31 continue;
32 (*func_table[c - 'a'])();
33 }
34 return 0;
35 }
02 table-driven example from <<thinking in cpp>>
03 */
04
05
06 #include <iostream>
07 using namespace std;
08
09 #define DF(N) void N(){\
10 cout << "function"#N " called..."<<endl;}
11
12 DF(a);
13 DF(b);
14 DF(c);
15 DF(d);
16
17 void (*func_table[])() = {a, b, c, d};
18
19 int main()
20 {
21 while(1)
22 {
23 cout << "press a key from 'a' to 'd'\
24 or 'q' to quit" << endl;
25 char c, cr;
26 cin.get(c);
27 cin.get(cr);
28 if(c == 'q')
29 break;
30 if(c < 'a' || c > 'd')
31 continue;
32 (*func_table[c - 'a'])();
33 }
34 return 0;
35 }