#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class Word //定义类Word
{
public:
void set(string E,string C,string Wor); //置值
int compare(string);
void display(); //输出
private:
string English;
string Chinese;
string Word_Class;
};
void Word::set(string E, string C, string Wor)
{
English=E;
Chinese=C;
Word_Class=Wor;
}
int Word::compare(string k)
{
return English.compare (k);
}
void Word::display()
{
cout<<English<<'\t'<<Word_Class<<'\t'<<Chinese<<endl;
cout<<endl;
}
int BinSeareh(int low, int high, Word *w, string k)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(w[mid].compare(k)==0)
{
return mid;
}
if(w[mid].compare(k)>0)
{
high=mid-1;
}
else
low=mid+1;
}
return -1;
}
int main()
{
Word words[8000];
string E,C,W_C;
string key;//查询关键字
int wordsNum=0;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(!infile.eof())
{
infile>>E>>C>>W_C;
words[wordsNum].set(E,C,W_C);
++wordsNum;
}
do
{
cout<<"请输入要查询的词:";
cin>>key;
if(key!="0000")
{
int low=0,high=wordsNum-1;
int index=BinSeareh(low, high, words,key);
if(index==-1)
cout<<"查无此词!"<<endl;
else
words[index].display();
}
}
while(key!="0000");
cout<<"欢迎下次使用!"<<endl;
system("pause");
return 0;
}
运行结果:
请输入要查询的词:what
what pron. 什么
请输入要查询的词:is
查无此词!
请输入要查询的词:was
查无此词!
请输入要查询的词:this
this adj.,pron. 这,这个
请输入要查询的词:that
that adj. 那,那个
请输入要查询的词:where
where adv. 哪里
请输入要查询的词:come
come v. 来到,出现,发生
请输入要查询的词:a
查无此词!
请输入要查询的词:
感言:
相当的好(但别人指点了不少)!!!还要努力啊!!!