您的位置:首页 > 其它

统计一篇英文文章中出现次数最多的前五个单词

2013-03-05 10:22 826 查看
#include <stdio.h>
#include <string.h>

//统计单词最大个数
#define MAX_WORD_COUNT 500

//保存单词及其出现的个数
typedef struct WordCount
{
char cWord[20];
int  iCount;
}T_WordCount;

//统计单词个数并输入排名前五的单词
int CalcEachWord(const char *pText);
//将单词转换为小写形式
void LowerText(char *pText);
//交换两个单词对象
void SwapItem(T_WordCount *ItemA, T_WordCount * ItemB);
//对保存单词出现个数的数组进行排序
void SortWord(T_WordCount *pWordSet);

//主函数
int main(int argc, char *argv[])
{

//测试数据,单词中间可能会用-隔开
char pText[] ="Text HAs HAS ONE h-as MOrE Has MORE ha-S BLANk more blank or more oR blank  Between   wor-ds.";
printf("The text is :\n");
printf("----------------------------------\n");
printf("%s\n", cBuf);
printf("----------------------------------\n");
printf("The top 5 words is :\n");

CalcEachWord(cBuf);
return 0;
}

int CalcEachWord(const char *pText)
{
char cTmp[20] = {0};
int  i		 = 0;
char *pTmp   = cTmp;
int  iFlag   = 0;

T_WordCount tWordSet[MAX_WORD_COUNT];
memset(tWordSet, 0, sizeof(tWordSet));

while (*pText != '\0')
{
if ((*pText >= 'A' && *pText <= 'Z') || (*pText >= 'a' && *pText <= 'z'))
{

*pTmp = *pText;
pTmp++;

}
else if (*pText == '-')
{
++pText;
continue;
}
else
{

if (strlen(cTmp) > 0)
{
LowerText(cTmp);
iFlag = 0;
for (i = 0; i < MAX_WORD_COUNT; ++i)
{
if (strlen(tWordSet[i].cWord) > 0)
{
if (strcmp(tWordSet[i].cWord, cTmp) == 0)
{
iFlag = 1;
tWordSet[i].iCount++;
break;
}
}
else
{
strcpy(tWordSet[i].cWord, cTmp);
tWordSet[i].iCount = 1;
iFlag = 1;
break;
}

}

if (!iFlag)
{
printf("No more space to save word.\n");
}

}
memset(cTmp, 0, 20);
pTmp = cTmp;
}

++pText;
}

SortWord(tWordSet);
for (i = 0; i < 5; ++i)
{
if (strlen(tWordSet[i].cWord) > 0)
{
printf("%s:%d\n",tWordSet[i].cWord,tWordSet[i].iCount);
}
}

return 0;
}

void LowerText(char *pText)
{
char *pTmp = pText;
while (*pTmp != '\0')
{
if ((*pTmp >= 'A' && *pTmp <= 'Z'))
{
*pTmp += 32 ;
}

pTmp++;
}
}

void SwapItem(T_WordCount *ItemA, T_WordCount * ItemB)
{
T_WordCount Tmp;
memset(&Tmp, 0, sizeof(T_WordCount));
strcpy(Tmp.cWord, ItemA->cWord);
Tmp.iCount = ItemA->iCount;

strcpy(ItemA->cWord, ItemB->cWord);
ItemA->iCount = ItemB->iCount;

strcpy(ItemB->cWord, Tmp.cWord);
ItemB->iCount = Tmp.iCount;
}

void SortWord(T_WordCount *pWordSet)
{
int i,j;
for (j = 0; j < MAX_WORD_COUNT - 1; j++)
{
for (i = 0; i < MAX_WORD_COUNT - 1 - j; i++)
{
if (pWordSet[i].iCount < pWordSet[i+1].iCount)
{
SwapItem(&pWordSet[i], &pWordSet[i+1]);
}
}
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐