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

 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

 

转载于:https://www.cnblogs.com/alan-forever/archive/2013/06/07/3123684.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值