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.
Programming, the user input English words, show the part of speech and Chinese interpretation.
Tip 1: Define a word structure that represents an entry in which the data member is String 中文版; Denotes an English word, a string Chinese, a corresponding Chinese meaning, a string word_class; denotes the part of the word; defines word words[8000] holds all the entry members, int wordsnum; Indicates the number of entries in the dictionary.
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, related functions are implemented using functions, preferably in multi-file form organization
/** Copyright (c) 2014, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: December 21, 2014 * version number: v1.0*/#include <iostream > #include <string> #include <fstream> #include <cstdlib>using namespace Std;int binary_search ( String key,int n); struct word{string 中文版; String Chinese; string Word_class;}; Word Word[8000];int Main () {int wordsnum=0; String key; int tem; Open file Ifstream infile ("Dictionary.txt", ios::in); if (!infile) {cout<< "Open file failed! "; Exit (1); } while (Infile>>word[wordsnum].english) {infile>>word[wordsnum].chinese; infile>>word[wordsnum].word_class; wordsnum++; } infile.close (); Close file cout<< "Welcome to this dictionary (0000) exit" <<endl; while (1) {cin>>key; if (key== "0000") break; Tem=binary_search (Key,wordsnum); if (tem==-1) {cout<< "└───── no such word" <<endl; Continue } else cout<< "└─────" <<word[tem].word_class<<word[tem].chinese<<endl; }}int Binary_search (string key,int N)//two-way lookup {int i=-1; int low=0,high=n-1,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 i;}
Operation Result:
@ Mayuko
17th Week Item 7-Electronic dictionary structure version