怎么在C++中利用多線程實(shí)現(xiàn)電子詞典

本篇文章給大家分享的是有關(guān)怎么在C++中利用多線程實(shí)現(xiàn)電子詞典,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),井陘礦企業(yè)網(wǎng)站建設(shè),井陘礦品牌網(wǎng)站建設(shè),網(wǎng)站定制,井陘礦網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,井陘礦網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

具體內(nèi)容如下

// Dictionary.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//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("\\*").c_str(), &fileinfo)) != -1)
 {
  do
  {
  //如果是目錄,迭代之
  if ((fileinfo.attrib & _A_SUBDIR))
  {
   //if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
   //getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
   continue;
  }
  else
  {
   files.push_back(p.assign(path).append("\\").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);//獲取當(dāng)前窗口句柄

 SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//設(shè)置顏色

}
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 << "加載完成!" << "耗時(shí):" << (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;
}

效果圖:

怎么在C++中利用多線程實(shí)現(xiàn)電子詞典

以上就是怎么在C++中利用多線程實(shí)現(xiàn)電子詞典,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)標(biāo)題:怎么在C++中利用多線程實(shí)現(xiàn)電子詞典
路徑分享:http://bm7419.com/article20/gihjjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化、App開(kāi)發(fā)網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)