高手们帮我解决一道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 展开
的单词出现的频率、最长的单词长度和出现频率最多的数目,并按如下形式显示。假定各单词间以空格分隔,单词最长不超过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 展开
展开全部
闲来无事,写一段吧:
//假定,空格为唯一分隔符,回车表示完成输入
//以一个数组countArray来记录wordcount,用它的索引来表示wordlength
//wordlen = index + 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORD_LENGTH 30
unsigned int countArray[MAX_WORD_LENGTH];
void Initialize();
void OutPut();
void main(void)
{
char input;
unsigned int currentLength = 0;
Initialize();
printf("Please enter your words:\n");
for(input = getchar();;input = getchar())
{
if(input != ' ' && input != '\n')
currentLength++;
else
{
if(currentLength == 0);
else if(currentLength > MAX_WORD_LENGTH)
{
printf("Input string error, length of word exceeds length limit!\n");
exit(0);
}
else//0 < length <= MAX_WORD_LENGTH
{
countArray[currentLength - 1]++;
currentLength = 0;
}
if(input == '\n')break;
}
}
OutPut();
}
void Initialize()
{
int i;
for(i = 0;i < MAX_WORD_LENGTH;i++)
countArray[i] = 0;
}
void OutPut()
{
unsigned int i,maxLen = 0,maxCount = 0;
printf("WordLen\tWordCount\n");
for(i = 0;i < MAX_WORD_LENGTH;i++)
{
if(countArray[i] != 0)
{
printf("%d\t%d\n",i + 1,countArray[i]);
maxLen = i + 1;
if(maxCount < countArray[i]) maxCount = countArray[i];
}
}
printf("MaxLen = %d, MaxCount = %d\n",maxLen,maxCount);
}
//假定,空格为唯一分隔符,回车表示完成输入
//以一个数组countArray来记录wordcount,用它的索引来表示wordlength
//wordlen = index + 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORD_LENGTH 30
unsigned int countArray[MAX_WORD_LENGTH];
void Initialize();
void OutPut();
void main(void)
{
char input;
unsigned int currentLength = 0;
Initialize();
printf("Please enter your words:\n");
for(input = getchar();;input = getchar())
{
if(input != ' ' && input != '\n')
currentLength++;
else
{
if(currentLength == 0);
else if(currentLength > MAX_WORD_LENGTH)
{
printf("Input string error, length of word exceeds length limit!\n");
exit(0);
}
else//0 < length <= MAX_WORD_LENGTH
{
countArray[currentLength - 1]++;
currentLength = 0;
}
if(input == '\n')break;
}
}
OutPut();
}
void Initialize()
{
int i;
for(i = 0;i < MAX_WORD_LENGTH;i++)
countArray[i] = 0;
}
void OutPut()
{
unsigned int i,maxLen = 0,maxCount = 0;
printf("WordLen\tWordCount\n");
for(i = 0;i < MAX_WORD_LENGTH;i++)
{
if(countArray[i] != 0)
{
printf("%d\t%d\n",i + 1,countArray[i]);
maxLen = i + 1;
if(maxCount < countArray[i]) maxCount = countArray[i];
}
}
printf("MaxLen = %d, MaxCount = %d\n",maxLen,maxCount);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询