C++查询文本中所有单词出现的频率,并且根据出现次数由多到少依次排列。

C++统计文章中所有单词出现的频率,并且根据出现次数由多到少依次排列。

编译器

本人所用的编译器为:CodeBlocks

程序说明

该程序统计查找文本中所有单词的出现次数,并且根据出现次数由多到少依次排列。该程序查考了C++ primer中单词查询程序,对单词查询程序加以修改得到的。

程序运行结果

对从网上找到的英语六级进行单词统计,统计所有单词的出现频率。结果如下:
英语六级真题

运行结果:
运行结果

源代码

main函数
#include <iostream>
#include<vector>
#include<fstream>
#include<cstdlib>
#include"statistics.h"

ifstream& open_file(ifstream& in, const string& file)
{
    in.close();
    in.clear();
    in.open(file.c_str());
    return in;
}

int main(int argc, char **argv)
{
    ifstream infile("text.txt");
    statistics tq;
    tq.read_file(infile);
    tq.dis();
    return 0;
}
类函数
#ifndef STATISTICS_H
#define STATISTICS_H
#include<iostream>
#include<vector>
#include<map>
#include<iostream>
#include<vector>
#include<fstream>
#include<set>
#include<string>
#include<map>
#include<sstream>

using namespace std;
class statistics
{
    public:
        typedef vector<string>::size_type line_no;
        statistics();
        virtual ~statistics();
        void read_file(ifstream& is){
            store_file(is);
            build_map();
        }
        void store_file(ifstream&);
        void build_map();
        void dis();
   private:
        vector<string> lines_of_text;
        map<string, int> word_map;
        map<string, int> finish;
};
#endif
#include "statistics.h"
void statistics::store_file(ifstream& file)
{
    string textline;
    while(getline(file, textline))
        lines_of_text.push_back(textline);
}
void statistics::build_map()
{
    for (statistics::line_no line_num = 0; line_num != lines_of_text.size(); ++line_num){
        istringstream line(lines_of_text[line_num]);
        string word;
        while(line >> word){
            word_map[word] += 1;
        }
    }
}
void statistics::dis()
{
    map<string, int> first_Max;
    map<string, int>::iterator Max;
    first_Max["finish"] = 0;
    for(map<string, int>::size_type times = 0; times != word_map.size(); times++){
        Max = first_Max.begin();
        for(map<string, int>::iterator num = word_map.begin(); num != word_map.end(); num++){
            if(num->second > Max->second)
                Max = num;
        }
        cout << Max->first << "\t" << Max->second <<" 次"<<endl;
        finish[Max->first] = Max->second;
        word_map.erase(Max);
    }
}
statistics::statistics()
{
}
statistics::~statistics()
{
}
  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个基本的统计特定单词文本出现次数和位置的 C++ 程序示例: ```cpp #include <iostream> #include <vector> #include <string> using namespace std; vector<int> count_word(string word, string text) { vector<int> positions; int start = 0; while (true) { int index = text.find(word, start); if (index == string::npos) { break; } positions.push_back(index); start = index + 1; } return positions; } int main() { string text = "This is a text sample for testing text."; string word = "text"; vector<int> positions = count_word(word, text); int count = positions.size(); cout << "The word '" << word << "' appears " << count << " times in the text at positions: "; for (int i = 0; i < count; i++) { cout << positions[i]; if (i < count - 1) { cout << ", "; } } cout << endl; return 0; } ``` 使用方法如下: 编译: ``` g++ count_word.cpp -o count_word ``` 运行: ``` ./count_word ``` 输出: ``` The word 'text' appears 2 times in the text at positions: 10, 32 ``` 其,`count_word` 函数接受两个参数:`word` 表示要统计单词,`text` 表示要搜索的文本串。函数返回一个 `vector`,存储了每次出现的位置。函数使用了字符串的 `find` 方法来查找单词文本出现的位置,每次搜索的起始位置是上一次搜索的位置加一。 主函数首先调用 `count_word` 函数来获取单词文本出现的位置,然后计算出单词文本出现次数,并输出结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值