#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib> //调用exit(1)需要包含cstdlib
using namespace std;
int Middle (string,int,int);
const int num=8100;
string Chinese[num],English[num];
int main()
{
int i,get,a=0,left=0,right=8099;
string word;
ifstream infile("dictionary.txt",ios::in); //测试是否成功打开,打开失败时(如要读的数据文件不存在)退出
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0; i<num; i++)
{
infile>>English[i]>>Chinese[i];
a++;
}
infile.close();
while(word!="0000")
{
cout<<"请输入一个英文单词: ";
cin>>word;
get=Middle(word,left,right);
if(get!=-1)
cout<<English[get]<<"\t"<<Chinese[get]<<endl<<endl;
else
{
cout<<"该词典中没有这个单词!\n";
break;
}
}
return 0;
}
int Middle (string word,int left,int right)
{
while(left<=right)
{
int half=(left+right)/2;
if(English[half]==word)
{
return half;
}
else if(English[half]>word)
{
right=half-1;
}
else
{
left=half+1;
}
}
return -1;
}
电子词典

最新推荐文章于 2014-12-02 10:30:47 发布