计算一篇文章中单词出现的频率,并把输出频率最高的十五个单词输出来

 
#include<stdio.h>
#include"string.h"
#define WORDNUM 65535 // max word number
#define WORDLENGTH 15 // word max length
#define PRINTWORDNUM 15 //out put max word number
char differentword[WORDNUM][WORDLENGTH]={0};//different word info
int differentCount[WORDNUM];//equal word number
int iIndex = 0;//current file word total number
int iWordCount =0;//current file different word number
void GetWordInfo(FILE *fpRead);
void SetWordList(char word[WORDLENGTH]);
void OrderWordList();
void OutPutWordList();
void DealWithWord()
{
FILE *fpRead;
if((fpRead = fopen("D:\word.txt", "r")) == NULL)
{
   printf("Cannot read file %s\n", "D:\word.txt");
   return;
}
GetWordInfo(fpRead);
OrderWordList();
OutPutWordList();
}
void GetWordInfo(FILE *fpRead)
{
int jIndex = 0;
int i = 0;
char ch;
char word[WORDLENGTH]={0};
while((ch=fgetc(fpRead))!=EOF)
{
   //putchar(ch);
   if ((ch >= 65 && ch <=90) ||(ch >= 97 && ch <=122))
   {
    if (jIndex < WORDLENGTH)
    {
     word[jIndex] = ch;
     jIndex ++;
    }
   }
   else
   {
    if (jIndex != 0)
    {
     SetWordList(word);
     jIndex = 0;
     iIndex ++;
    }
   }
}
}
void SetWordList(char word[WORDLENGTH])
{
int i;
int iEqual = 0;
if (iIndex == 0)
{
   strcpy(differentword[0],word);
   differentCount[0] = 0;
   iWordCount ++;
}
for (i = 0; i < iWordCount; i ++)
{
   if (strcmp(differentword[i],word) == 0)
   {
    differentCount[i] ++;
    iEqual = 1;
   }
}
if (iEqual == 0)
{
   strcpy(differentword[iWordCount],word);
   differentCount[iWordCount] ++;
   iWordCount ++;
}
for (i = 0; i <WORDLENGTH; i ++)
{
   word[i] = '\0';
}
}
void OrderWordList()
{
int iCurrent = 0;
int i,j,tempValue;
char wordTemp[WORDLENGTH]={0};
for (i = 0; i < iWordCount; i ++)

   iCurrent = i;
   for (j = i + 1; j < iWordCount; j ++)
   {
    if (differentCount[iCurrent] < differentCount[j])
    {
     iCurrent = j;
    }
   } 
   if (iCurrent != i)
   {
    tempValue = differentCount[iCurrent];
    differentCount[iCurrent] = differentCount[i];
    differentCount[i] = tempValue;
    strcpy(wordTemp,differentword[iCurrent]);
    strcpy(differentword[iCurrent],differentword[i]);
    strcpy(differentword[i],wordTemp);
   }
}
}
void OutPutWordList()
{
int i;
for (i = 0; i < iWordCount && i < PRINTWORDNUM; i ++)
{
   printf("%s %d\n", differentword[i], differentCount[i]);
}
}
int main(int argc, char* argv[])
{ // 从英語文章中统计出单词出現の频率,打印出前15个出现频率高的单词
DealWithWord();
getchar();
}

 

图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值