紧急求助!关于文本文件和map的题(追加分!!)

来源:百度知道 编辑:UC知道 时间:2024/06/01 21:14:08
Write a program to read a text file. Store each word in a map.
The key value of the map is the count of the number of times the word appears in the text.
Display the list of words and the relevant count.
And display the top 5 element with maximize count.

如果答对了追加200分。。
我要的是程序不是翻译啊。。谢谢
dingpwen - 江湖少侠 六级 您的程序运行有错误啊,在一半终止了。
冒险岛乐乐 - 江湖大侠 您好!我们要求用C++在vs2005下运行,但是我觉得你写的c不错试了一下,出现了flag没有初始化的错误,鉴于上一个答案也有错误,但是没改正,如果你改对了我就把分给你,虽然我要的是C++程序,但是您做的也很好。

渴望能拿200分。改了!
已经基本搞定,程序计算方法已经过优化,速度较快,当文件较大单词数较多时,请用Dev-c++等32位的编译器运行。如有什么疑问,给我消息。
本题的一个完整的c程序如下,程序在win-tc和Dev-c++下都调试通过。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000 /* 设文件不超过1000词数,可更改 */

int main(void)
{FILE *fp1,*fp2;
char ch,s[N][21];/* 设每个单词不超过20个字符长度,可更改 */
int i=0,j=0,k,max,flag=0,t[N],tt[N];/*flag=0*/
system("cls");
if((fp1=fopen("001.txt","r"))==NULL)/* 设要统计的文件名为001.txt */
{ printf("Cannot open this file!\n");
system("pause");
exit(0);
}
fp2=fopen("002.txt","w");/* 设结果写入文件002.txt */
ch=fgetc(fp1);/* 读文件 */
while(ch!=EOF) /* 当文件未结束时循环 */
{
if(ch==' '||ch=='\t'||ch=='.'||ch==','||ch=='?'||ch=='!'
||ch==':'||ch==';'||ch=='-'||ch=='\n') /*