c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图

简介:

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. 
统计输入中单词的长度,并且绘制相应的直方图。水平的直方图比较容易绘制,垂直的直方图较困难一些

 

 

复制代码
/* This program was the subject of a thread in comp.lang.c, because of the way it handled EOF.
 * The complaint was that, in the event of a text file's last line not ending with a newline,
 * this program would not count the last word. I objected somewhat to this complaint, on the
 * grounds that "if it hasn't got a newline at the end of each line, it isn't a text file".
 *
 * These grounds turned out to be incorrect. Whether such a file is a text file turns out to
 * be implementation-defined. I'd had a go at checking my facts, and had - as it turns out -
 * checked the wrong facts! (sigh)
 *
 * It cost me an extra variable. It turned out that the least disturbing way to modify the
 * program (I always look for the least disturbing way) was to replace the traditional
 * while((c = getchar()) != EOF) with an EOF test actually inside the loop body. This meant
 * adding an extra variable, but is undoubtedly worth the cost, because it means the program
 * can now handle other people's text files as well as my own. As Ben Pfaff said at the
 * time, "Be liberal in what you accept, strict in what you produce". Sound advice.
 *
 * The new version has, of course, been tested, and does now accept text files not ending in
 * newlines.
 *
 * I have, of course, regenerated the sample output from this program. Actually, there's no
 * "of course" about it - I nearly forgot.
 */

#include <stdio.h>

#define MAXWORDLEN 10

int main(void)
{
  int c;
  int inspace = 0;
  long lengtharr[MAXWORDLEN + 1];
  int wordlen = 0;

  int firstletter = 1;
  long thisval = 0;  //这个变量对于绘制垂直直方图,很有作用。
  long maxval = 0;
  int thisidx = 0;
  int done = 0;

for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++) { lengtharr[thisidx] = 0; }

//为什么不利用 while((c = getchar()) != EOF) 进行接收判断一起进行?这可以处理那些不是以新行结尾的文件。
while(done == 0) { c = getchar();
//空格、换行、制表、EOF代表单词的结束,可以根据上一个单词的wordlen做统计了,并存放到lengtharr[wordlen - 1]  
if(c == ' ' || c == '\t' || c == '\n' || c == EOF) { if(inspace == 0) { firstletter = 0; inspace = 1; if(wordlen <= MAXWORDLEN) { if(wordlen > 0) { thisval = ++lengtharr[wordlen - 1]; if(thisval > maxval) { maxval = thisval; } } } else { thisval = ++lengtharr[MAXWORDLEN]; if(thisval > maxval) { maxval = thisval; } } } if(c == EOF) { done = 1; } } else //统计当前单词的长度 { if(inspace == 1 || firstletter == 1) { wordlen = 0; firstletter = 0; inspace = 0; } ++wordlen; } }
//绘制垂直直方图,这个算法是可以借鉴的。不利用数组存储,而是根据指标变量thisval,也就是当前最大的直方图峰值来绘图。
for(thisval = maxval; thisval > 0; thisval--) { printf("%4d | ", thisval); for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++) { if(lengtharr[thisidx] >= thisval) { printf("* "); } else { printf(" "); } } printf("\n"); }
printf(
" +"); for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++) { printf("---"); } printf("\n "); for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++) { printf("%2d ", thisidx + 1); } printf(">%d\n", MAXWORDLEN); return 0; }
复制代码

 



样本输出


Here's the output of the program when given its own source as input:

 113  | *                                
 112  | *                                
 111  | *                                
 110  | *                                
 109  | *                                
 108  | *                                
 107  | *                                
 106  | *                                
 105  | *                                
 104  | *                                
 103  | *                                
 102  | *                                
 101  | *                                
 100  | *                                
  99  | *                                
  98  | *                                
  97  | *                                
  96  | *                                
  95  | *                                
  94  | *  *                             
  93  | *  *                             
  92  | *  *                             
  91  | *  *                             
  90  | *  *                             
  89  | *  *                             
  88  | *  *                             
  87  | *  *                             
  86  | *  *                             
  85  | *  *                             
  84  | *  *                             
  83  | *  *                             
  82  | *  *                             
  81  | *  *                             
  80  | *  *                             
  79  | *  *                             
  78  | *  *                             
  77  | *  *                             
  76  | *  *                             
  75  | *  *                             
  74  | *  *                             
  73  | *  *                             
  72  | *  *                             
  71  | *  *                             
  70  | *  *                             
  69  | *  *                             
  68  | *  *                             
  67  | *  *                             
  66  | *  *                             
  65  | *  *                             
  64  | *  *                             
  63  | *  *  *                          
  62  | *  *  *                          
  61  | *  *  *                          
  60  | *  *  *                          
  59  | *  *  *                          
  58  | *  *  *                          
  57  | *  *  *                          
  56  | *  *  *                          
  55  | *  *  *                          
  54  | *  *  *                          
  53  | *  *  *                          
  52  | *  *  *  *                       
  51  | *  *  *  *                       
  50  | *  *  *  *                       
  49  | *  *  *  *                       
  48  | *  *  *  *                       
  47  | *  *  *  *                       
  46  | *  *  *  *                       
  45  | *  *  *  *                       
  44  | *  *  *  *                       
  43  | *  *  *  *        *              
  42  | *  *  *  *        *              
  41  | *  *  *  *        *              
  40  | *  *  *  *        *              
  39  | *  *  *  *        *              
  38  | *  *  *  *        *              
  37  | *  *  *  *        *              
  36  | *  *  *  *        *              
  35  | *  *  *  *        *              
  34  | *  *  *  *        *              
  33  | *  *  *  *        *              
  32  | *  *  *  *        *              
  31  | *  *  *  *        *              
  30  | *  *  *  *        *           *  
  29  | *  *  *  *        *           *  
  28  | *  *  *  *  *     *           *  
  27  | *  *  *  *  *     *           *  
  26  | *  *  *  *  *     *           *  
  25  | *  *  *  *  *  *  *           *  
  24  | *  *  *  *  *  *  *           *  
  23  | *  *  *  *  *  *  *           *  
  22  | *  *  *  *  *  *  *        *  *  
  21  | *  *  *  *  *  *  *        *  *  
  20  | *  *  *  *  *  *  *        *  *  
  19  | *  *  *  *  *  *  *        *  *  
  18  | *  *  *  *  *  *  *        *  *  
  17  | *  *  *  *  *  *  *        *  *  
  16  | *  *  *  *  *  *  *        *  *  
  15  | *  *  *  *  *  *  *        *  *  
  14  | *  *  *  *  *  *  *  *     *  *  
  13  | *  *  *  *  *  *  *  *     *  *  
  12  | *  *  *  *  *  *  *  *     *  *  
  11  | *  *  *  *  *  *  *  *     *  *  
  10  | *  *  *  *  *  *  *  *     *  *  
   9  | *  *  *  *  *  *  *  *  *  *  *  
   8  | *  *  *  *  *  *  *  *  *  *  *  
   7  | *  *  *  *  *  *  *  *  *  *  *  
   6  | *  *  *  *  *  *  *  *  *  *  *  
   5  | *  *  *  *  *  *  *  *  *  *  *  
   4  | *  *  *  *  *  *  *  *  *  *  *  
   3  | *  *  *  *  *  *  *  *  *  *  *  
   2  | *  *  *  *  *  *  *  *  *  *  *  
   1  | *  *  *  *  *  *  *  *  *  *  *  
      +---------------------------------
        1  2  3  4  5  6  7  8  9 10 >10
本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/3647930.html,如需转载请自行联系原作者
相关文章
|
2月前
|
机器学习/深度学习 编译器 C语言
【C语言】数据输出的域宽控制(如何在输出数据时控制0占位)(如何输出前导0)(保留几位小数)(乘法口诀表打印不齐)等问题
【C语言】数据输出的域宽控制(如何在输出数据时控制0占位)(如何输出前导0)(保留几位小数)(乘法口诀表打印不齐)等问题
28 0
|
Python
Python基础(输出五行五角星,数量每行递增/输出九九乘法表)
需求:在控制台连续输出五行*, 每一行星星的数量依次递增 思路:使用while循环输出五行内容, 依次输出数字1到5, 再使用数字乘以字符串'*', 即可在每行输出一个星星, 两个星星, ... 五个星星, 从而实现递增
654 1
Python基础(输出五行五角星,数量每行递增/输出九九乘法表)
|
4月前
|
算法 Python Java
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
42 0
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
|
10月前
学C的第十三天【应用多文件的形式实现 三子棋 程序(重点);练习:1. 打印9*9乘法口诀表、2. 求10个整数中的最大值、3. 分数加减交叉计算、4. 数一下 1到 100 的整数中出现了多少个9】
9.数组的应用实例1:三子棋(综合以前学习的知识) 三子棋的实现:(重点都在注释中) 1. 游戏不退出,继续玩下一把(循环) 2. 应用多文件的形式写代码
|
算法
算法练习题(六)——Z字型打印矩阵
算法练习题(六)——Z字型打印矩阵
82 0
|
存储 算法
leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
时间复杂度:O(q * (m + n) + m * n) 其中q表示 indices 数组的长度,m、n为矩阵的行数和列数,遍历 indices 数组都要更新一次行列,总共需要O(q * (m + n))的时间,最后遍历一次矩阵,总共需要O(m * n)的时间
50 0
leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
|
Python
编写程序,输入若干整数(整数之间以逗号间隔),统计每个整数的出现次数。
编写程序,输入若干整数(整数之间以逗号间隔),统计每个整数的出现次数。
188 0
编写程序,输入若干整数(整数之间以逗号间隔),统计每个整数的出现次数。