#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
class Word
{
private:
string english;
string chinese;
string word_class;
public:
void getword(string e,string c,string w)
{
english=e,chinese=c,word_class=w;
}
void dispaly()
{
cout<<english<<'\t'<<chinese<<'\t'<<word_class<<endl;
}
bool dengyu(string &w)
{
bool f=false;
if(w==english)
{
f=true;
}
return f;
}
bool dayu(string &w)
{
bool f=false;
if(w>english)
{
f=true;
}
return f;
}
};
class Dictionary
{
private:
int wordsNum;
Word words[8000];
public:
Dictionary()
{
int i=0;
string e,c,w;
ifstream infile("dictionary.txt");
if(!infile)
{
cout<<"打开错误!"<<endl;
}
while(infile>>e>>c>>w)
{
words[i].getword(e,c,w);
i++;
}
wordsNum=i-1;
}
void fin(string w)
{
int mi=0,mid=wordsNum/2,ma=wordsNum;
while(1)
{
if(words[mid].dengyu(w))
{
words[mid].dispaly();
break;
}
else
{
if(words[mid].dayu(w))
{
mi=mid+1;
mid=(wordsNum+mi)/2;
}
else
{
ma=mid-1;
mid=(ma+mi)/2;
}
}
if(mi>ma) {
cout<<"未找到!"<<endl;
}
}
}
};
int main()
{
string word;
Dictionary dictionary;
while(1)
{
cout << "请输入要查找的单词:" << endl;
cin>>word;
dictionary.fin(word);
}
return 0;
}
OOP版电子词典
最新推荐文章于 2017-07-08 13:57:00 发布