Problem:
/**copyright (c) 2015, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: project.cpp* Creator: wenqing * Completion Date: June 17, 2015 * Version number: v1.0** Problem Description: 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: 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 multi-file Organization * Program input: * Program output: * *
Code:
Head.h
#ifndef head_h_included#define head_h_included#include<string>using namespace std;//defines the term class Word{public: int Compare (string); The English part compares with the given string, equals return, is greater than return, is less than 1, is used for dichotomy lookup string Getchinese () //return Chinese explanation { return Chinese; } String Getword_class () //Returns the part of speech { return word_class; } void Set (string e, String C, string wc); Use private when reading data: string 中文版; String Chinese; String word_class;};/ /define Dictionary Classes Class Dictionary{public: Dictionary (); void Searchword (string k); Check the word private: int Binseareh (int low, int. High, String k); Binary method to find int wordsnum; Number of entries word words[8000];//Used to save Thesaurus}; #endif//head_h_included
Word_class.cpp
#include "head.h" void Word::set (string e, String C, string wc) { english=e; Chinese=c; WORD_CLASS=WC;} int Word::compare (string k) //For binary lookup { return english.compare (k);}
Dictionary_class.cpp
#include "head.h" #include <fstream> #include <iostream> #include <cstdlib>using namespace Std;d Ictionary::D ictionary () {string E,C,WC; wordsnum=0; Reads the data in the file into an array of objects Ifstream infile ("Dictionary.txt", ios::in); Open file as input if (!infile)//test successfully opened {cerr<< "Can ' t open the dictionary!" <<endl; Exit (1); } while (!infile.eof ()) {infile>>e>>c>>wc; (Words[wordsnum]). Set (E, C, WC); ++wordsnum; } infile.close ();} 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}void dictionary::searchword (string key) {int Low=0,high=wordsnuM-1; The initial value of the current search interval, the lower bound int Index=binseareh (low, high, key); if (index>=0) cout<<key<< "--->" <<words[index].getword_class () + "\ T" <<words[index]. Getchinese (); else cout<< "no such word"; Cout<<endl<<endl;}
Main.cpp
#include <iostream> #include "head.h" using namespace Std;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;}
Operation Result:
Summary of Knowledge points:
Binary Method Search
Construction of Class
Use of File streams
Learning experience:
To write a program like this, first of all, the class is defined well, the framework is generally conceived,
Then, when defining a class, each member function can be divided into several small member functions that complement the data members and member functions of each class.
Then carefully focus on solving every small problem. Like a constructor and a friend function. Be sure to pay attention to the details, or although the error is fixed, but still can not run the program.
What needs to be delved into is the dichotomy lookup, which constructs a compare function that returns -1,0,1 values to determine the range of comparisons.
Finally, it is the right way to copy. O (∩_∩) o haha ~
14th Week Project 3:oop Edition electronic Dictionary