C++ 统计英文文本单词数量和出现次数

本教程提供 C++ 代码示例,用于统计英文文本中的单词数量和每个单词出现的次数,包含基本版(区分大小写,不删除标点符号)和进阶版(删除指定标点符号,忽略大小写)两种实现。

输入说明

若干行英文,最后以'!!!!!'为结束。

输出说明

  • 单词数量
  • 出现次数排名前 10 的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。

基本版代码

#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

int main() {
    string line;
    map<string, int> wordCount;
    int totalWords = 0;

    while (getline(cin, line) && line != '!!!!!') {
        if (!line.empty()) {
            string word;
            for (int i = 0; i < line.length(); i++) {
                if (isspace(line[i])) {
                    if (!word.empty()) {
                        wordCount[word]++;
                        totalWords++;
                        word.clear();
                    }
                } else {
                    word += line[i];
                }
            }
            if (!word.empty()) {
                wordCount[word]++;
                totalWords++;
            }
        }
    }

    cout << '单词数量: ' << totalWords << endl;
    cout << '出现次数排名前 10 的单词: ' << endl;

    // 排序并输出前 10 个单词
    vector<pair<string, int>> top10(wordCount.begin(), wordCount.end());
    sort(top10.begin(), top10.end(), [](const pair<string, int>& a, const pair<string, int>& b) {
        if (a.second != b.second) {
            return a.second > b.second; // 按出现次数降序排序
        } else {
            return a.first < b.first; // 次数相同则按字母升序排序
        }
    });

    for (int i = 0; i < min(10, (int)top10.size()); i++) {
        cout << top10[i].first << ' : ' << top10[i].second << endl;
    }

    return 0;
}

进阶版代码

#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

int main() {
    string line;
    map<string, int> wordCount;
    int totalWords = 0;

    while (getline(cin, line) && line != '!!!!!') {
        if (!line.empty()) {
            // 删除指定标点符号
            for (char& c : line) {
                if (c == '!' || c == '.' || c == ',' || c == ':' || c == '*' || c == '?' ) {
                    c = ' ';
                }
            }

            string word;
            for (int i = 0; i < line.length(); i++) {
                if (isspace(line[i])) {
                    if (!word.empty()) {
                        wordCount[word]++;
                        totalWords++;
                        word.clear();
                    }
                } else {
                    word += tolower(line[i]); // 忽略大小写
                }
            }
            if (!word.empty()) {
                wordCount[word]++;
                totalWords++;
            }
        }
    }

    cout << '单词数量: ' << totalWords << endl;
    cout << '出现次数排名前 10 的单词: ' << endl;

    // 排序并输出前 10 个单词
    vector<pair<string, int>> top10(wordCount.begin(), wordCount.end());
    sort(top10.begin(), top10.end(), [](const pair<string, int>& a, const pair<string, int>& b) {
        if (a.second != b.second) {
            return a.second > b.second; // 按出现次数降序排序
        } else {
            return a.first < b.first; // 次数相同则按字母升序排序
        }
    });

    for (int i = 0; i < min(10, (int)top10.size()); i++) {
        cout << top10[i].first << ' : ' << top10[i].second << endl;
    }

    return 0;
}
C++ 统计英文文本单词数量和出现次数 - 基本版和进阶版

原文地址: https://www.cveoy.top/t/topic/oelX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录