Make a simple e-dictionary. In the document Dictionary.txt, the preservation is a English-Chinese control dictionary, the vocabulary quantity nearly 8,000, English, the Chinese explanation and the part of speech uses ' \ t ' separates.
Code
#include <iostream> #include <fstream> #include <cstdlib>using namespace Std;class dictionary;class Word{public:word () {} void Set (String e,string c,string W) {english=e; Chinese=c; Word_class=w; } friend class dictionary;private:string 中文版; String Chinese; string Word_class;}; Class Dictionary{public:dictionary (); void Searchword (string en); int midsearch (int low,int high,string key);p Rivate:int Wordnum; Word word[8000];}; Dictionary::d ictionary () {fstream infile; String e,c,w; wordnum=0; Infile.open ("Dictionary.txt", ios::in); if (!infile) {cout<< "dictionary.txt can ' t open" <<endl; Exit (1); } while (!infile.eof ()) {infile>>e>>c>>w; Word[wordnum].set (E,C,W); wordnum++; } infile.close ();} int dictionary::midsearch (int low,int high,string key) {int mid; while (Low<=high) {mid= (Low+high)/2; if (Word[mid].english==key) return mid; else if (Word[mid].english>key) high=mid-1; else low=mid+1; } return-1;} void Dictionary::searchword (string en) {int key=midsearch (0,wordnum-1,en); if (key) cout<<word[key].english<< "-" <<word[key].word_class<< "\ T" <<word[key]. word_class<<endl; else cout<< "look no such word!" "<<endl;} int main () {dictionary D; string W; cout<< "Please enter the word you want to find:" <<endl; while (cin>>w) {D.searchword (w); } return 0;}
14th Week on-machine practical Project 3--Electronic Dictionary