c++11 文字查詢練手小程式

anribras發表於2017-07-28

查詢文字,輸入單詞,列印檔案中出現該單詞的次數,以及行號,
同一行出現多次,僅算做1次
單詞不區分大小寫
文字標點暫時只有. , !

#include <memory>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <map>
#include <iterator>
#include <sstream>
#include <unordered_map>
#include <set>




using namespace std;



//string 存單詞,set存行號,set的大小,就是單詞出現的次數
typedef unordered_map<string,set<int> > word_db;

string & word_convert(string & word)
{
    if (word[0] >= 'A' && word[0] <= 'Z')
            word[0] += 32;

    if (word[word.length() -1] == ',' ||
            word[word.length() -1] == '.' ||
            word[word.length() -1] == '!' ) {

        word = word.substr(0,word.length() -1);
    }   

    return word;
}

shared_ptr<word_db> init_file(const char* filename)
{
    int lnum = 0;
    int count = 0;
    auto pret = make_shared<word_db>();
    char line[512];
    //char* filename = "README";
    ifstream fin(filename);
    if (!fin)  {
        cout << "file" << filename << "not exsit\n";
        exit(-1);
    }
    //istream_iterator<string> scin;
    string word;

    while(fin.getline(line,512)) {
        //cout << line << endl;
        ++lnum;
        istringstream ss(line);
        auto s =  set<int>();
        while(ss >> word) {
            word = word_convert(word);
            auto it = (*pret).find(word); 

            //如果word在沒有出現過,新增1次
            if (it == end(*pret) ) {
                //新增key,key對應一個新建的set,同時新增本行行號
                s.insert(lnum);
                (*pret)[word] = s;
            } else {
                //否則直接新增行號
                it->second.insert(lnum);
            }

        }
    }
    return pret;
}

void cout_by_line(int num, const char* filename) 
{
    char line[512];
    int lnum = 0;
    ifstream fin(filename);
    if (!fin)  {
        cout << "file" << filename << "not exsit\n";
        exit(-1);
    }
    while(fin.getline(line,512)) {
        ++lnum;
        if(lnum == num) cout << "(Line " << num << " ) " << line << endl;
    }


}


int main(int argc, char* argv[])
{
    char line[512];
    char* filename = argv[1];
    ifstream fin(filename);
    if (!fin)  {
        cout << "file" << filename << "not exsit\n";
        exit(-1);
    }
    //istream_iterator<string> scin;
    string word;

    string a = string("Cool.");
    string b = string("Sool!");
    string c = string("Soosdsdf.");
    cout << word_convert(a) <<  word_convert(b) << endl;

   auto database = init_file(filename);

   string input;
   while(1) {
       cout << "input a word:\n";

       cin >> input;

       input = word_convert(input);

       if(input == "quit") break;

       auto it =  database->find(input);
       if ( it != end(*database) ) {
           cout << "Found word " << input << " " << it->second.size() << " times.\n" ;
           for( auto itset:(it->second)) {
               cout_by_line(itset,filename);
           }
       } else {
           cout << "Not find anything.\n";
       }
   }

    return 0;
}

相關文章