/* * ------------------------------------------------------------------------ * CS2S03/SE2S03, September 2011 * Assignment 2, Question 2 * File: PE0214.cpp * ------------------------------------------------------------------------ * * This program defines a prototype library database capable of storing * information of the card catalog system of a library. It also * impletements function SearchBySubject() that takes as parameters the * library database and a subject string. For each book in the library that * lists the subject string as one of its subject headings, SearchSubject * displays the title, the name of the first author, and the Library * Congress catalog number of the book. * * ------------------------------------------------------------------------ */ #include "genlib.h" #include "simpio.h" #include /* constant definitions */ const int MAX_N_BOOKS = 100; const int MAX_N_AUTHORS = 5; const int MAX_N_SUBJECTS = 5; /* data structures */ struct bookT { string title; string authors[MAX_N_AUTHORS]; string catalogNum; string subjects[MAX_N_SUBJECTS]; string publisher; int year; bool isCirculating; }; struct LibraryT { bookT* books[MAX_N_BOOKS]; int nBooks; }; /* Private function prototypes */ void ReadCard(bookT* book); void SearchBySubject(LibraryT data, string subject); void Display(bookT book); /* Main program */ int main() { LibraryT libdata; bookT* bookptr; libdata.nBooks = 0; /* set up a small library database */ libdata.books[libdata.nBooks] = new bookT; if (libdata.books[libdata.nBooks] == NULL) { Error("Fail to allocate memory."); } bookptr = libdata.books[libdata.nBooks]; bookptr->title = "The C Programming Language"; bookptr->authors[0] = "Brian W. Kernighan"; bookptr->authors[1] = "Dennis M. Ritchie"; bookptr->authors[2] = ""; bookptr->catalogNum = "Q76.73.C15K47"; bookptr->subjects[0] = "computer"; bookptr->subjects[1] = "program language"; bookptr->subjects[2] = ""; bookptr->publisher = "Prentice-Hall"; bookptr->year = 1988; bookptr->isCirculating = "y"; libdata.nBooks++; libdata.books[libdata.nBooks] = new bookT; if (libdata.books[libdata.nBooks] == NULL) { Error("Fail to allocate memory."); } bookptr = libdata.books[libdata.nBooks]; bookptr->title = "Relativity"; bookptr->authors[0] = "Albert Einstein"; bookptr->authors[1] = ""; bookptr->catalogNum = "QC6.W48"; bookptr->subjects[0] = "physics"; bookptr->subjects[1] = "relativity"; bookptr->subjects[2] = ""; bookptr->publisher = "Random House"; bookptr->year = 1961; bookptr->isCirculating = "y"; libdata.nBooks++; libdata.books[libdata.nBooks] = new bookT; if (libdata.books[libdata.nBooks] == NULL) { Error("Fail to allocate memory."); } bookptr = libdata.books[libdata.nBooks]; bookptr->title = "Journey to the Center of the Earth"; bookptr->authors[0] = "Jules Verne"; bookptr->authors[1] = ""; bookptr->catalogNum = "PQ2469.V5E5"; bookptr->subjects[0] = "fiction"; bookptr->subjects[1] = ""; bookptr->publisher = "Dover"; bookptr->year = 2005; bookptr->isCirculating = "y"; libdata.nBooks++; /* read catalog card information of books and store it in library data base */ cout << "Number of books you would like to enter, up to "; cout << MAX_N_BOOKS - libdata.nBooks << " books: "; int n = GetInteger(); cout << endl; for (int i = 0; i < n; i++) { libdata.books[libdata.nBooks] = new bookT; if (libdata.books[libdata.nBooks] == NULL) { Error("Fail to allocate memory."); } ReadCard(libdata.books[libdata.nBooks]); libdata.nBooks++; cout << endl; } cout << endl << "Finished entering books." << endl << endl; cout << "Search by subject ..." << endl << endl; cout << "Enter a subject: "; string sub = GetLine(); cout << endl; SearchBySubject(&libdata, sub); return 0; } /* Function: ReadCard() * Usage: ReadCard(bookptr) * --------------------------------------------------------------------- * * This function reads catalog card information of a book from the user. * * --------------------------------------------------------------------- */ void ReadCard(bookT* bookptr) { string line; cout << "Title: "; bookptr->title = GetLine(); cout << "Up to five authors, hit return to end." << endl; for (int j = 0; j < MAX_N_AUTHORS; j++) { cout << "Author" << j + 1 << " : "; line = GetLine(); (bookptr->authors)[j] = line; if (line == "") break; } cout << "Library of Congress catalog number: "; bookptr->catalogNum = GetLine(); cout << "Up to five subjects, hit return to end." << endl; for (int j = 0; j < MAX_N_SUBJECTS; j++) { cout << "Subject" << j + 1 << " : "; line = GetLine(); (bookptr->subjects)[j] = line; if (line == "") break; } cout << "Publisher: "; bookptr->publisher = GetLine(); cout << "Year of publication: "; bookptr->year = GetInteger(); cout << "Is circulating? (y or n): "; bookptr->isCirculating = (GetLine() == "y"); } /* * Function: SearchBySubject() * Usage: SearchSubject(libdata, subject); * ------------------------------------------------------------------------ * * This function searches the libdata for books with subject. For each book * that lists the subject as one of its subject headings, this function * displays its * title * the first author * the Library of Congress catalog number * * ------------------------------------------------------------------------ */ void SearchBySubject(LibraryT* libdata, string subject) { bookT book; string sub; bool found = false; for (int i = 0; i < libdata->nBooks; i++) { if (libdata->books[i] == NULL) Error("Library database is corrupted."); book = *(libdata->books[i]); /* check the subjects */ for (int j = 0; j < MAX_N_SUBJECTS; j++) { sub = book.subjects[j]; if (sub == "") break; if (sub == subject) { found = true; cout << "Found a book with subject heading " << subject << endl; Display(book); cout << endl; } } } if (!found) { cout << "No book found." << endl; } } /* * Function Display() * Usage Display(book) * ------------------------------------------- * * This function displays the book information * title * the first author * the Library of Congress catalog number * * ------------------------------------------- */ void Display(bookT book) { cout << "title: " << book.title << endl; cout << "First author: " << (book.authors)[0] << endl; cout << "Library of Congress catalog number: " << book.catalogNum << endl; }