/* *Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 作 者:马广明 * 完成日期:2014 年 6 月 7 日 * 版 本 号:v1.0 * 问题描述:电子词典 */ #include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; class Word { public: string getenglish() { return english; } string getchinese() { return chinese; } string getword_class() { return word_class; } friend istream&operator>>(istream &input,Word &w); friend ostream&operator<<(ostream &output,Word &w); private: string english; string chinese; string word_class; }; Word word[8000]; istream&operator>>(istream &input,Word &w) { input>>w.english>>w.word_class>>w.chinese; return input; } ostream&operator<<(ostream &output,Word &w) { output<<w.word_class<<" "<<w.chinese<<endl; return output; } int main() { int n=0,hign,low,middle; string Eng; ifstream infile("dictionary.txt",ios::in); if(!infile) { cerr<<"open error!"<<endl; exit(1); } while(!infile.eof()) { infile>>word[n]; n++; } infile.close(); while(1) { hign=n-1; low=0; middle=(hign+low)/2; cin>>Eng; if(Eng=="0000") { break; } else { while(hign>=low) { middle=(hign+low)/2; if(word[middle].getenglish()==Eng) { cout<<word[middle]; break; } if(word[middle].getenglish()>Eng) { hign=middle-1; } if(word[middle].getenglish()<Eng) { low=middle+1; } } if(word[middle].getenglish()!=Eng) cout<<"查无此词"<<endl; } } return 0; }
英汉字典
时间: 2024-10-16 11:26:39