00001
00002
00003 #include "DataBase.hpp"
00004
00005 DataBase::DataBase()
00006 {
00007 BST = new BinSearchTree();
00008 cmp = new double[4];
00009 NbCmp = new double[4];
00010
00011 for(int k = 0; k < 4; k++)
00012 {
00013 cmp[k] = 0;
00014 NbCmp[k] = 0;
00015 }
00016 }
00017
00018 DataBase::~DataBase()
00019 {
00020 if ( _current != NULL )
00021 delete _current;
00022
00023 delete _db;
00024 delete BST;
00025
00026 delete [] cmp;
00027 delete [] NbCmp;
00028
00029 }
00030
00031 int DataBase::read(const char* datafile)
00032 {
00033 FILE* record = NULL;
00034 char c = 0;
00035 char command[80];
00036 int p = 0;
00037
00038
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) )
00047 {
00048 c = fgetc(record);
00049
00050 if ( c != 13 && c != 10 )
00051 {
00052 command[p] = c;
00053 p++;
00054 command[p] = '\0';
00055 }
00056 else if ( p > 0 )
00057 {
00058 command[p] = '\0';
00059
00060
00061
00062 p = 0;
00063 _db = new DBase( command );
00064
00065 switch ( command[0] )
00066 {
00067 case 'I': cmd = "Insert";
00068 if ( findID( 0 ) < 0 ) {
00069 BST->insert( _db );
00070 } else {
00071 cout << msgI_IDexist << endl;
00072 }
00073 break;
00074
00075 case 'S': cmd = "Search";
00076 if ( findID( 1 ) < 0 ) {
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 ) {
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();
00103 }
00104 break;
00105
00106 case 'D': cmd = "Delete";
00107 if ( findID( 3 ) < 0 ) {
00108 cout << msgD_IDnotFound << endl;
00109 } else {
00110 BST->remove();
00111 }
00112 break;
00113
00114 default :
00115 cout << command << endl;
00116 cout << msgInvalidOp << endl;
00117
00118 }
00119
00120
00121
00122
00123
00124
00125 }
00126
00127 }
00128
00129
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 ) {
00153 double comp = 0;
00154
00155 pos = BST->findID( *_db->_id );
00156 comp = pos;
00157
00158
00159
00160 comp = BST->search();
00161
00162 _current = BST->getValue();
00163
00164
00165
00166 if ( index >= 0 ) {
00167 cmp[index] += comp;
00168 NbCmp[index]++;
00169 }
00170
00171 return pos;
00172 }