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.
(1) Programming, the user input English words, show the part of speech and Chinese interpretation.
Tip 1: If you want to use OOP to complete this dictionary (which can also be implemented using the Oo method), you can define a word class that represents an entry with the data member string 中文版; denotes English word, string Chinese; Indicates the Chinese meaning, string word_class; A dictionary class that represents the dictionary, where word words[8000] members represent terms in the dictionary, int wordsnum; Represents the number of entries in a dictionary, reads entries from a file in a constructor, and adds a member function to look up a word.
Tip 2: The words in the file are sorted, so when searching, use the binary search method to improve the efficiency.
Tip 3: Such projects, preferably in the form of multiple files organized.
Source:
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: June 21, 2015 * version number: v1.0*/#include <iostream > #include <string> #include <fstream> #include <cstdlib>using namespace Std;class word{private: String 中文版; String Chinese; String word_class;public:void Set (string e,string c,string WC); String Get_english () {return 中文版;} String Get_chinese () {return chinese;} String Get_word_class () {return word_class;}}; void Word::set (String e,string c,string WC) {ENGLISH=E;CHINESE=C;WORD_CLASS=WC;} Class Dictionary{public:dictionary (); int Binary_search (string key); int wordsnum; Word words[8000]; void display (int tem);}; void Dictionary::d isplay (int tem) {Cout<<words[tem].get_chinese () <<words[tem].get_word_class () < <endl;} Dictionary::D ictionary () {string E,C,WC; wordsnum=0; Ifstream infile ("Dictionary.txt", ios::in); if (!infile) {cout<< "Open file failed! "; Exit (1); } while (!infile.eof ()) {infile>>e>>c>>wc; Words[wordsnum].set (E,C,WC); wordsnum++; } infile.close ();} int Dictionary::binary_search (string key) {int i=-1; int low=0,high=wordsnum-1,mid; while (Low<=high) {mid= (Low+high)/2; if (Words[mid].get_english () ==key) {return mid; } else if (Words[mid].get_english () >key) high=mid-1; else low=mid+1; } return i;} int main () {Dictionary dic; String key; int tem; cout<< "Welcome to use this dictionary (0000) exit" <<endl; while (1) {cin>>key; if (key== "0000") break; Tem=dic.binary_search (key); if (tem==-1) {cout<< "does not have this term" <<endl; Continue } else Dic.display (TEM); }}
Operation Result:
@ Mayuko
14th Week Project 3-oop Edition electronic Dictionary