17th Week Item 7-Electronic dictionary structure version

Source: Internet
Author: User

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

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.