1-13 编写一个程序,打印输入中单词长度的直方图。水平方向的直方图比较容易绘制,垂直方向的直方图则要困难些。
这个题是我第一次码超过百行的代码,码了一天。如有不当之处,请批评指正。
思路是首先给每个长度的单词赋个数值,作为数组。
其次,输出水平直方图。
最后,输出垂直直方图。
#include <stdio.h>
#define IN 1/*定义在在单词内的状态*/
#define OUT 0/*定义在单词外的状态*/
#define max 10
int main()
{
int c, state, wordlength;
state = OUT;
wordlength = 0;/*记录每个单词的长度*/
int number[10]{ 0 };/*长度不超过10的单词个数*/
int bignumber = 0;/*长度超过10的单词个数*/
while ((c = getchar()) != EOF) {
if (c != ' ' && c != '\n' && c != '\t') {
state = IN;
++wordlength;
}
else if (state == IN) {/*单词读取结束*/
state = OUT;
if (wordlength > max) /*单词长度大于10*/
++bignumber;
else {
++number[wordlength - 1];
}
wordlength = 0;/*单词读取结束,长度赋0*/
}
}
int i, j;
printf("\n\t\t Horizontal histogram\