/* 打印输入中单词(任何其中不包括空格,制表符或换行符的字符序列)长度的直方图 */
#include<stdio.h>
#define OUT 0 //在单词外
#define IN 1 //在单词内
static void print(char z[]);
/* 打印输入中单词(任何其中不包括空格,制表符或换行符的字符序列)长度的直方图 */
int main(void)
{
char s[128] = { 0 };//s按行存储
char zft[11] = { 0 };//直方图:下标代表一个单词数目,内容代表这种单词有几个
int i = 0, c;
freopen("C:\\\\Users\\wwwzh\\Desktop\\data.in", "r", stdin);
freopen("C:\\\\Users\\wwwzh\\Desktop\\data.out", "w", stdout);
while ((c = getchar()) != EOF)
{
int state = OUT; //state标识字符所处状态
int count = 0; //count单词数目计数
s[i] = c;
if (s[i] == '\n')
{
s[i + 1] = '\0';
i = 0;
while (s[i] != '\0')
{
//不在单词内state = OUT
if (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
{
if (state == IN)//由第一次出单词
{
if (count <= 10)
zft[count]++;
}
state = OUT;
}
else if (state == OUT)
{//由外进入单词第一个
count = 1;
state = IN;
}
else//单词里面计数加
count++;
i++;
}
i = -1;
}
i++;
}
print(zft);
return 0;
}
static void print(char z[])
{
int max, i;
max = z[0];
for (i = 1; i < 11; i++)
if (max < z[i])
max = z[i];
printf("单词数目\n");
for (i = 1; i <= max; max--)
{
int j;
printf("%4d|", max);
for (j = 1; j <= 10; j++)
{
if (z[j] >= max)
printf(" # ");
else
printf(" ");
}
putchar('\n');
}
printf("%4d|", 0);
for (i = 1; i <= 10; i++)
printf(" %d ", i);
printf("单词长度\n");
}
/*
Enjoy that uniquenesss1
You do not have to pretend in order to seem more like someone else
As food is to the body so is learning to the mind
Our bodies grow and muscles develop with the intake of adequate nutritious food
*/


405

被折叠的 条评论
为什么被折叠?



