C语言K&R习题系列——统计文档中每个单词所包含的字母个数,以直方图形式输出

原题:


Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.

这也是我第一个过百行的代码(带注释,空格什么的)

主要分两个部分:输入和输出


#include < stdio.h >
 
#define MAXWORDLEN 10
 
main ( void )
{
    int c;
    int wordLen = 0;
    int thisIdx = 0;
    long lengthArray[MAXWORDLEN + 1];
    long thisVal = 0;
    long maxVal = 0;
     
    //initialize
     
    int inspace = 0;
    int firstLetter = 1;
    int done = 0;
     
    for ( thisIdx = 0; thisIdx <= MAXWORDLEN; thisIdx++ )
    {
        lengthArray[thisIdx] = 0;
    }
     
    while ( done == 0 )
    {
        c = getchar();
         
        if ( c == ' ' || c == '\n' || c == '\t' || c == EOF )
        {
            if ( inspace == 0 )
            {
                inspace = 1;
                firstLetter = 0;
                 
                if( wordLen <= MAXWORDLEN )
                {
                    thisVal = ++lengthArray[wordLen - 1];
                     
                    if  ( thisVal > maxVal )
                    {
                        maxVal = thisVal;
                    }
                }
                else
                {
                    thisVal = ++lengthArray[MAXWORDLEN];
                     
                    if  ( thisVal > maxVal )
                    {
                        maxVal = thisVal;
                    }
                }
            }
            if ( c    == EOF )
            {
                done = 1;
            }
        }
        else
        {
            if ( inspace == 1 || firstLetter == 1 )
            {
                wordLen = 0;
                inspace = 0;
                firstLetter = 0;
            }
             
            ++wordLen;
        }
    }


之后为输出部分


for ( thisVal = maxVal; thisVal > 0; thisVal-- )
      {
            printf ( "%4d  |", thisVal );
            for ( thisIdx = 0; thisIdx <= MAXWORDLEN; thisIdx++ )
            {
                  if ( lengthArray[thisIdx] >= thisVal )
                  {
                        printf ( " * " );
                  }
                  else
                  {
                        printf ( "   " );
                  }
            }
                  printf ( "\n" );
      }
       
      printf ( "      |_" );
      for ( thisIdx = 0; thisIdx <= MAXWORDLEN; thisIdx++)
      {
            printf ( "___" );
      }
      printf ( "\n      " );
      for ( thisIdx = 0; thisIdx < MAXWORDLEN; thisIdx++ )
      {
            printf ( "%3d", thisIdx + 1 );
      }
      printf ( "  >\n" );
      for ( thisIdx = 0; thisIdx < MAXWORDLEN + 2; thisIdx++ )
      {
            printf ( "   " );
      }
      printf ( "  %2d\n", MAXWORDLEN );
}


运行后的测试:







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值