/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年6月10日
*版本号:v1.0
*
*问题描述:做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文、中文释义与词性间用’\t’隔开。
*输入描述:英语单词。
*输出描述:单词的词性和中文释义。
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
class Dictionary;
class Word
{
public:
friend class Dictionary;
private:
string english;
string chinese;
string word_class;
};
class Dictionary
{
public:
Dictionary ();
void searchwords(string word);
private:
Word words [8000];
int wordsNum;
};
Dictionary::Dictionary()
{
wordsNum=0;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"The file can't open!"<<endl;
exit(1);
}
while(!infile.eof())
{
infile>>words[wordsNum].english>>words[wordsNum].chinese>>words[wordsNum].word_class;
++wordsNum;
}
infile.close();
}
void Dictionary::searchwords(string word)
{
int high=wordsNum-1,low=0,key=-1,mid;
while(high>=low)
{
mid=(high+low)/2;
if(word==words[mid].english)
{
key=mid;
break;
}
if(word>words[mid].english)
low=mid+1;
else
high=mid-1;
}
if(key>=0)
cout<<word<<"--->"<<words[key].word_class<<'\t'<<words[key].chinese<<endl;
else cout<<"查无此词!"<<endl;
}
int main()
{
Dictionary dict;
string word;
do
{
cout<<"请输入待查询的关键词(英文),0000结束:"<<endl;
cin>>word;
if (word!="0000")
{
dict.searchwords(word);
}
}
while(word!="0000");
cout<<"欢迎再次使用!"<<endl<<endl;
return 0;
}
运行结果: