/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:C++.cpp
*作 者:王一锟
*完成日期:2014年11月29日
*版 本 号:v1.0
*
*问题描述:电子词典
*输入描述:
*输出描述:
*/
#include <fstream>
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
string e[8000],c[8000];
int wordsNum=0;
int BinSeareh(int low, int high, string k);
int main()
{
string word;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while (infile>>e[wordsNum]>>c[wordsNum])
{
++wordsNum;
}
infile.close();
do
{
cout<<"请输入要查的词(0000结束):";
cin>>word;
if (word=="0000")
break;
else
{
int low=0,high=wordsNum-1;
int index=BinSeareh(low, high, word);
if (index==-1)
cout<<"查无此词!"<<endl<<endl;
else
cout<<word<<"的中文意思是:"<<c[index]<<endl<<endl;
}
}
while(1);
cout<<"欢迎再次使用!"<<endl<<endl;
return 0;
}
int BinSeareh(int low, int high, string k)
{
int mid;
while(low<=high)
{
mid=(low + high)/2;
if(e[mid]==k)
{
return mid;
}
if(e[mid]>k)
high=mid-1;
else
low=mid+1;
}
return -1;
}
运行结果: