KMP文本单词检索

文本单词检索

试题描述:要求编程建立一个文本文件,每个单词不包含空格且不跨行,单词由字符序列构成且区分大小写;统计给定单词在文本文件中出现的总次数;检索输出某个单词出现在文本中的行号、在该行中出现的次数以及位置。该设计要求可分为三个部分实现:其一,建立文本文件,文件名由用户用键盘输入;其二,给定单词的计数,输入一个不含空格的单词,统计输出该单词在文本中的出现次数;其三,检索给定单词,输入一个单词,检索并输出该单词所在的行号、该行中出现的次数以及在该行中的相应位置。能够进行BF或KMP的模式匹配。要求有运行结果截图。

#include <bits/stdc++.h>
using namespace std;
string file_name, a;
int KMP(string line, string word, int flag = 0, int o = 0) {
    int F[20], count = 0;
    F[0] = -1;
    for (int i = 1; i < int(word.length()); i++) {
        int j = F[i - 1];
        while ((word[j + 1] != word[i]) && (j >= 0))
            j = F[j];
        if (word[j + 1] == word[i])
            F[i] = j + 1;
        else
            F[i] = -1;
    }
    int i = 0, j = 0, num = 1, tflag = 0;
    while (i < int(line.length())) {
        if (line[i] == word[j]) {
            i++;
            j++;
            if (j == int(word.length())) {
                if (line[i]!=' '&&i!=int(line.length()))
                {
                    j=0;
                    while(line[i]!=' ')
                        i++;
                    num++;
                    continue;
                }
                if (flag) {
                    if (!tflag){
                        cout << "第" << o << "行的:";
                        tflag=1;
                    }
                    cout << "第" << num << "个单词,";
                }
                count++;
                j = F[j - 1] + 1;
            }
        } else {
            if (j == 0)
                i++;
            else
                j = F[j - 1] + 1;
        }
        if (line[i] == ' ') num++;
    }
    return count;
}
void writeFile() {
    system("cls");
    int n;
    cout << "请输入单词本名称: ";
    cin >> file_name;
    cout << "请输入单词本行数:";
    cin >> n;
    ofstream outfile;
    outfile.open((file_name + ".txt").c_str());
    cout << "请输入"<<n<<"行单词(单词间以空格分隔):" << endl;
    for (int i = 0; i <= n; ++i) {
        getline(cin, a);
        if (i==0) continue;
        outfile << a << endl;
    }
    outfile.close();
    cout<<"单词本已生成在!按任意键返回"<<endl;
    char c;
    cin>>c;
}
void search(string word) {
    ifstream infile;
    int flag=0;
    int count = 0, num = 1;
    infile.open((file_name + ".txt").c_str());;
    while (getline(infile, a)) {
        count = KMP(a, word, 1, num);
        if (count) {
            cout << "一共出现了" << count << "次" << endl;
            flag=1;
        }
        num++;
    }
    if (!flag) cout<<"该单词未在文本中出现!"<<endl;
}
void countNum(string word) {
    int count = 0;
    ifstream infile;
    infile.open((file_name + ".txt").c_str());;
    while (getline(infile, a)) {
        count += KMP(a, word);
    }
    cout << "该单词共在文本中出现了" << count << "次!" << endl;
}
int main(int argc, char const *argv[]) {
    cout<<"                                 欢迎来到Eumenides的单词本"<<endl<<endl;
    cout<<"目前无单词本,请先制作!继续或退出(y/任意键)"<<endl;
    char c;
    cin>>c;
    if(c=='y') writeFile();
    while(c=='y'){
        system("cls");
        cout<<"你可以进行以下操作:"<<endl;
        cout<<"   q:单词检索"<<endl;
        cout<<"   c:单词计数"<<endl;
        cin>>c;
        if(c=='q'){
            string word;
            cout<<"请输入需要检索的单词(不包含空格):";
            cin>>word;
            search(word);
        }else if(c=='c'){
            string word;
            cout<<"请输入需要计数的单词(不包含空格):";
            cin>>word;
            countNum(word);
        }else{
            cout<<"未包含该选项,重新选择or退出(y/任意键):";
            cin>>c;
            continue;
        }
        cout<<"继续操作or退出(y/任意键):";
        cin>>c;
    }
    
    return 0;
}
发布了21 篇原创文章 · 获赞 3 · 访问量 1955

猜你喜欢

转载自blog.csdn.net/Ruanes/article/details/104239280