logo资料库

C语言实现统计文章的单词数目(实验报告+源代码).doc

第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
资料共7页,全文预览结束
一、实验名称:c 语言综合程序设计试验 二、实验学时:4 三、实验内容和目的: (1)实验内容 编写一个程序。功能是读入一篇英文文章,统计该文章中每个单词的出现次数, 并输出出现次数最多的前 5 个单词及其出现次数。 注意: 1 单词不区分大小写,比如:The 和 the 是相同的单词。 ② 按照英文书写习惯,过长的单词遇到换行时,会加入连字符“-”。比如: “international”遇到换行时可能会写为“intern-ational”,统计时要注意除去连字 符。 (2)实验目的 ①熟练掌握 C 语言文件操作。 ②培养综合编程能力。 四、实验原理: 计算单词个数以及输出信息 把单词变成小写格式 交换元素、排序 #include #include #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[] ="How to Study English Well? We all know that Eng-lish is very useful. Many people in the world speak English. So more and more pe-ople in China study it. How to study English well? I think we must have a good way to study English. If you want to learn English well, listening, speaking, re-ading and writing are
important. You should listen to tapes every day. You should often speak English with your teachers and friends. You should read English every morning. And, you had better keep a diary every day. In this way, you can study English well."; printf("The text is :\n"); printf("----------------------------------\n"); printf("%s\n", pText); printf("----------------------------------\n"); printf("The top 5 words is :\n"); CalcEachWord(pText); return 0; } int CalcEachWord(const char *pText) {char cTmp[20] = {0}; int = 0; char *pTmp int 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; = cTmp; i iFlag = 0; 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; } 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]); } } } } 五、实验器材(设备、元器件) pc 微机一台 六、实验步骤: 1.编写源代码 2.编译,生成.obj 文件 3. 链接,生成可执行的.exe 可执行程序 4.对程序进行调试和修改 七、实验数据及结果分析: #include #include #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[] ="How to Study English Well? We all know that Eng-lish is very useful. Many
people in the world speak English. So more and more pe-ople in China study it. How to study English well? I think we must have a good way to study English. If you want to learn English well, listening, speaking, re-ading and writing are important. You should listen to tapes every day. You should often speak English with your teachers and friends. You should read English every morning. And, you had better keep a diary every day. In this way, you can study English well."; printf("The text is :\n"); printf("----------------------------------\n"); printf("%s\n", pText); printf("----------------------------------\n"); printf("The top 5 words is :\n"); CalcEachWord(pText); return 0; } int CalcEachWord(const char *pText) {char cTmp[20] = {0}; int = 0; char *pTmp int 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; = cTmp; i iFlag = 0; 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; } 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]); } } } }
分享到:
收藏