C和指针 13章练习题算是从书上转载的吧

来源:互联网 发布:linux服务器系统占有率 编辑:程序博客网 时间:2024/06/06 18:13
#include <stdio.h>#include <stdlib.h>#include <ctype.h>//计算从标准输入的几类字符的百分比,是书上的答案int is_not_print( int ch )//避免了主循环处理特殊情况的需要{        return !isprint( ch);}//用于区分每种类型的分类函数的跳转表static int ( *test_func[] )( int ) ={iscntrl,isspace,isdigit,islower,isupper,ispunct,is_not_print};#define N_CATEGORIES\   (sizeof( test_func)/ sizeof( test_func[ 0 ] ) )   //每种类型名字char *label [] ={        "control",        "whitespace",        "digit",        "lower case",        "upper case",        "punctuation",        "non_printable"};int count[ N_CATEGORIES ];int total;int main(){    int ch;    int category;    //读取和处理每个字符    while( ( ch = getchar()) !=  EOF )//这里就是需要回车换行,ctrl+z再回车          {                  total += 1;                  //为这个字符调用每个测试函数,结果为真,增加对应计数器的值                  for( category = 0; category < N_CATEGORIES;category += 1 )                        {                                if( test_func[category](ch))                                        count[category]+=1;                        }          }          //打印结果          if( total == 0 )          {                  printf("没有字符");          }          else          {                  for( category = 0; category < N_CATEGORIES; category += 1 )                  {                          printf( "%3.0f%% %s characters\n", count[ category ]*100.0/total,label[ category]);                  }          }    return 0;}

阅读全文
0 0
原创粉丝点击