#include #include // remember to add this in order to open named file! #include #include using namespace std; #include "book.h" #include "bookshelf.h" /* * Given a string filename, open the file and read in the books. */ void Bookshelf::read_books(string filename) { int i; string line; ifstream fin(filename.c_str()); if (fin.fail()) { cout << "file open of " << filename << " failed." << endl; exit(2); } getline(fin, line); while (! fin.eof()) { getline(fin, line); if (line.length() == 0) continue; string title = line.substr(0,36); string author = line.substr(36,18); string subject = line.substr(54,line.length()-54); // need to trim strings before we make our Book object. // subtract characters from the end until it reaches a letter trim(title); trim(author); trim(subject); // Make a book object // Grow array of Books by 1 if (num == 0) { // start our array with 1 book (array of size 1) books = new Book[1]; books[0] = Book(title, author, subject); num++; // create one-element arrays of pointers to this Book[0] that we've got authorcatalog = new Book *[1]; authorcatalog[0] = &(books[0]); subjectcatalog = new Book *[1]; subjectcatalog[0] = &(books[0]); titlecatalog = new Book *[1]; titlecatalog[0] = &(books[0]); } else { // grow our array by 1 Book * bigger = new Book [num+1] ; for(i =0; i < num; i++) { // copy from old array into bigger array bigger[i] = books[i]; } num++; // better update our arrays of pointers before we delete what we were // pointing at! for(i=0; i< num-1; i++ ) { authorcatalog[i] = bigger + (authorcatalog[i] - books); subjectcatalog[i] = bigger + (subjectcatalog[i] - books); titlecatalog[i] = bigger + (titlecatalog[i] - books); } delete[] books; books = bigger; books[ num-1 ] = Book(title, author, subject); Book **newauthorcatalog = new Book*[num]; Book **newsubjectcatalog = new Book*[num]; Book **newtitlecatalog = new Book*[num]; for(i = 0 ; i < num - 1; i++){ newauthorcatalog[i] = authorcatalog[i]; newtitlecatalog[i] = titlecatalog[i]; newsubjectcatalog[i] = subjectcatalog[i]; } delete[]authorcatalog; delete[]titlecatalog; delete[]subjectcatalog; authorcatalog = newauthorcatalog; titlecatalog = newtitlecatalog; subjectcatalog = newsubjectcatalog; // insert books[num-1] into arrays of pointers at correct // alphabetic position // search author catalog for position at which to insert books[num-1] for(i=0; i < num-1; i++) { // does books[num-1] belong alphabetically before this element? if (books[num-1].get_author() < authorcatalog[i]->get_author()) { for (int j = num-2; j >= i; j--) { authorcatalog[j+1] = authorcatalog[j]; } authorcatalog[i] = & (books[num-1]); break; } } if (i==num-1) authorcatalog[i] = &(books[num-1]); // search title catalog for position at which to insert books[num-1] for(i=0; i < num-1; i++) { // does books[num-1] belong alphabetically before this element? if (books[num-1].get_title() < titlecatalog[i]->get_title()) { for (int j = num-2; j >= i; j--) { titlecatalog[j+1] = titlecatalog[j]; } titlecatalog[i] = & (books[num-1]); break; } } if (i==num-1) titlecatalog[i] = &(books[num-1]); // search subject catalog for position at which to insert books[num-1] for(i=0; i < num-1; i++) { // does books[num-1] belong alphabetically before this element? if (books[num-1].get_subject() < subjectcatalog[i]->get_subject()) { for (int j = num-2; j >= i; j--) { subjectcatalog[j+1] = subjectcatalog[j]; } subjectcatalog[i] = & (books[num-1]); break; } } if (i==num-1) subjectcatalog[i] = &(books[num-1]); } } fin.close(); cout << "Authors:" << endl ; for(i=0; i < num; i++) { cout << authorcatalog[i]->get_author() << endl; } cout << "Titles:" << endl ; for(i=0; i < num; i++) { cout << titlecatalog[i]->get_title() << endl; } cout << "Subject:" << endl; for(i=0; i < num; i++) { cout << subjectcatalog[i]->get_subject() << endl; } } void Bookshelf::trim(string &s) { while (s[s.size()-1] == ' ' ) { s.erase(s.size()-1, 1); } } Book *Bookshelf::search_title(string) { } Book *Bookshelf::search_author(string) { } Book *Bookshelf::*search_subject(string) { } void Bookshelf::insert_book(string,string,string) { } void Bookshelf::delete_book(string,string,string) { } void Bookshelf::delete_title(string) { } void Bookshelf::delete_author(string) { } void Bookshelf::delete_subject(string) { } void Bookshelf::save_books(string) { }