/* Filename: diction.cpp Author: Br. David Carlson Date: July 14, 1999 Last Revised: December 23, 2001 This interactive program creates a map (table), where each entry is a pair containing a word and its definition. Thus the map is a small dictionary. The program then allows the user to repeatedly look up the meaning of words. In the code, change CTRL z to CTRL d for Linux. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include #include using namespace std; const int LineMax = 72; const int NULLCHAR = '\0'; typedef char LineType[LineMax]; /* To keep the example short, we will set up LineClass right here. LineClass objects each contain a short C-style string. The idea is that such an object contains a line of text. The class basically provides object-oriented packaging for a C-style string. */ class LineClass { private: LineType Info; public: LineClass(void); LineClass(LineType Str); void GetLine(LineType Line) const; }; /* Given: Nothing. Task: This is the default constructor. It creates a LineClass object containing the empty string. Return: Nothing directly, but the implicit object is created. */ LineClass::LineClass(void) { Info[0] = NULLCHAR; } /* Given: Nothing. Task: This constructor creates a LineClass object containing the string Str. Return: Nothing directly, but the implicit object is created. */ LineClass::LineClass(LineType Str) { strcpy(Info, Str); } /* Given: Nothing. Task: To get the string contained in the implicit LineClass object. Return: Line A copy of the string contained in the implicit object. */ void LineClass::GetLine(LineType Line) const { strcpy(Line, Info); } /* Given: LHS A LineClass object (the left side of the comparison). RHS A LineClass object (the right side of the comparison). Task: To compare the strings containe in LHS and RHS. Return: In the function name, it returns true if the LHS string is less than the RHS string, false otherwise. */ bool operator<(LineClass LHS, LineClass RHS) { LineType Left, Right; LHS.GetLine(Left); RHS.GetLine(Right); if (strcmp(Left, Right) < 0) return true; else return false; } // Function prototypes: void LoadData(map & Dictionary); void Lookup(LineType Target, map & Dictionary); int main(void) { map Dictionary; LineType Target; LoadData(Dictionary); // Change to CTRL d for Linux: cout << "Enter word to look for (or CTRL z to quit): "; cin >> Target; while (! cin.fail()) { Lookup(Target, Dictionary); // Change to CTRL d for Linux: cout << "Enter word to look for (or CTRL z to quit): "; cin >> Target; } return 0; } /* Given: Target The word to look up. Dictionary The map containing the dictionary data. Task: To look up Target in the Dictionary map and print the meaning of Target if it was found, or a "not found" message otherwise. Return: Nothing. */ void Lookup(LineType Target, map & Dictionary) { map::iterator p; LineType Meaning; p = Dictionary.find(LineClass(Target)); if (p != Dictionary.end()) { p->second.GetLine(Meaning); cout << "Meaning is: " << Meaning << endl; } else cout << "Word not found" << endl; } /* Given: Dictionary A map. Task: To add suitable data to Dictionary. Return: Dictionary The updated map. */ void LoadData(map & Dictionary) { // Note the 2 different ways of creating a pair to insert: Dictionary.insert(pair(LineClass("hot"), LineClass("having a high temperature"))); Dictionary.insert(make_pair(LineClass("cold"), LineClass("having a low temperature"))); Dictionary.insert(make_pair(LineClass("jog"), LineClass("run slowly"))); Dictionary.insert(make_pair(LineClass("intelligent"), LineClass("smart"))); Dictionary.insert(make_pair(LineClass("register"), LineClass("a high-speed storage location on a computer's CPU chip"))); }