14th Week Item three electronic dictionary

Source: Internet
Author: User

/**copyright (c) 2014, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: d.cpp* Author: Zhang Wanhua * Completion Date: June 3, 2015 * version number: v1.0*//* Project 3-oop Edition Electronic Dictionary "make a simple electronic 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 organized in multi-file format * * #include <fstream> #include <iostream> #include <string> #include <    Cstdlib>using namespace Std;class word{public:void Set (string e, String C, string wc);  int compare (string);    The English part compares with the given string, which equals return, greater than return, less than 1 string Getchinese ();    String Getword_class ();p rivate:string Chinese;    String 中文版; string Word_class;};    void Word::set (String e, String C, string wc) {english=e;    Chinese=c; WORD_CLASS=WC;} int Word::compare (string p)//English part with given wordstring comparison, equal to return, greater than return, less than return -1{if (english>p) return 1;    if (english==p) {return 0; } return-1;} String Word::getchinese () {return chinese;} String Word::getword_class () {return word_class;}    Class Dictionary{public:dictionary ();    void Searchword (string k);p rivate:int binseareh (int low, int. High, String k);    int wordsnum; Word words[8000];    Used to save the thesaurus};D ictionary::D ictionary () {string E,C,WC;    wordsnum=0;    Ifstream infile ("Dictionary.txt", ios::in);        if (!infile) {cerr<< "Open error" <<endl;    Exit (1);        } while (!infile.eof ()) {infile>>e>>c>>wc;        Words[wordsnum].set (E, C, WC);    ++wordsnum; } infile.close ();}    void Dictionary::searchword (string key) {int low=0,high=wordsnum-1;    int Index=binseareh (Low, high, key); if (index>=0) cout<<key<< "--->" <<words[index].getword_class () + "\ T" <<words[index].    Getchinese (); Else Cout<< "Do not find this word"; Cout<<endl<<endl;}    int Dictionary::binseareh (int low, int. High, string key) {int mid;        while (Low<=high) {mid= (low + high)/2;            if (Words[mid].compare (key) ==0) {return mid;//find successful return} if (Words[mid].compare (key) >0) High=mid-1; Continue in W[low. Find else low=mid+1 in mid-1]; Continue to find} return-1 in W[mid+1..high];    When Low>high indicates that the lookup interval is empty, the lookup fails}int main () {Dictionary dict;    String key;        Do {cout<< "Please enter the keywords to be queried (English), 0000 end:" <<endl;        cin>>key;        if (key!= "0000") {Dict.searchword (key);    }} while (key!= "0000"); cout<< "Welcome to use again!"    "<<endl<<endl; return 0;}


14th Week Item three electronic dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.