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()
{
}
1. 读取一篇包括标点符号的英文文章(InFile.txt),假设文件中单词的个数最不超过5000个。从文件中读取单词,过滤掉所有的标点。 2. 分别利用线性表(包括基于顺序表的顺序查找、基于链表的顺序查找、折半查找)、二叉排序树和哈希表(包括基于开放地址法的哈希查找、基于链地址法的哈希查找)总计6种不同的检索策略构建单词的存储结构。 3. 不论采取哪种检索策略,完成功能均相同。 (1)词频统计 当读取一个单词后,若该单词还未出现,则在适当的位置上添加该单词,将其词频计为1;若该单词已经出现过,则将其词频增加1。统计结束后,将所有单词及其频率按照词典顺序写入文本文件中。其中,不同的检索策略分别写入6个不同的文件。 基于顺序表的顺序查找--- OutFile1.txt 基于链表的顺序查找--- OutFile2.txt 折半查找--- OutFile3.txt 基于二叉排序树的查找--- OutFile4.txt 基于开放地址法的哈希查找--- OutFile5.txt 基于链地址法的哈希查找--- OutFile6.txt 注:如果实现方法正确,6个文件的内容应该是一致的。 (2)单词检索 输入一个单词,如果查找成功,则输出该单词对应的频率,同时输出查找成功的平均查找长度ASL和输出查找所花费的时间。如果查找失败,则输出“查找失败”的提示。生成一篇c++5.11可以运行的代码
最新发布
06-12
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值