##电子词典
#pragma warning (disable:4996)
#include<iostream>
#include<map>
#include<fstream>
#include<string>
using namespace std;
int main()
{
//创建一个map容器用来存放中英文数据
map<string, string> word_map;
fstream fd;
//打开文件
fd.open("dict.txt", fstream::in);
char str[100] = { 0 };
char key[20] = { 0 };
char value[50] = { 0 };
//peek()读取下一个字符,只要没读完就一直读
while (fd.peek() != EOF)
{
//按行读取文件中的内容
fd.getline(str, 100);
//将读取到的数据通过sscanf函数进行拆分,以空格作为分割,分别放到key,value数组中
sscanf(str, "%s %s", key, value);
//再把key,value存到map容器中
word_map.insert(pair<string, string>(key, value));
}
string word;
while (1)
{
cout << "请输入要查询的单词:" << endl;
cin >> word;
//count(key)通过key去查找,查找成功返回1,找不到则返回0
int ret = word_map.count(word);
if (ret == 0)
{
cout << "未查询到该单词!" << endl;
}
else
{
cout << "单词解释如下:" << endl;
//通过[key]去访问map容器中的数据
cout << word_map[word] << endl;
}
}
return 0;
}