一、题目
读入一篇英文短文,去除介词、连词、冠词、副词、代词等非关键性单词后,统计每个单词出现的次数,并将单词按出现次数的降序和单词字符的升序进行显示5个单词。
二、算法
1. 通过库创建fstream流对象,并从文件中读取全部字符存入内存。
2. 将内存的字符串进行空格、标点符号、换行符的分割,若分割后的单词属于需要记入统计的单词,则将其存入map容器中,通过map进行词频统计。
3. 对map中的词频统计结果写入vector中进行排序,并按照规定的排序顺序进行打印。
三、代码
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
四、测试
测试文档test1.txt
No one can help others as much as you do.
No one can express himself like you.
No one can express what you want to convey.
No one can comfort others in your own way.
No one can be as understanding as you are.
No one can feel happy, carefree, and no one can smile as much as you do.
In a word, no one can show your features to anyone else.
hi, how are you? I love you!
运行结果