第15周 电子词典

/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:赵焱
*完成日期:2014 年 6 月 6 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
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();
    return out;
}
int main()
{
    Word word[8000];
    string english;
    int i=0,head,end,middle;
    ifstream infile ("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;
}

你可能感兴趣的:(第15周 电子词典)