吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4307|回复: 13
收起左侧

[C&C++ 转载] C++实现电子词典项目,适合进阶

[复制链接]
52lxw 发表于 2018-11-2 09:36
[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("\\*").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);
		}
		isdone = true;
		cout << vecDics.size() << endl;
		
	}
};
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;
	
	vector<map<string, string> > allWords;
	for (int i = 0; i < 26; i++)
	{
		string name = ".\\";
		name += 'A' + i;
		cout << name << endl;
		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());
		}
		if (cnt == pds.size())
			break;
	}

	string inquir;
	while (1)
	{
		bool flag = false;
		setColor();
		cout << "输入要查询的单词:";
		cin >> inquir;
		for (int i = 0; i < allWords.size(); i++)
		{
			auto  t = allWords[i][inquir];
			if (t.size())
			{
				setColor(7, 0);
				cout << t << endl;
				flag=true;
			}
		}
		if (!flag)
		{
			setColor(4, 0);
			cout << "抱歉,未找到单词" << endl;
		}
	}

	
	system("pause");
	return 0;
}


运行效果:

J85RKOFWP%`BR$YUBKXYT{7.png

免费评分

参与人数 2吾爱币 +1 热心值 +2 收起 理由
教授专用 + 1 谢谢@Thanks!
笨笨猪 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wangqiustc 发表于 2018-11-2 11:00
这么神奇吗,我试试看
狂龙@ 发表于 2018-11-2 11:08
fsrank 发表于 2018-11-2 11:59
罗密欧山伯 发表于 2018-11-2 12:21
我之前也写过这个练手,但是功能要好一些,更多更全一点,改天整理一下发出来,楼主挺热心。
 楼主| 52lxw 发表于 2018-11-2 12:48
狂龙@ 发表于 2018-11-2 11:08
在哪里看见过,传智的视频吗?

不是。。。。
破解project 发表于 2018-11-2 12:53
感谢分享,字典的内容是怎么弄到的?
笨笨猪 发表于 2018-11-2 16:06
谢谢楼主分享,我还很少用C++单纯的DOS界面写过什么东西呢
天道法海 发表于 2018-11-2 16:22
感谢楼主分享,长见识了
m0tky 发表于 2018-11-14 12:25
练手,感谢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 15:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表