/*
*Copyright (c) 2014,烟台大学计算机学院void change(int a[8][8]);
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2014年12月23日
*版本号:v1.0
*
*问题描述:电子词典
*输入描述:英语单词
*程序输出:英语单词所对应的汉语意思
*/
#include <fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
struct Word
{
string english;
string chinese;
string word_class;
};
Word words[8000];
int wordsnum=0;
using namespace std;
void readDictionary();
void searchwords(string);
int binsearch(int low,int high,string k);
int main()
{
readDictionary();
string key;
do
{
cout<<"请输入要查询的词(输入0000结束):"<<endl;
cin>>key;
if(key!="0000")
searchwords(key);
}
while(key!="0000");
cout<<"欢迎再次使用!"<<endl<<endl;
return 0;
}
void readDictionary()
{
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"dictionary open error!"<<endl;
exit(1);
}
while (!infile.eof())
{
infile>>words[wordsnum].english;
infile>>words[wordsnum].chinese;
infile>>words[wordsnum].word_class;
++wordsnum;
}
infile.close();
}
void searchwords(string key)
{
int low=0,high=wordsnum-1;
int a=binsearch(low,high,key);
if(a==-1)
cout<<"查无此词"<<endl<<endl;
else
cout<<key<<"--->"<<words[a].word_class+"\t"<<words[a].chinese<<endl<<endl;
}
int binsearch(int low,int high,string k)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(words[mid].english==k)
{
return mid;
}
if(words[mid].english>k)
high=mid-1;
else
low=mid+1;
}
return -1;
}
结构体应用不熟练