</pre><pre name="code" class="cpp">/*。
*Copyright(c)2014,烟台大学计算机学院
*All right reserved,
*文件名:test.cpp
*作者:biyutang
*完成日期:2014年12月1日
*版本号:v1.0
*
问题描述:
*输入描述:输入x
*程序输出:输出Y
*/
#include<fstream>
#include<iostream>
#include<cstdlib>
using namespace std;
string e[8000],c[8000];
int num=0;
int fac(int low,int high,string z);
int main()
{
string key;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(infile>>e[num]>>c[num])
{
num++;
}
infile.close();
do
{
cout<<"请输入要查的词(0000结束):"<<endl;
cin>>key;
if(key=="0000")
break;
else
{
int low=0,high=num-1;
int index=fac(low,high,key);
if(index==-1)
cout<<"查无此词!";
else
cout<<key<<"的中文意思是:"<<c[index]<<endl;
}
}
while(1);
return 0;
}
int fac(int low,int high,string z)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(e[mid]==z)
{
return mid;
}
if(e[mid]>z)
{
high=mid-1;
}
else
low=mid+1;
}
return (-1);
}
运行结果: