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 = ;
long lengtharr[MAXWORDLEN + ];
int wordlen = ; int firstletter = ;
long thisval = ; //这个变量对于绘制垂直直方图,很有作用。
long maxval = ;
int thisidx = ;
int done = ;
for(thisidx = ; thisidx <= MAXWORDLEN; thisidx++)
{
lengtharr[thisidx] = ;
} //为什么不利用 while((c = getchar()) != EOF) 进行接收判断一起进行?这可以处理那些不是以新行结尾的文件。
while(done == )
{
c = getchar();
//空格、换行、制表、EOF代表单词的结束,可以根据上一个单词的wordlen做统计了,并存放到lengtharr[wordlen - 1]
if(c == ' ' || c == '\t' || c == '\n' || c == EOF)
{
if(inspace == )
{
firstletter = ;
inspace = ; if(wordlen <= MAXWORDLEN)
{
if(wordlen > )
{
thisval = ++lengtharr[wordlen - ];
if(thisval > maxval)
{
maxval = thisval;
}
}
}
else
{
thisval = ++lengtharr[MAXWORDLEN];
if(thisval > maxval)
{
maxval = thisval;
}
}
}
if(c == EOF)
{
done = ;
}
}
else //统计当前单词的长度
{
if(inspace == || firstletter == )
{
wordlen = ;
firstletter = ;
inspace = ;
}
++wordlen;
}
}
//绘制垂直直方图,这个算法是可以借鉴的。不利用数组存储,而是根据指标变量thisval,也就是当前最大的直方图峰值来绘图。
for(thisval = maxval; thisval > ; thisval--)
{
printf("%4d | ", thisval);
for(thisidx = ; thisidx <= MAXWORDLEN; thisidx++)
{
if(lengtharr[thisidx] >= thisval)
{
printf("* ");
}
else
{
printf(" ");
}
}
printf("\n");
}
printf(" +");
for(thisidx = ; thisidx <= MAXWORDLEN; thisidx++)
{
printf("---");
}
printf("\n ");
for(thisidx = ; thisidx < MAXWORDLEN; thisidx++)
{
printf("%2d ", thisidx + );
}
printf(">%d\n", MAXWORDLEN); return ;
}
样本输出
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
c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图的更多相关文章
- c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...
- c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...
- c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...
- c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()
The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...
- c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。
Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...
- php实现 统计输入中各种字符的个数
php实现 统计输入中各种字符的个数 一.总结 一句话总结:谋而后动,想清楚,会非常节约编写代码的时间. 1.对结果可能是0的变量,记得初始化? 4 $len=0; 5 $len=strlen($st ...
随机推荐
- QT5新手上路(2)发布exe文件
QT编程教程在网上有很多,但写完代码以后如何打包成可执行exe文件却少有提及,本文主要介绍这一部分:1.首先确认自己建的工程在debug模式下运行无误.2.在release模式下运行一遍.(如何更改成 ...
- iOS 详细解释@property和@synthesize关键字
/** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...
- 【转载】C#后台声明式验证,远离if验证
ViewModel public class ViewModel { [Required(ErrorMessage="标题不能为空")] public string Title { ...
- zoj 3471 Most Powerful
题目链接:zoj 3471 Most Powerful 作者:jostree 转载请说明出处 很经典的状态dp,使用i的二进制位表示粒子的状态,0表示存在,1表示不存在.dp[i]表示在状态i的情况 ...
- SSD论文优秀句子
1. Nonvolatile memory(e.g., Phase Change Memory) blurs the boundary between memory and storage and i ...
- 排序算法ONE:选择排序SelectSort
/** *选择排序: * 对冒泡排序的一个改进 * 进行一趟排序时,不用每一次都交换,只需要把最大的标示记下 * 然后再进行一次交换 */ public class SelectSort { /** ...
- 盗链网易163、腾讯QQ、新浪sina、百度Baidu的图片之PHP独立版
网易相册频道,网易相册确实是一个高质量图片的地方,而且免费,唯一缺点是很多加了水印,但这个不重要了,无意间发现网易163相册频道的图片是防止盗链的,于是便自己写了一个小程序来突破这个. 本盗链图片最新 ...
- nuget的使用总结
使用NuGet发布自己的类库包(Library Package) from:http://blog.csdn.net/gulijiang2008/article/details/41724927 使用 ...
- sencha architect/sencha touch , to prevent breakpoint lost when you debug
add this to your loader config: Ext.Loader.setConfig({ disableCaching: false }); or : click 'applica ...
- js Touch事件(向左滑动,后退)
js Touch事件(向左滑动,后退) 代码如下 var touch_p = { c_x : 0, c_y : 0, hasbacked : false }; function touches(ev) ...