(字符串)统计字母和数字个数,打印柱状图

1. 从键盘上输入字符,(1)分别统计一下其中字母,数字,其他字符的个数, 

(2)将统计的字母,数字,其他字符的个数以柱状图的形式打印。例如

  5

*****

*****     3

*****   *****     2

*****   *****   *****

*****   *****   *****

 alp     num     oth

这道题的一点难度就在输出,我们不能全部输出字母的柱状图再输出数字的柱状图,因为输出只能按行

输出,所以我们需要对没个柱状图再什么时候输出需要有一个判断,那就是我们判断这个柱状图在哪一个

列需要输出。代码如下。

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4. #include<algorithm>  
  5. using namespace std;  
  6. #define max 100  
  7.   
  8. void count_diff_char()  
  9. {  
  10.     int char_num=0,integer_num=0,else_num=0;  
  11.     char str[max];  
  12.     int index;  
  13.     int index_p;  
  14.     while(fflush(stdin),gets(str)!=NULL)  
  15.     {  
  16.         index=0,char_num=0,integer_num=0,else_num=0;  
  17.         while(str[index]!='')  
  18.         {  
  19.             if(str[index]>='a'&&str[index]<='z')  
  20.                 char_num++;  
  21.             else if(str[index]>='0'&&str[index]<='9')  
  22.                 integer_num++;  
  23.             else  
  24.                 else_num++;  
  25.             index++;  
  26.         }  
  27.         //打印结果  
  28.         index=char_num>=integer_num? (char_num>=else_num? char_num:else_num):(integer_num>=else_num? integer_num:else_num);  
  29.         index_p=0;  
  30.         while(index>=0)  
  31.         {  
  32.             if(index==char_num)  
  33.             {  
  34.                 printf("%d",char_num);  
  35.                 index_p++;  
  36.             }  
  37.             else if(index<char_num)  
  38.             {  
  39.                 printf("*****");  
  40.                 index_p+=5;  
  41.             }  
  42.   
  43.   
  44.   
  45.             while(index_p<7)  
  46.             {  
  47.                 printf(" ");  
  48.                 index_p++;  
  49.             }  
  50.             if(index==integer_num)  
  51.             {  
  52.                 printf("%d",integer_num);  
  53.                 index_p++;  
  54.             }  
  55.             else if(index<integer_num)  
  56.             {  
  57.                 printf("*****");  
  58.                 index_p+=5;  
  59.             }  
  60.   
  61.   
  62.   
  63.             while(index_p<14)  
  64.             {  
  65.                 printf(" ");  
  66.                 index_p++;  
  67.             }  
  68.             if(index==else_num)  
  69.             {  
  70.                 printf("%d",else_num);  
  71.                 index_p++;  
  72.             }  
  73.             else if(index<else_num)  
  74.             {  
  75.                 printf("*****");  
  76.                 index_p+=5;  
  77.             }  
  78.             printf(" ");  
  79.             index--;  
  80.             index_p=0;  
  81.         }  
  82.         //打印下标  
  83.         index_p=0;  
  84.         while(index_p<=14)  
  85.         {  
  86.             if(index_p==0)  
  87.             {  
  88.                 printf("alp");  
  89.                 index_p+=3;  
  90.             }  
  91.             else if(index_p==7)  
  92.             {  
  93.                 printf("num");  
  94.                 index_p+=3;  
  95.             }  
  96.             else if(index_p==14)  
  97.             {  
  98.                 printf("oth");  
  99.                 index_p+=3;  
  100.             }  
  101.             else  
  102.             {  
  103.                 printf(" ");  
  104.                 index_p++;  
  105.             }  
  106.         }  
  107.         printf(" ");  
  108.     }  
  109. }  
原文地址:https://www.cnblogs.com/xzzzh/p/6435123.html