/* CDDB.cpp * * Sample CD data base program * * Bruce M. Bolden * November 17, 1997 * * http://www.cs.uidaho.edu/~bruceb/ * --------------------------------------------------------- */ #include #include #include #include #include #include // for g++ const int FALSE = 0; const int TRUE = 1; // CD Info record structure const int MAX_ARTIST_LENGTH = 15; const int MAX_TITLE_LENGTH = 40; struct st_CD_Info { char artist[MAX_ARTIST_LENGTH]; char title[MAX_TITLE_LENGTH]; double cost; }; typedef struct st_CD_Info CDInfo; // The data base const int MAX_DB_ITEMS = 20; CDInfo currDB[MAX_DB_ITEMS]; // function prototypes int ReadCDDB( char *dbName, const int maxCDs ); void ShowCDDB( ofstream& outFile, const int nCDs ); void SortByArtist( const int nCDs ); void SwapCDRecords( const int i, const int j ); void SwapString( char *s1, char *s2 ); void SwapDouble( double& d1, double& d2 ); int SearchByArtist( const char *artistName, const int nMax ); void ShowArtistSearchResult( ofstream& o, const char *aName, const int i ); int main() { ofstream outFile( "CDDB.out", ios::out ); if( !outFile ) { cerr << "Unable to open: \"" << "CDDB.out" << "\"" << endl; exit( -1 ); } int nCDs = ReadCDDB( "CDDB.dat", MAX_DB_ITEMS ); cout << "Read " << nCDs << " records" << endl; char *artists[3] = { "Beach Boys", "Who", "Gershwin" }; if( nCDs > 0 ) { outFile << "Initial contents of CD data base:\n" << endl; ShowCDDB( outFile, nCDs ); SortByArtist( nCDs ); outFile << "CD data base contents after sorting:\n" << endl; ShowCDDB( outFile, nCDs ); // Test searching outFile << " ----------------------------------- " << endl; outFile << "\nTest of limited search capabilities\n" << endl; int iFound; // Index returned by search operation //char *artists[3] = { "Beach Boys", "Who", "Gershwin" }; for( int i = 0 ; i < 3 ; ++i ) { iFound = SearchByArtist( artists[i], nCDs ); ShowArtistSearchResult( outFile, artists[i], iFound ); } } outFile.close(); return 0; } /* Read CD Database */ int ReadCDDB( char *dbName, const int maxCDs ) { // Open data base file ifstream inCDFile( dbName ); if( !inCDFile ) { cerr << "Unable to open: \"" << dbName << "\"" << endl; return -1; } // Read data base file int i = 0; int done = FALSE; const int MAX_LINE = 80; char tmpLine[MAX_LINE]; while( !done && i < maxCDs ) { inCDFile.getline( currDB[i].artist, MAX_ARTIST_LENGTH ); inCDFile.getline( currDB[i].title, MAX_TITLE_LENGTH ); inCDFile >> currDB[i].cost; inCDFile.getline( tmpLine, MAX_LINE ); cout << currDB[i].artist << endl; cout << currDB[i].title << endl; cout << currDB[i].cost << endl; cout << "Artist string length: " << strlen(currDB[i].artist) << endl; // g++ is a bit different... if( strlen(currDB[i].artist) < 1 ) { ++i; done = TRUE; break; } ++i; if( !inCDFile.good() ) // original Metrowerks done = TRUE; if( !inCDFile ) // g++ done = TRUE; } i--; // one too far inCDFile.close(); return i; } /* ShowCDDB * * Show the contents of the CD data base */ void ShowCDDB( ofstream& o, const int nCDs ) { for( int i = 0 ; i < nCDs ; i++ ) { o << currDB[i].artist << "\t"; o << currDB[i].title; o << endl; } o << "---------------------\n" << endl; } /* SortByArtist * * Perform a bubble sort on the CD data base by artist. */ void SortByArtist( const int nCDs ) { cout << "SortByArtist() not implemented yet\n" << endl; } /* SwapCDRecords * * Swap the contents of two CD records. */ void SwapCDRecords( const int i, const int j ) { CDInfo tmpRec; tmpRec = currDB[i]; currDB[i] = currDB[j]; currDB[j] = tmpRec; /* Not all compilers allow struct assignments/copying SwapString( currDB[i].artist, currDB[j].artist ); SwapString( currDB[i].title, currDB[j].title ); SwapDouble( currDB[i].cost, currDB[j].cost ); */ } /* SwapString * * Swap two strings. */ void SwapString( char *s1, char *s2 ) { char tmpStr[MAX_TITLE_LENGTH]; // longest string strcpy( tmpStr, s1 ); strcpy( s1, s2 ); strcpy( s2, tmpStr ); } /* SwapDouble * * Swap two doubles. */ void SwapDouble( double& d1, double& d2 ) { double dTmp; dTmp = d1; d1 = d2; d2 = dTmp; } /* SearchByArtist * * Searches for the value, artistName, in the CD data base. * If successful, the index is returned. * If unsuccessful, -1 is returned. */ int SearchByArtist( const char *artistName, const int nMax ) { // Simple linear search---should be replaced! for( int i = 0 ; i < nMax ; i++ ) { if( strcmp( currDB[i].artist, artistName) == 0 ) return i; } return -1; } /* ShowArtistSearchResult * * Show results of a search for a given artistName */ void ShowArtistSearchResult( ofstream& o, const char *aName, const int i ) { o << aName; if( i < 0 ) { o << " not found"; } else { o << " found at location: " << i; } o << endl; }