P.4文本统计工具 功能读取指定文本文件统计字符数含/不含空格和标点符号、单词数、行数、高频词TOP10。二、训练重点ifstream大文件读取、string的遍历与处理、mapstring, int统计词频、对map值排序、STL算法count/replace过滤停用词is/a/an/the等等、支持多文件统计、输出统计结果到新文件。三、我的思路将任务分为两部分1读取文本2将单词出现频率从高到低排序3输出统计结果。最后的执行函数run也是按照这个顺序进行的。1)读取文本使用getline一行一行地读取读取一行字符串text后进行分析。先将这一行字符串所有大写字母转换为小写形式再遍历字符串if (text[idx] (空格 || 标点符号)) idx跳过该字符else text[i] 单词使用read_word函数读取这个单词如果读取到的单词在要过滤的名单中过滤名单见代码则不予理睬否则将该单词出现次数2)将单词出现频率从高到低排序这一步我直接使用STL中的sort了。3)输出统计结果共5n行n 高频词数量num 10 ? 10 : num前4行分别是字符数含空格和标点符号、字符数不含空格和标点符号、单词数、行数。第五行是High-frequency words(Top 10)接下来10行是高频词 Top 10若不足10个则按实际个数输出。四、注意事项代码需要在C 20及以上才能运行因为使用了ranges头文件范围库及相关操作读取的文本仅支持全英文文本不能包含任何中文中文统计功能本来是想加入的但是我能力不够读取的文本的每一行的末尾不能包含不完整的单词即不能有使用连字符将本行放不下的单词放在下一行的情况否则读取的单词不完整不能识别复合词代码中可能有未知Bug和可以优化的地方如果您发现了希望您能告知作者因为作者是在读大学生希望提高自己的能力。五、演示示例文章不知道什么原因一个段落都显示到一行中了Food and Health: The Foundation of a Good LifeYou are what you eat is an old saying that holds more truth today than ever before. Our diet is not just about satisfying hunger—it directly shapes our physical health, energy levels, mental clarity, and even our long-term lifespan. In a world dominated by fast food and ultra-processed snacks, understanding the connection between what we eat and how we feel has become essential.A balanced diet is the cornerstone of good health. It does not mean strict restrictions or giving up all the foods we love. Instead, it means eating a variety of nutrient-dense foods in the right proportions. This includes plenty of colorful fruits and vegetables, which are packed with vitamins, minerals, and antioxidants that protect our bodies from diseases. Whole grains like brown rice and oats provide sustained energy, while lean proteins such as fish, chicken, and beans help build and repair our muscles. Healthy fats from nuts, avocados, and olive oil are crucial for brain function and heart health.Unfortunately, modern lifestyles have led many people to rely heavily on processed foods. These convenient options are often loaded with added sugars, salt, and unhealthy trans fats, but lack essential nutrients. Regular consumption of these foods can lead to a range of health problems, including obesity, heart disease, type 2 diabetes, and high blood pressure. They can also cause energy crashes, leaving us feeling tired and sluggish throughout the day.What many people do not realize is that diet also has a profound impact on our mental health. Research has shown that a diet rich in whole foods can reduce the risk of depression and anxiety. The gut is often called our second brain, and the food we eat affects the production of neurotransmitters like serotonin, which regulates our mood. On the contrary, a diet high in sugar and processed foods can disrupt this balance and worsen mood swings.Making small, sustainable changes to your eating habits can have a huge impact on your overall health. Start by adding one extra serving of vegetables to your meals each day. Swap sugary drinks for water or herbal tea. Try cooking at home more often, so you can control the ingredients in your food. Remember, healthy eating is a journey, not a destination. It is okay to enjoy your favorite treats occasionally, as long as you maintain balance most of the time.In conclusion, the food we choose to eat is one of the most powerful tools we have for taking care of our health. By nourishing our bodies with wholesome foods, we can increase our energy, improve our mood, and reduce the risk of chronic diseases. A healthy diet is not just about living longer—it is about living better.输出的统计结果character count including spaces: 3193character count excluding spaces: 2212Totally words: 151Totally lines: 7High-frequency words(Top 10)our: 12health: 8foods: 7diet: 6food: 5not: 5your: 5energy: 4eat: 4healthy: 3六、代码#include fstream#include map#include string#include vector#include algorithm#include iostream#include stdexcept#include unordered_set#include rangesusing namespace std;class Text_Statistics {private: // 类成员变量int chars_with_spaces, chars_without_spaces; // 字符数含/不含空格与标点符号mapstring, int words_frequency; // 词频vectorpairstring, int high_frequency_words; // 高频词int word_count, line_count; // 单词数目、文本行数unordered_setchar punctuation; // 空格与标点符号private: // 辅助函数/*----- 获得单词 -----*/void read_word(const string text, int i){const int SIZE text.size();string word;while (i SIZE){chars_with_spaces;if (punctuation.count(text[i])) break; // 遇见空格或标点符号word text[i];i;chars_without_spaces;}if (filter(word)) words_frequency[word]; // 添加if (words_frequency.count(word)) word_count;}/*----- 过滤器 -----*/bool filter(const string word){// 过滤空字符串if (word.empty()) return false;// 过滤常见虚词static const unordered_setstring stop_words {a, an, the, am, is, are, was, were,be, been, being, have, has, had, do, does, did,will, would, shall, should, may, might, can, could,of, in, on, at, to, for, with, by, about, as,but, and, or, so, if, because, when, where, which,that, this, these, those, he, she, it, we, you, they,i, me};return !stop_words.count(word);}/*----- 词频排序 -----*/void sort_by_value(){high_frequency_words.assign(words_frequency.begin(), words_frequency.end());sort(high_frequency_words.begin(), high_frequency_words.end(), [](const pairstring, int a, const pairstring, int b){return a.second b.second;});}private: // 关键函数/*----- 加载文本 -----*/void load_from_local(){ios::sync_with_stdio(false);cin.tie(nullptr);ifstream ifs;ifs.open(Text.txt, ios::in);if (!ifs.is_open()) throw runtime_error(Error: Failed to open file data.txt!);string buf;while (getline(ifs, buf)){line_count;int idx 0;const int SIZE buf.size();// 将所有大写字母转为小写auto lowerView buf | views::transform([](unsigned char c){return tolower(c);});string lowerStr(lowerView.begin(), lowerView.end());// 读取单词while (idx SIZE){const char character buf[idx];// 碰到空格或标点符号if (punctuation.count(character)){