Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/BST/DataBase.cpp

Go to the documentation of this file.
00001 // DataBase.cpp - Binary Search Tree DataBase Class Implementation
00002 
00003 #include "DataBase.hpp"
00004 
00005 DataBase::DataBase()            // Default Constructor
00006 {
00007   BST   = new BinSearchTree();  // Create a Binary Search Tree
00008   cmp   = new double[4];        // Create an array for comparisons
00009   NbCmp = new double[4];        // informations for all the commands.
00010 
00011   for(int k = 0; k < 4; k++)    // Reset all the elements in the array,
00012   {
00013     cmp[k]   = 0;               // else we get "garbage += new_value;"
00014     NbCmp[k] = 0;
00015   }
00016 }
00017 
00018 DataBase::~DataBase()           // Destructor
00019 {
00020   if ( _current != NULL ) 
00021     delete _current;
00022 
00023   delete _db;                   // Delete pointer to objects
00024   delete BST;
00025 
00026   delete [] cmp;                // Delete comparison tables
00027   delete [] NbCmp;
00028 
00029 }
00030 
00031 int DataBase::read(const char* datafile)
00032 {
00033   FILE* record = NULL;          // Initialize variable
00034   char  c = 0;
00035   char  command[80];
00036   int   p = 0;
00037 
00038   // Open a file in read-only text mode, if the file exist.
00039   if ( (record = fopen(datafile, "rt") ) == NULL)
00040   {
00041     cout << "Cannot open the data file [" << datafile;
00042     cout << "] in the current directory." << endl;
00043     return 1;
00044   }
00045 
00046   while ( !feof(record) )       // Loop until end of file
00047   {
00048     c =  fgetc(record);         // Read a Byte from the record file
00049   
00050     if ( c != 13 && c != 10 )   // Scanning for CR, LF ASCII Code 
00051     {              
00052       command[p] = c;           // Add the new char to the array.
00053       p++;
00054       command[p] = '\0';
00055     } 
00056     else if ( p > 0 )           // We reached a end of line byte.
00057     {
00058       command[p] = '\0';
00059 
00060 //      cout << command << endl;
00061 
00062            p = 0;    // Reset the counter for the next file read
00063            _db = new DBase( command );
00064 
00065       switch ( command[0] ) 
00066       {
00067         case 'I': cmd = "Insert";
00068             if ( findID( 0 ) < 0 ) {        // Insert
00069               BST->insert( _db );        // ID was not Found
00070             } else {
00071               cout << msgI_IDexist  << endl;  // ID was Found
00072             }
00073           break;
00074 
00075         case 'S': cmd = "Search";
00076             if ( findID( 1 ) < 0 ) {        // Search
00077               cout << command << endl;
00078               cout << msgS_IDnotFound << endl;
00079             } else {
00080               cout << msgS_IDFound << endl;
00081               _current->display();
00082             }
00083           break;
00084           
00085         case 'C': cmd = "Change";
00086             if ( findID( 2 ) < 0 ) {        // Change
00087               cout << command << endl;
00088               cout << msgC_IDnotFound << endl;
00089             } else {
00090               cout << "The following element:" << endl;
00091               _current->display();
00092 
00093               if ( _db->_name->filled() ) {
00094                  _current->_name = _db->_name;
00095               }
00096 
00097               if ( _db->_tel->filled() ) {
00098                  _current->_tel = _db->_tel;
00099               }
00100                   
00101               cout << "was changed to the following:" << endl;
00102               _current->display();      // Show modification
00103             }
00104           break;
00105  
00106         case 'D':  cmd = "Delete";
00107             if ( findID( 3 ) < 0 ) {        // Delete
00108               cout << msgD_IDnotFound << endl;
00109             } else {
00110               BST->remove();
00111             }
00112           break;
00113         
00114         default :                  // Invalid command
00115             cout << command     << endl;
00116             cout << msgInvalidOp << endl;
00117 
00118       } // End of switch
00119 
00120         // Display all the BST Step by Step to debug:
00121 
00122         //  findID( -10 );
00123         //  BST->display();
00124 
00125     } // End of line
00126 
00127   } // End of file while loop
00128 
00129   // Show the results
00130   cout << endl << endl;
00131    cout << msgTotalCmp << " Insert was " << cmp[0];
00132   cout << " with " << NbCmp[0] << " Insert commands.\n";
00133   cout << msgAvgCmp   << " Insert was " << (cmp[0] / NbCmp[0]) << ".\n";
00134 
00135   cout << msgTotalCmp << " Search was " << cmp[1];
00136   cout << " with " << NbCmp[1] << " Search commands.\n";
00137   cout << msgAvgCmp   << " Search was " << (cmp[1] / NbCmp[1]) << ".\n";
00138 
00139   cout << msgTotalCmp << " Change was " << cmp[2];
00140   cout << " with " << NbCmp[2] << " Change commands.\n";
00141   cout << msgAvgCmp   << " Change was " << (cmp[2] / NbCmp[2]) << ".\n";
00142  
00143   cout << msgTotalCmp << " Delete was " << cmp[3];
00144   cout << " with " << NbCmp[3] << " Delete commands.\n";
00145   cout << msgAvgCmp   << " Delete was " << (cmp[3] / NbCmp[3]) << ".\n\n";
00146 
00147   fclose(record);    
00148 
00149   return 0;
00150 }
00151 
00152 double DataBase::findID( int index ) { // Find an ID and record
00153   double comp = 0;             // the number of comparisons needed
00154 
00155   pos  = BST->findID( *_db->_id );
00156   comp = pos;
00157 
00158 // cout << msgCompare << cmd << index << " was "; // << endl;
00159 
00160   comp = BST->search();
00161 
00162   _current = BST->getValue();
00163   
00164 //  cout << comp << " out of " << BST->length() << endl;
00165 
00166   if ( index >= 0 ) {
00167     cmp[index] += comp;
00168     NbCmp[index]++;
00169   }
00170 
00171   return pos;
00172 }

Generated on Sun Oct 14 18:46:17 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001