函数指针(习题课后答案)

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <ctype.h>
 4 
 5 int is_not_print( int ch )
 6 {
 7     return !isprint( ch );
 8 }
 9 
10 static int(*test_func[])( int ) = {
11     iscntrl,
12     isspace,
13     isdigit,
14     islower,
15     isupper,
16     ispunct,
17     is_not_print
18 };
19 #define N_CATEGORIES ( sizeof( test_func ) / sizeof( test_func[0] ) )
20 
21 char* label[] = {
22     "control",
23     "whitespace",
24     "digit",
25     "lower case",
26     "upper case",
27     "punctuation",
28     "non-printable"
29 };
30 
31 int count[ N_CATEGORIES ];
32 int total;
33 
34 int main()
35 {
36     int ch;
37     int category;
38 
39     while( (ch = getchar()) != EOF )
40     {
41         total += 1;
42         for( category = 0; category < N_CATEGORIES; category += 1 )
43             if( test_func[ category ]( ch ) )
44                 count[ category ] += 1;
45     }
46 
47     if( total == 0 )
48         printf( "No characters in the input!\n" );
49     else
50     {
51         for( category = 0; category < N_CATEGORIES; category += 1 )
52         {
53             printf( "%3.0f%% %s characters\n", count[ category ] * 100.0 / total, label[ category ] );
54         }
55     }
56     return EXIT_SUCCESS;
57 }
View Code

 

posted @ 2013-06-07 13:36  alan_forever  阅读(307)  评论(0编辑  收藏  举报