一个简单的字词统计程序 C++

【问题描述】

某应用需要统计给定的文本文件中特定字词的出现次数。请按要求完成。

【功能要求】

  1. 文本文件按行存储;
  2. 定义和实现至少4个特定的字词统计类,每个字词统计类的定义的待统计字词是固定的;
  3. 只能通过文本文件的一次遍历,来完成多个字词的统计任务;
  4. 用户会同时选择对2个以上的预定字词进行统计;
  5. 用户可以对一个文本文件进行多次统计,每次统计完成后,在显示统计结果的同时,并将结果写入保存用户统计历史信息的文件中。

具体要求如下:

  1. 采取模块化方式进行程序设计,要求程序的功能设计、数据结构设计及整体结构设计合理。
  2. 系统以菜单界面方式(至少采用文本菜单界面,如能采用图形菜单界面更好)工作,运行界面友好,演示程序以用户和计算机的对话方式进行。
  3. 程序算法说明清晰,理论分析与计算正确,运行情况良好,实验测试数据无误,容错性强(能对错误输入进行判断控制)。
  4. 编程风格良好(包括缩进、空行、适当注释、变量名和函数名见名知意,程序容易阅读等);

 修改需要查找的函数中判断的字符可改为查找其他单词,要添加更多单词请参照已有的继承的类继续写,同时记得修改用户指引和相对的说明语句,修改输出路径在fopen哪里。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

class baseSearch{
public:
    virtual void wordSearch(vector<string> readFile)=0;//字词查找函数
    virtual void printWordNumber()=0;//打印结果和写文件函数
    int countplus(){//主函数中定义count
        count += 1;
        return count;
    }
private:
    int count = 0;
};

class andSearch:public baseSearch{
public:
    void wordSearch(vector<string> readFile){
        for(auto x:readFile){
            if(x == "and"||x == "And"){
                countplus();
            }
        }
    }

    void printWordNumber(){
        cout<<"这篇文章有"<<countplus()-1<<"个and"<<endl;
        ofstream fileout;
        fileout.open("D:\\C++Class Design\\history.txt",ios::out|ios::ate|ios::app);
        fileout<<"这篇文章有"<<countplus()-2<<"个and"<<endl;
    }

};

class mySearch:public baseSearch{
public:
    void wordSearch(vector<string> readFile){
        for(auto x:readFile){
            if(x == "my"||x == "My"){
                countplus();
            }
        }
    }
    void printWordNumber(){

        cout<<"这篇文章有"<<countplus()-1<<"个my"<<endl;
        ofstream fileout;
        fileout.open("D:\\C++Class Design\\history.txt",ios::out|ios::ate|ios::app);
        fileout<<"这篇文章有"<<countplus()-2<<"个my"<<endl;
    }

};

class canSearch:public baseSearch{
public:
    void wordSearch(vector<string> readFile){
        for(auto x:readFile){
            if(x == "can"||x == "Can"){
                countplus();
            }
        }
    }

    void printWordNumber(){
        cout<<"这篇文章有"<<countplus()-1<<"个can"<<endl;
        ofstream fileout;
        fileout.open("D:\\C++Class Design\\history.txt",ios::out|ios::ate|ios::app);
        fileout<<"这篇文章有"<<countplus()-2<<"个can"<<endl;
    }

};

class hisSearch:public baseSearch{
public:
    void wordSearch(vector<string> readFile){
        for(auto x:readFile){
            if(x == "his"||x == "His"){
                countplus();
            }
        }
    }

    void printWordNumber(){
        cout<<"这篇文章有"<<countplus()-1<<"个his"<<endl;
        ofstream fileout;
        fileout.open("D:\\C++Class Design\\history.txt",ios::out|ios::ate|ios::app);
        fileout<<"这篇文章有"<<countplus()-2<<"个his"<<endl;
    }


};

class herSearch:public baseSearch{
public:
    void wordSearch(vector<string> readFile){
        for(auto x:readFile){
            if(x == "her"||x == "Her"){
                countplus();
            }
        }
    }

    void printWordNumber(){
        cout<<"这篇文章有"<<countplus()-1<<"个her"<<endl;
        ofstream fileout;
        fileout.open("D:\\C++Class Design\\history.txt",ios::out|ios::ate|ios::app);
        fileout<<"这篇文章有"<<countplus()-2<<"个her"<<endl;
    }

};

void userGuide(){//用户指引
    cout<<"这是一个用于查找特定单词在一篇英语文章中的单词出现频率的程序"<<endl;
    cout<<"请在电脑的D盘创建名为C++Class Design的文件夹,将需要查找的文章命名为Passage.txt"<<endl;
    cout<<"查找结束后,在相同文件夹文件:history.txt,记录相关的查找记录和查找结果"<<endl;
    cout<<"请输入你要查找的单词的个数(1-5个):";
}

void searchGuide(){//用户指引
    cout<<"查找and   请输入1"<<endl;
    cout<<"查找my    请输入2"<<endl;
    cout<<"查找can   请输入3"<<endl;
    cout<<"查找his   请输入4"<<endl;
    cout<<"查找her   请输入5"<<endl;
    cout<<"请输入你要查找单词的序号:";
}

int main()
{
    ifstream fileIn;//文件打开、遍历
    fileIn.open("D:\\C++Class Design\\Passage.txt",ios::in);
    if(!fileIn){
        cout<<"error003:Can't found this passage"<<endl;
        exit(1);
    }
    fileIn.seekg(0,ios::beg);//指针移到第一位
    vector<string>Passage;
    string temp;
    while (fileIn>>temp){
        Passage.push_back(temp);
    }
    fileIn.close();
    int count,word_type;
    userGuide();
    cin>>count;
    if(count>5||count<0){//用户输入错误时提示
        cout<<"error001:请输入1-5之间的序号";
        exit(1);
    }
    baseSearch *p[count];
    for(int i = 0;i<count;i++){
        searchGuide();
        cin>>word_type;
        if(word_type>5||word_type<0){//用户输入错误时提示
            cout<<"error002:请输入1-5之间的序号";
            exit(0);
        }
        switch(word_type){//基类指针指向相应继承类对象
            case 1:
                p[i]=new andSearch;
                break;
            case 2:
                p[i]=new mySearch;
                break;
            case 3:
                p[i]=new canSearch;
                break;
            case 4:
                p[i]=new hisSearch;
                break;
            case 5:
                p[i]=new herSearch;
                break;
       }
    }
    for(int i = 0;i<count;i++){//多态的体现
        p[i]->wordSearch(Passage);
        p[i]->printWordNumber();
        if (i==count-1) {//查询结束时在文件结尾标志本次查找
            ofstream fileout;
            fileout.open("D:\\C++Class Design\\history.txt",ios::out|ios::ate|ios::app);
            fileout<<"<---------------------以上为查询结果------------------------->"<<endl;
            fileout.close();//关闭文件
        }
    }
    return 0;
}

单个查找:

 顺序查询:

 乱序查询:

 

重复查询:

 

 用户输入错误数据:

 可见结果,当用户不按顺序和重复查找时能输出正确结果,单个查找和多个查找均可以查找到正确结果,当用户输入不合法内容时程序有相应提示并退出程序,保证数据安全性。

用户使用说明

用户使用说明:这是一个用于查找特定单词在一篇英语文章中的单词出现频率的程序。请在电脑的D盘创建名为C++Class Design的文件夹,将需要查找的文章命名为Passage.txt,查找结束后,显示结果,并在相同文件夹文件:history.txt,记录相关的查找记录和查找结果,请输入你要查找的单词的个数(1-5个)。

用户查找说明:查找and   请输入1。查找my    请输入2。查找can   请输入3。查找his   请输入4。查找her   请输入5。请输入你要查找单词的序号。 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kun.A.A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值