home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 7 < prev    next >
Encoding:
Text File  |  1992-04-01  |  4.8 KB  |  180 lines

  1. /*
  2.  * Example 4: class BinaryTree, storing and retrieving collectable strings
  3.  *
  4.  * $Header:   E:/vcs/toolexam/example4.cpv   1.1   01 Apr 1992 16:51:10   keffer  $
  5.  *
  6.  ****************************************************************************
  7.  *
  8.  * Rogue Wave Software, Inc.
  9.  * P.O. Box 2328
  10.  * Corvallis, OR 97339
  11.  * Voice: (503) 754-2311    FAX: (503) 757-7350
  12.  *
  13.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  14.  * protection under the laws of the United States and other countries.
  15.  *
  16.  ***************************************************************************
  17.  *
  18.  * $Log:   E:/vcs/toolexam/example4.cpv  $
  19.  * 
  20.  *    Rev 1.1   01 Apr 1992 16:51:10   keffer
  21.  * Now includes <rw/xxx.h>
  22.  * 
  23.  */
  24.  
  25.  
  26. // Declarations for Binary Tree class:
  27. #include <rw/bintree.h>
  28.  
  29. /*
  30.  * Declarations for class RWCollectableString.  
  31.  * This class inherits class RWCString and class RWCollectable.  
  32.  * Class RWCollectable has a virtual function compareTo() that is redefined 
  33.  * by class RWCollectableString.  This function is used to order the 
  34.  * RWCollectableStrings in the binary tree.
  35.  */
  36. #include <rw/collstr.h>
  37. #include <rw/rstream.h>
  38.  
  39. /*
  40.  * "STARTWRAP" and "ENDWRAP" are macros that allow inclusion of ANSI-C header
  41.  * files using standard "C" linkage.  They are necessary for C++ compilers that
  42.  * do not supply their own special "C++" header files and, instead, rely on
  43.  * standard C header files.
  44.  */
  45. STARTWRAP
  46. #include <stdlib.h>
  47. ENDWRAP
  48.  
  49. /*
  50.  * The following complex conditional inclusion owes its existence to
  51.  * everyone going their own way, defining "standard" include files.
  52.  */
  53.  
  54. #ifdef HAS_IOSTREAMS
  55. #  ifdef __ZTC__
  56. #    include <fstream.hpp>
  57. #  else
  58. #    ifdef __GLOCK__
  59. #      include <fstream.hxx>
  60. #    else
  61. #      include <fstream.h>
  62. #    endif
  63. # endif
  64. #endif
  65.  
  66. /*
  67.  * A pointer to this function will be handed to RWBinaryTree::apply() 
  68.  * to enumerate the members of the collection.
  69.  */
  70. static void
  71. printStrings(RWCollectable* c, void*)
  72. {
  73.   /*
  74.    * Cast the RWCollectable pointer to a RWCollectableString pointer,
  75.    * then dereference and print.  RWCollectableString inherits
  76.    * its ability to be printed from its base class RWCString.
  77.    */
  78.   cout << * (RWCollectableString*) c << " ";
  79. }
  80.  
  81.  
  82. main()
  83. {
  84.   RWBinaryTree        B;
  85.   RWCollectableString    aWord;
  86.   RWCollectableString*    pWord;
  87.   int i = 0;
  88.  
  89.   cout << "***** Example using a Binary Tree (SortedCollection) ******\n";
  90.   
  91. #if HAS_IOSTREAMS
  92.   ifstream inputFile("textfile.in", ios::in);
  93.   if(!inputFile){
  94.     cerr << "Cannot open file textfile.in.\n";
  95.     exit(1);
  96.   }
  97. #else
  98.   filebuf fb;
  99.   if ( fb.open("textfile.in", input) == 0 ){
  100.     cerr << "Cannot open file textfile.in.\n";
  101.     exit(1);
  102.   }
  103.   istream inputFile(&fb);    // Connect to istream.
  104. #endif
  105.  
  106.   cout << "Reading from file \"textfile.in\"...\n";
  107.  
  108.   while(1){
  109.     pWord = new RWCollectableString;
  110.     if(!pWord) {
  111.       cerr << "Out of memory.\n";
  112.       break;
  113.     }
  114.     if( !(inputFile >> *pWord).good() ) break;
  115.     B.insert(pWord);
  116.     cout << i++ << " " << *pWord << NL;
  117.   }
  118.  
  119.   delete pWord;    // Delete the final, unused, word.
  120.  
  121.   cout << "done.\n\nA total of " << i << " words were read.\n";
  122.   cout << "Contents of the tree are:\n\n";
  123.   B.apply(printStrings, 0);    // Uses global function defined above.
  124.   cout << "\n\n";
  125.  
  126.   // Loop to do various things to the table:
  127.   char option;
  128.   
  129.   while(1){
  130.  
  131.     cout << "(i)nsert (s)earch (d)elete (c)lear (l)ist e(x)it:\t";
  132.  
  133.     // Check for EOF or terminating character:
  134.     if ( !(cin >> option).good() || option=='x' || option=='X' ) break;
  135.  
  136.     switch ( option ) {
  137.     case 'i':        // Insert a word in tree.
  138.     case 'I':
  139.       cout << "Enter word: ";
  140.       pWord = new RWCollectableString;
  141.       if(pWord){
  142.         if( (cin >> *pWord).good() ) B.insert(pWord);    // Check for failed stream
  143.         else delete pWord;
  144.       }
  145.       else cerr << "Out of memory.\n";
  146.       break;
  147.     case 's':        // Find the number of occurrences of a word.
  148.     case 'S':
  149.       cout << "Enter word:\t";
  150.       cin >> aWord;
  151.       cout << B.occurrencesOf(&aWord) << " occurrences of word in tree.\n";
  152.       break;
  153.     case 'd':       // Remove a word.
  154.     case 'D':
  155.       cout << "Enter word:\t";
  156.       cin >> aWord;
  157.       B.removeAndDestroy(&aWord);
  158.       break;
  159.     case 'c':
  160.     case 'C':        // Both clear AND destroy the contents
  161.       B.clearAndDestroy();
  162.       break;
  163.     case 'l':
  164.     case 'L':        // List the contents of the tree, in order.
  165.       cout << B.entries() << " entries in tree:\n";
  166.       B.apply(printStrings,0);
  167.       cout << "\n\n";
  168.       break;
  169.     default:
  170.       cerr << "Unrecognized.\n";
  171.     }     // End switch
  172.   }    // End while
  173.  
  174.   cout << NL;        // To pretty things up.
  175.  
  176.   B.clearAndDestroy();    // Not really essential, but good style
  177.  
  178.   return 0;
  179. }
  180.