帮解一道统计类的题目.

来源:百度知道 编辑:UC知道 时间:2024/06/20 16:16:34
本人正在学习C语言......
现遇到一统计问题拘手无策,望得到高人指点~~~
题目是这样的:
编写一完整程序,对命令行输入的文本文件,统计该文本文件中不同长度的单词出现的频率、最长的单词长度和出现频率最多的数目,并按如下形式显示。假定各单词间以空格分隔,单词最长不超过30个字符。
例如,文本文件中的内容为:
dfg ghjk hjkll yu rty yuio
asdfghjk opio we
运行后屏幕显示如下:
WordLen WordCount
2 2
3 2
4 3
5 1
8 1
11 1
maxwordLen=11,most Wordcount=3

想了两天还是一团糟......

请写出完整的程序.感激不尽!!!
请写出完整的程序.感激不尽!!!
请写出完整的程序.感激不尽!!!
米若明,你也让让人家嘛.别太......

Leo239,谢谢你.但是你是好像太复杂了吧......

#include "stdio.h"
#include "iostream.h"
#include "fstream.h"
#include "string.h"

void main()
{
ifstream in("文件名.txt");
if(in.fail())
{
in.close();
in.open("文件名.txt"); //单词存放在文件名.txt文件中,改文本放在debug同一目录下
}
char s[31];
int word[31],maxwordLen=0,mostWordCount=0; //word[i]存放长度为i的单词个数
memset(word,0,sizeof(word)); //初始化单词个数为0

while(in>>s) //s存放读取的文本单词,一个一个单词读取
{
int len=strlen(s); //单词长度
word[len]++; //len长度单词个数累加
}
printf("WordLen WordCount\n");
for(int i=1;i<31;i++)
{
if(word[i]!=0)
{
printf(" %d %d\n",i,word[i]);
maxwordLen=i; //按单词长度找,故个数不为0则不断记录为maxwordLen
if(mostWordCount<word[i]) //判断出现次数最多的单词,并记录
mostWordCount=word[i];
}
}
printf("maxwordLen=%d,mostWordCount=%d\n",m