网站源码之家,深圳网站优化网站,怎么创建网站 优帮云,教师专用ppt模板免费下载一、题目
读入一篇英文短文#xff0c;去除介词、连词、冠词、副词、代词等非关键性单词后#xff0c;统计每个单词出现的次数#xff0c;并将单词按出现次数的降序和单词字符的升序进行显示5个单词。 二、算法
1. 通过fstream库创建fstream流对象#xff0c;并从…一、题目
读入一篇英文短文去除介词、连词、冠词、副词、代词等非关键性单词后统计每个单词出现的次数并将单词按出现次数的降序和单词字符的升序进行显示5个单词。 二、算法
1. 通过fstream库创建fstream流对象并从文件中读取全部字符存入内存。
2. 将内存的字符串进行空格、标点符号、换行符的分割若分割后的单词属于需要记入统计的单词则将其存入map容器中通过map进行词频统计。
3. 对map中的词频统计结果写入vector中进行排序并按照规定的排序顺序进行打印。 三、代码
#define _CRT_SECURE_NO_WARNINGS 1#include iostream
#include fstream
#include cstring
#include map
#include vector
#include algorithm
using namespace std;// 不计入统计的单词表介词、连词、冠词、副词、代词
vectorstring g_delWord {to, in, on, for, of, from, between, behind, by, about, at, with, than,a, an, the, this, that, there,and, but, or, so, yet,often, very, then, therefore,i, you, we, he, she, my, your, hes, her, our, us, it, they, them,am, is, are, was, were, be,when, where, who, what, how,will, would, can, could
};// 仿函数
struct Compare
{bool operator()(const pairstring, int e1, const pairstring, int e2){return e1.second e2.second;}
};int main()
{// 1. 读入文件数据// ofstream写文件// ifstream读文件// fstream读写文件fstream f;// ios::in// ios::out// ios::app追加写配合ios::out使用// ios::trunc覆盖写配合ios::out使用// ios::binary以二进制的形式f.open(./test1.txt, ios::in);if (!f.is_open()){cout file open failed! endl;return 1;}char text[4096] { 0 };f.read(text, 4096);// 2. 分割字符串存入mapmapstring, int wordMap;const char* cut ,.!?;:\n; // 部分单词分隔符char* w strtok(text, cut);while (w){string word w;// 单词转小写string lwrWord;transform(word.begin(), word.end(), back_inserter(lwrWord), ::tolower);// 排除不计入统计的单词if (find(g_delWord.begin(), g_delWord.end(), lwrWord) g_delWord.end()){wordMap[lwrWord];// map 的 “[]”重载有插入、查询、修改功能返回值为键值对的second值或false}w strtok(NULL, cut);}// 3. 词频排序vectorpairstring, int wordVec;for (auto e : wordMap){wordVec.push_back(e);}// sort是基于快速排序实现的算法是不稳定的排序算法可用stable_sort代替stable_sort(wordVec.begin(), wordVec.end(), Compare());for (int i 0; i 5; i){cout wordVec[i].first , wordVec[i].second endl;}return 0;
} 四、测试
测试文档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! 运行结果