*/
#include<iostream> #include<fstream> #include<string> #include<iomanip> using namespace std; int min(int a,int b); class Word { public: Word(); Word(string word,string word_meaning,string word_type); void set_word(string word); void set_word_meaning(string word_meaning); void set_word_type(string word_type); string get_word(); string get_word_meaning(); string get_word_type(); //friend ostream& operator << (ostream&,Word&); //重载流插入运算符“<<” ;*/ private: string word; string word_meaning; string word_type; }; void input_word(Word s[]); Word search(string word,Word w1[]); int main() { Word w1[8000],w2; bool key=1; string word; input_word(w1); while(key==1) { cout<<"请输入您要查找的单词:"; cin>>word; w2=search(word,w1); cout<<"您要查找单词的意思为:"<<w2.get_word_meaning()<<'\t'<<w2.get_word_type()<<endl<<endl; cout<<"若想继续查词请按1,结束查词请按0000!"; cin>>key; cout<<endl; } system("PAUSE"); return 0; } int min(int a,int b) { if(a>b) a=b; return a; } Word::Word() { word="unknow"; word_meaning="unknow"; word_type="unknow"; } Word::Word(string word,string word_meaning,string word_type) { this->word=word; this->word_meaning=word_meaning; this->word_type=word_type; } void input_word(Word s[]) { int i=0; string word; string word_meaning; string word_type; ifstream infile("dictionary.txt",ios::in); if (!infile) { cerr<<"open error!"<<endl; exit(1); } for (i=0;i<8000;i++) { infile>>word; s[i].set_word(word); infile>>word_meaning; s[i].set_word_meaning(word_meaning); infile>>word_type; s[i].set_word_type(word_type); } infile.close(); //cout<<endl; } void Word::set_word(string word) { this->word=word; } void Word::set_word_meaning(string word_meaning) { this->word_meaning=word_meaning; } void Word::set_word_type(string word_type) { this->word_type=word_type; } string Word::get_word() { return word; } string Word::get_word_meaning() { return word_meaning; } string Word::get_word_type() { return word_type; } Word search(string word,Word w1[]) { Word w2; int i=0; int a=0,b=0,c; w2.set_word(word); string s1,s2,s3,s4; int low=0,high=7962,mid; s1=w1[low].get_word(); s2=w1[high].get_word(); s4=w2.get_word(); mid=(low+high)/2; s3=w1[mid].get_word(); while(low<=high) { a=word.size(); b=s3.size(); c=min(a,b); if(s3==word) return w1[mid]; else if(s3>word) { high=mid-1; } else { low=mid+1; } mid=(low+high)/2; s1=w1[low].get_word(); s2=w1[high].get_word(); s3=w1[mid].get_word(); } cerr<<"很遗憾,查无此词!"; exit(1); return w2; }