home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / queries.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-31  |  2.6 KB  |  64 lines

  1. // queries.cpp
  2. //
  3. // Example program uses the container query mechanisms.  Strings are
  4. // read in one line at a time and inserted into a linked list until an
  5. // 'x' (exit) is typed.  The list's "forEach" function is used to
  6. // print the strings in the list.  Then the program prompts for a letter.
  7. // The list's "query" function is used to build a sub list containing
  8. // all strings beginning with a letter less than the input letter.
  9. // That sub list is then printed using the sub list's "forEach" function.
  10.  
  11. #include <cm/include/cmlist.h>
  12. #include <cm/include/cmstring.h>
  13.  
  14. // Prototype query functions.
  15. Bool printObject(CmObject*, void*);           // Print specified object.
  16. Bool buildSubset(CmObject*, void*);           // Check specified object.
  17.  
  18. int main()                                    // Start main.
  19. {
  20.   CmLinkedList list;                          // Declare linked list.
  21.   CmString     input;                         // Input string.
  22.   Bool         finished = FALSE;              // Boolean data type.
  23.  
  24.   while (!finished)                           // While boolean is true.
  25.   {
  26.     cout << "Enter string (\"x\" when done): " << flush;
  27.     cin  >> input;                            // Read string input.
  28.  
  29.     int lnth = input.length();                // Get string length.
  30.     if (lnth == 1 && input[0] == 'x')         // If "x" then finished.
  31.       finished = TRUE;
  32.  
  33.     else if (lnth > 0)                        // Add string to tree.
  34.       list.add(new CmString(input));
  35.   }
  36.  
  37.   list.forEach(printObject, &cout);           // Print strings.
  38.   cout << endl;
  39.                                               // Prompt for string to
  40.   CmString match;                             // use in conditional.
  41.   cout << "Print strings starting with letter less than: " << flush;
  42.   cin  >> match;
  43.                                               // Build the subset.
  44.   CmContainer *subset = list.query(buildSubset, &match);
  45.  
  46.   subset->forEach(printObject, &cout);        // Print subset.
  47.   delete subset;
  48.   return 0;
  49. }
  50.  
  51. Bool printObject(CmObject* pObj, void* pData) // Print function.
  52. {
  53.   ostream& os = *((ostream*) pData);          // Cast data to stream.
  54.   os << *pObj << endl;                        // Print object.
  55.   return TRUE;                                // Continue.
  56. }
  57.  
  58. Bool buildSubset(CmObject* pObj, void* pData) // Condition function.
  59. {
  60.   if (!pObj->isA("CmString")) return FALSE;   // If not string, return.
  61.   CmString &str = *((CmString*) pData);       // Cast data to match string.
  62.   return ((CmString&)(*pObj) < str);          // Return condition.
  63. }
  64.