文件中单词词频统计
- 用散列表进行快速地查找和插入
int main()
{
int TableSize = 10000; //散列表的估计大小
int wordcount = 0, length;
HashTable H;
ElementType word;
FILE *fp;
char document[30] = "HarryPotter.txt";//要统计词频的文件名
H = Initialize Table( TableSize );//建立散列表
if(fp = fopen(docoument,"r")==NULL) FatalError("无法打开文件!\n");
while(!feof(fp)){
length = GetAWord(fp, word);//从文件中读取一个单词
if(length>3) {//只考虑适当长度的单词
wordcount++;//统计文件中单词总数
InsertAndCount(word,H);
}
}
fclose(fp);
printf("该文档共出现了%d个有效单词",wordcount);
Show(H, 10.0/100);//显示词频
DestoryTable(H); //销毁散列表
return 0;
}