国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++多線程實現電子詞典

2020-01-26 13:30:22
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C++多線程實現電子詞典的具體代碼,供大家參考,具體內容如下

// Dictionary.cpp : 定義控制臺應用程序的入口點。//vs2013編譯//字典文件:https://pan.baidu.com/s/1YHtwptaq_V8j034U9_J96A#include "stdafx.h"#include <string>#include <vector>#include <iostream>#include <map>#include <fstream>#include <io.h>#include <thread>#include <time.h>#include <Windows.h>using namespace std;class ParseDirectory{public: ParseDirectory(string path){ this->path = path; getFiles(files); isdone = false; t = thread(&ParseDirectory::txtToDic, this); //t.join(); } bool isDone() { return isdone; } map<string, string> getDic() { return vecDics; } virtual ~ParseDirectory() { }private: vector<string> files; string path; thread t; map<string, string> vecDics; bool isdone; void getFiles(vector<string>& files) { //文件句柄 long  hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; if ((hFile = _findfirst(p.assign(path).append("http://*").c_str(), &fileinfo)) != -1) {  do  {  //如果是目錄,迭代之  if ((fileinfo.attrib & _A_SUBDIR))  {   //if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)   //getFiles(p.assign(path).append("http://").append(fileinfo.name), files);   continue;  }  else  {   files.push_back(p.assign(path).append("http://").append(fileinfo.name));  }  } while (_findnext(hFile, &fileinfo) == 0);  _findclose(hFile); } } void txtToDic() { for each (string file in files) {  fstream f(file);  string word, explain;  //map<string, string> dic;    if (f.is_open())  {  //cout << file << endl;  while (1)  {      getline(f, word);   if (!getline(f, explain))   break;   vecDics[word] = explain;  }  }  f.close();  //vecDics.push_back(dic); }  //cout << vecDics.size() << endl; isdone = true;  }};void setColor(unsigned short ForeColor = 2, unsigned short BackGroundColor = 0){ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//獲取當前窗口句柄 SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//設置顏色}int _tmain(int argc, _TCHAR* argv[]){   vector<ParseDirectory*> pds; cout << "正在加載資源..."; long start = clock(); vector<map<string, string> > allWords; for (int i = 0; i < 26; i++) { string name = ".//"; name += 'A' + i; pds.push_back(new ParseDirectory(name)); } int cnt = 0;  for (int i = 0; i < pds.size(); i++) { if (pds[i]->isDone()) {  cnt++;  allWords.push_back(pds[i]->getDic());  Sleep(300); } if (cnt == pds.size())  break; } system("cls"); cout << "加載完成!" << "耗時:" << (clock()-start)/1000.0 << "s" << endl; cout << allWords.size(); string inquir; while (1) { bool flag = false; setColor(); cout << "/n輸入要查詢的單詞:"; setColor(7, 0); cin >> inquir; for (int i = 0; i < allWords.size(); i++) {  auto t = allWords[i][inquir];  if (t.size())  {    cout << t << endl;  flag=true;  } } if (!flag) {  setColor(4, 0);  cout << "抱歉,未找到單詞" << endl; } }  system("pause"); return 0;}

效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三原县| 诸城市| 中西区| 丰镇市| 泽州县| 永平县| 荆州市| 鲁甸县| 南岸区| 扬中市| 莱州市| 察哈| 台前县| 库尔勒市| 永康市| 龙南县| 黔江区| 濮阳县| 瑞安市| 衢州市| 虹口区| 樟树市| 常熟市| 桐柏县| 上思县| 南康市| 二连浩特市| 鸡泽县| 钟山县| 盖州市| 天津市| 肃宁县| 云梦县| 无极县| 漳平市| 宜城市| 县级市| 凌云县| 包头市| 河北省| 松滋市|