您的位置:首页 > 其它

第十五周项目3-简易中英词典

2014-06-03 12:23 190 查看
/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 6 月 3 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: 英语单词
*问题描述:输出英语单词对应的意思
*程序输出:单词意思
*问题分析:
*算法设计:
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
class Word
{
public:
Word() {}
string getEnglish();
string getChinese();
string getWord_Class();
friend istream & operator >>(istream &in,Word &w);
friend ostream & operator <<(ostream &out,Word &w);
private:
string english;
string chinese;
string word_class;
};
string Word::getEnglish()
{
return english;
}
string Word::getChinese()
{
return chinese;
}
string Word::getWord_Class()
{
return word_class;
}
istream & operator >>(istream &in,Word &w)
{
in>>w.english>>w.chinese>>w.word_class;
return in;
}
ostream & operator <<(ostream &out,Word &w)
{
out<<w.getChinese()<<"\t"<<w.getWord_Class()<<endl;
return out;
}
int main()
{
Word word[8000];
string english;
int i=0,head,end,middle;
ifstream infile ("D:\\dictionary.txt",ios::in);
if(!infile)
{
cerr<<"Open Error!"<<endl;
exit(1);
}
while(!infile.eof())
infile>>word[i++];
infile.close();
while(cin>>english&&english!="0000")
{
head=0;
end=i;
middle=(head+end)/2;
while(head<end&&word[middle].getEnglish()!=english)
{
if(word[middle].getEnglish()<english)//要查的词在后半部分
head=middle+1;
if(word[middle].getEnglish()>english)//要查的词在前半部分
end=middle-1;
middle=(head+end)/2;//继续二分
}
if(word[middle].getEnglish()!=english)
cout<<"查无此词!"<<endl;
else
cout<<word[middle];
}
return 0;
}


运行结果:



心得体会:。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息