|
// file DShelf006.cpp // authors AOU // date 2006.02.20 /* ----------------------------------------------- 2006.02.20 Algorithm for insertAtLeft ----------------------------------------------- 2006.02.17 New insertAtLeft ..started test_insertAtLeft displaySimple ----------------------------------------------- CDShelf(void); //constructorDefault void test_constructorDefault(void); void displayDetailed(void); ----------------------------------------------- */ /* for a book of width 5 and id 1: 15 CBook books[MAX_SHELF_WIDTH] 11111 int a[MAX_SHELF_WIDTH] function | int a[MAX_SHELF_WIDTH] vs CBook books[MAX_SHELF_WIDTH] ------------------------------------------------------------------------------ constructorDefault | | isValid | |better constructorRandom | |better constructorWidth | | constructorFragmented| |better insertAtLeft | |better displayDetailed | | displaySimple |better | displayBrief | | isEmpty | | randomBook | |better randomBookPointer | |better search | |better remove_byID | |better remove_byPointer | |better reclaimSpace |better | reclaimableSpace | |better ------------------------------------------------------------------------------ */ #include <iostream> using namespace std; // Constants int const UNDEFINED = -9; int const MIN_SHELF_WIDTH = 5; int const MAX_SHELF_WIDTH = 20;//100; int const MIN_BOOK_ID = 1; int const MAX_BOOK_ID = 9;//100; int const TEST_COUNT = 10; class CBook { private: int id; int width; public: CBook(void) { id = 0; width = 0; }; friend class CDShelf; }; class CDShelf { private: int width; // width of the bookshelf int count; // number of books on shelf int widthOccupied; // total width of book on shelf // should it include empty holes? // should not include empty holes // instead use spaceAvailable CBook books[MAX_SHELF_WIDTH]; public: CDShelf(void); //constructorDefault void displayDetailed(void); bool insertAtLeft(int id, int width); void displaySimple(void); /* isValid constructorRandom constructorWidth constructorFragmented displayBrief isEmpty randomBook randomBookPointer search remove_byID remove_byPointer reclaimSpace reclaimableSpace */ }; void test_constructorDefault(void); void test_insertAtLeft(void); // test_isValid // test_constructorRandom // test_constructorWidth // test_constructorFragmented // test_insertAtLeft // test_displayDetailed // test_displaySimple // test_displayBrief // test_isEmpty // test_randomBook // test_randomBookPointer // test_search // test_remove_byID // test_remove_byPointer // test_reclaimSpace // test_reclaimableSpace void main(void) { cout << "Hello\n"; //test_constructorDefault(); test_insertAtLeft(); } /* Algorithm0: Possibilities: (1) Empty shelf (2) Invalid book (3) Duplicate book (4) Full shelf We will have to drop one or more books (5) Shelf is not full and not empty We may have to move one or more books We may have to drop one or more books */ void test_insertAtLeft(void) { CDShelf myShelf; myShelf.displayDetailed(); myShelf.displaySimple(); myShelf.insertAtLeft(3, 5); cout << "After myShelf.insertAtLeft(3, 5);\n"; myShelf.displayDetailed(); myShelf.displaySimple(); } bool CDShelf::insertAtLeft(int id, int width) { this->books[0].id=id; this->books[0].width=width; this->count++; this->widthOccupied += width; return true; } /* void CDShelf::displaySimple(void) Algorithm0: for each book b do the following display id width times Algorithm1: for each book b do the following do the following b.width times display b.id Algorithm2: for i=0 to count-1 do the following for j=0 to b[i].width-1 do the following display b[i].id */ void CDShelf::displaySimple(void) { cout << "width = " << width << endl; cout << "count = " << count << endl; cout << "widthOccupied = " << widthOccupied << endl; for (int i=0; i<this->count; i++) for (int j=0; j<books[i].width; j++) cout << books[i].id; cout << endl; } void CDShelf::displayDetailed(void) { cout << "width = " << width << endl; cout << "count = " << count << endl; cout << "widthOccupied = " << widthOccupied << endl; for (int i=0; i<MAX_SHELF_WIDTH; i++) { cout << books[i].id ; cout << books[i].width << ' '; } cout << endl; } void test_constructorDefault(void) { CDShelf myShelf; myShelf.displayDetailed(); myShelf.displaySimple(); } CDShelf::CDShelf(void) { this->width = MIN_SHELF_WIDTH; this->count = 0; this->widthOccupied = 0; for (int i=0; i<MAX_SHELF_WIDTH; i++) { books[i].id = 0; books[i].width = 0; } } |