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

  1. // strin.cpp
  2. //
  3. // Example program uses the string class and the binary tree class.
  4. // Strings are read one at a time and inserted into the binary tree
  5. // until an 'x' (exit) is typed.  An iterator is then created and
  6. // used to cycle through the tree printing each object.
  7.  
  8. #include <cm/include/cmbintr.h>
  9. #include <cm/include/cmstring.h>
  10.  
  11. int main()                                    // Start main.
  12. {
  13.   CmBinaryTree tree;                          // Binary tree container.
  14.   CmString     input;                         // Input string.
  15.   Bool         finished = FALSE;              // Boolean data type.
  16.  
  17.   while (!finished)                           // While boolean is true,
  18.   {
  19.     cout << "Enter string (\"x\" when done): " << flush;
  20.     cin  >> input;                            // Read string input.
  21.  
  22.     int lnth = input.length();                // Get string length.
  23.     if (lnth == 1 && input[0] == 'x')         // If "x" then finished.
  24.       finished = TRUE;
  25.  
  26.     else if (lnth > 0)                        // Add string to tree.
  27.       tree.add(new CmString(input));
  28.   }
  29.  
  30.   CmBinaryTreeIterator iterator(tree);        // Create an iterator.
  31.   while (iterator)                            // While iterator is ok,
  32.     cout << *iterator++ << endl;              // print each object.
  33.   return 0;
  34. }
  35.