c++11 文字查詢練手小程式
查詢文字,輸入單詞,列印檔案中出現該單詞的次數,以及行號,
同一行出現多次,僅算做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;
}
相關文章
- 文字查詢程式c++primer12.32C++
- Mysql查詢練習MySql
- MYSQL練習1: DQL查詢練習MySql
- Java 查詢和高亮Word文字Java
- CAD如何進行文字查詢
- linux根據字尾查詢文字Linux
- 26_上機動手實戰演練mget批次查詢apiAPI
- sql查詢入門練習題SQL
- Hive -------- hive常見查詢練習Hive
- MySQL講義第 47 講——select 查詢之查詢練習(五)MySql
- YonBuilder低程式碼實戰:YonQL資料查詢小Case,讓SQL查詢變簡單UISQL
- mysql三表關聯查詢練習MySql
- 入門MySQL——查詢語法練習MySql
- SSM的查詢簡單練習+JSPSSMJS
- [練手]CantoneseCool 一個能說廣東話的小程式。
- Python小技巧 - 子串查詢Python
- 幾個SQL查詢小技巧SQL
- 小程式tabBar帶文字展示tabBar
- ACM常用STL查詢手冊ACM
- 【分模組練習】二分查詢
- 雲開發與 WePY,快速實現 Linux 命令查詢小程式Linux
- 雲開發與WePY,快速實現Linux命令查詢小程式Linux
- Excel VBA小程式 -使 用VBA實現VLOOKUP函式查詢?Excel函式
- 全國海域潮汐表查詢微信小程式詳情教程及程式碼微信小程式
- 小程式中富文字解決方案
- 藍橋杯:基礎練習 查詢整數
- 這個實時公交查詢小程式,支援全國 100 個城市
- 【吐血整理】微信小程式如何接入天氣預報查詢 API微信小程式API
- Python 查詢PDF中的指定文字並高亮顯示Python
- 如何抽取Oracle資料到文字文件進行查詢NAOracle
- Python練手程式碼段(2020.11.11)Python
- 小程式如何把文字玩出花樣
- 微信小程式富文字寫法微信小程式
- 手撕Vue-查詢指令和模板Vue
- 加密的手機號,如何模糊查詢?加密
- 微博根據手機號查詢
- SQL查詢的:子查詢和多表查詢SQL
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- mysql 標量子查詢和現金盤程式製作非法子查詢MySql