回复 楼主 爱睡觉的猫
以下代码在Dev C++上编译运行通过。学习过程中,欢迎指教!
具体代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define
BUFLEN
81
#define
MORELEN
20
char delimiters[] = " \".,;;!?)(\n";
bool Seek_word(char *more, char *filename);
unsigned int Word_count(char *filename);
char *Most_words(char *filename, unsigned int count, char *data);
char *Word(char more[][MORELEN], unsigned int count, char *data);
int main(void)
{
char buf[BUFLEN] = "\0";
char more[MORELEN] = "\0";
char data[MORELEN] = "\0";
char filename[FILENAME_MAX] = "myfile.txt";
FILE *pfile = NULL;
unsigned int count = 0;
printf("该程序能将英文文章存储在myfile.txt文件中!\n");
printf("能在存储的文件中查找给定的单词是否存在!\n");
printf("也能够统计存储的文件中英文单词的数量和出现频率最高的单词!\n");
printf("注意:该程序统计单词时,是区分大小的。如:“And”与“and”是两个单词!\n");
if((pfile = fopen(filename, "w")) == NULL)
{
printf("打开 %s 文件失败!\n\a\a\a", filename);
exit(1);
}
setvbuf(pfile, NULL, _IOFBF, BUFSIZ);
printf("\n现在请从键盘输入要存储在myfile.txt文件中的英文文章:\n");
while(true)
{
fgets(buf, BUFLEN, stdin);
if(buf[0] == '\n')
break;
if(EOF == fputs(buf, pfile))
{
printf("写入 %s 文件错误!\n\a\a\a", filename);
exit(1);
}
}
fclose(pfile);
pfile = NULL;
printf("\n请输入要查找的单词: ");
scanf(" %s", more);
if(Seek_word(more, filename))
printf("\n%s有在文章中!", more);
else
printf("\n%s没有在文章中!", more);
printf("\n该文件中英文单词的数量为:%u!\n", Word_count(filename));
count = Word_count(filename);
printf("\n该文件中出现频率最高的单词为:%s!\n", Most_words(filename, count, data));
return 0;
}
bool Seek_word(char *more, char *filename)
{
FILE *pfile = NULL;
char buf[BUFLEN] = "\0";
if(!(pfile = fopen(filename, "r")))
{
printf("打开 %s 文件失败!\n\a\a\a", filename);
exit(1);
}
setvbuf(pfile, NULL, _IOFBF, BUFSIZ);
while(true)
{
if(!fgets(buf, BUFLEN, pfile))
break;
char *pWord = strtok(buf, delimiters);
if(pWord)
{
do
{
if(strcmp(pWord, more) == 0)
{
fclose(pfile);
pfile = NULL;
return true;
}
pWord = strtok(NULL, delimiters);
}
while(pWord);
}
}
fclose(pfile);
pfile = NULL;
return false;
}
unsigned int Word_count(char *filename)
{
FILE *pfile = NULL;
char buf[BUFLEN] = "\0";
unsigned int count = 0;
if(!(pfile = fopen(filename, "r")))
{
printf("打开 %s 文件失败!\n\a\a\a", filename);
exit(1);
}
setvbuf(pfile, NULL, _IOFBF, BUFSIZ);
while(true)
{
if(!fgets(buf, BUFLEN, pfile))
break;
char *pWord = strtok(buf, delimiters);
if(pWord)
{
do
{
++count;
pWord = strtok(NULL, delimiters);
}
while(pWord);
}
}
fclose(pfile);
pfile = NULL;
return count;
}
char *Most_words(char *filename, unsigned int count, char *data)
{
FILE *pfile = NULL;
char buf[BUFLEN] = "\0";
char more[count][MORELEN];
unsigned int count1 = 0;
if(!(pfile = fopen(filename, "r")))
{
printf("打开 %s 文件失败!\n\a\a\a", filename);
exit(1);
}
setvbuf(pfile, NULL, _IOFBF, BUFSIZ);
while(true)
{
if(!fgets(buf, BUFLEN, pfile))
break;
char *pWord = strtok(buf, delimiters);
if(pWord)
{
do
{
strcpy(more[count1++], pWord);
pWord = strtok(NULL, delimiters);
}
while(pWord);
}
}
fclose(pfile);
pfile = NULL;
strcpy(data, Word(more, count, data));
return data;
}
char *Word(char more[][MORELEN], unsigned int count, char *data)
{
int i = 0, j = 0;
int max = 0;
for(i = 0; i < count; ++i)
{
int n = 0;
for(j = 0; j < count; ++j)
{
if((strcmp(more[i], more[j])) == 0)
++n;
}
if(n > max)
{
strcpy(data, more[i]);
max = n;
}
}
return data;
}