#include using namespace std; #include "apvector.h" #include "apstring.h" struct Book { apstring title; apstring author; int lowAge; int highAge; Book() : title("none"), author("none"), lowAge(0), highAge(0) { } Book(const apstring& t, const apstring& a, int low, int high) : title(t), author(a), lowAge(low), highAge(high) { } }; bool LessThan(const Book& lhs, const Book& rhs) { return lhs.lowAge < rhs.lowAge || ( (lhs.lowAge == rhs.lowAge) && (lhs.highAge < rhs.highAge)); } ostream& operator << (ostream& out, const Book& book) { out << book.title << " " << book.author << "\t" << book.lowAge << " " << book.highAge; return out; } class BookList { public: BookList(); void InsertOne(const Book& bk); void InsertMany(const apvector& second); void Print() const; private: apvector myList; int myCount; }; BookList::BookList() : myList(0), myCount(0) { } void BookList::InsertOne(const Book& bk) { myCount++; if (myCount >= myList.length()) { // be safe if initially zero myList.resize(2*myList.length() + 1); } int k = myCount - 1; while (0 < k && LessThan(bk, myList[k-1])) { myList[k] = myList[k-1]; k--; } myList[k] = bk; } void BookList::InsertMany(const apvector& second) { int k; for(k=0; k < second.length(); k++) { InsertOne(second[k]); } } void BookList::Print() const { int k; for(k=0; k < myCount; k++) { cout << k << "\t" << myList[k] << endl; } } int main() { BookList b; apvector books(3); b.InsertOne(Book("Madeline", "Bemelmans", 3,8)); b.InsertOne(Book("The Lorax", "Seuss", 3,10)); b.InsertOne(Book("Harry Potter and the Sorcerer's Stone", "Rowling", 9,99)); b.InsertOne(Book("Holes", "Sacher", 12,18)); b.InsertOne(Book("I Know Why the Caged Bird Sings", "Angelou", 16,99)); // b.InsertOne(Book("Little House on the Prairie", "Wilder", 8,12)); b.Print(); books[0] = Book("The Phantom Tollbooth", "Juster",9,12); books[1] = Book("Invisible Man", "Ellison", 15,99); books[2] = Book("Charlotte's Web", "White", 8,12); b.InsertMany(books); cout << " --------" << endl; b.Print(); return 0; }