1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| class Book { private: string title; string author; string isbn; int pages; bool available; public: Book(const string& t, const string& a, const string& i, int p) : title(t), author(a), isbn(i), pages(p), available(true) {} string getTitle() const { return title; } string getAuthor() const { return author; } string getISBN() const { return isbn; } int getPages() const { return pages; } bool isAvailable() const { return available; } bool borrow() { if (available) { available = false; return true; } return false; } void returnBook() { available = true; } void displayInfo() const { cout << "《" << title << "》 - " << author << " (ISBN: " << isbn << ", " << pages << "页) " << (available ? "[可借]" : "[已借出]") << endl; } bool hasSameAuthor(const Book& other) const { return author == other.author; } };
class Library { private: string name; Book* books; int capacity; int bookCount; public: Library(const string& libName, int cap) : name(libName), capacity(cap), bookCount(0) { books = new Book*[capacity]; for (int i = 0; i < capacity; i++) { books[i] = nullptr; } cout << "图书馆 \"" << name << "\" 创建成功,容量: " << capacity << endl; } ~Library() { for (int i = 0; i < bookCount; i++) { delete books[i]; } delete[] books; cout << "图书馆 \"" << name << "\" 已关闭" << endl; } bool addBook(const string& title, const string& author, const string& isbn, int pages) { if (bookCount < capacity) { books[bookCount] = new Book(title, author, isbn, pages); bookCount++; cout << "图书添加成功: " << title << endl; return true; } else { cout << "图书馆已满,无法添加更多图书" << endl; return false; } } Book* findBookByTitle(const string& title) { for (int i = 0; i < bookCount; i++) { if (books[i]->getTitle() == title) { return books[i]; } } return nullptr; } Book* findBookByISBN(const string& isbn) { for (int i = 0; i < bookCount; i++) { if (books[i]->getISBN() == isbn) { return books[i]; } } return nullptr; } bool borrowBook(const string& title) { Book* book = findBookByTitle(title); if (book != nullptr) { if (book->borrow()) { cout << "借书成功: " << title << endl; return true; } else { cout << "图书已被借出: " << title << endl; return false; } } else { cout << "未找到图书: " << title << endl; return false; } } bool returnBook(const string& title) { Book* book = findBookByTitle(title); if (book != nullptr) { book->returnBook(); cout << "还书成功: " << title << endl; return true; } else { cout << "未找到图书: " << title << endl; return false; } } void displayAllBooks() const { cout << "\n=== " << name << " 图书清单 ===" << endl; if (bookCount == 0) { cout << "图书馆暂无图书" << endl; } else { for (int i = 0; i < bookCount; i++) { cout << (i + 1) << ". "; books[i]->displayInfo(); } } } void displayAvailableBooks() const { cout << "\n=== 可借图书 ===" << endl; bool hasAvailable = false; for (int i = 0; i < bookCount; i++) { if (books[i]->isAvailable()) { books[i]->displayInfo(); hasAvailable = true; } } if (!hasAvailable) { cout << "暂无可借图书" << endl; } } int getTotalBooks() const { return bookCount; } int getAvailableBooks() const { int count = 0; for (int i = 0; i < bookCount; i++) { if (books[i]->isAvailable()) { count++; } } return count; } };
void exercise2() { cout << "\n=== 练习2:图书类和图书馆类 ===" << endl; Library library("市立图书馆", 10); library.addBook("C++ Primer", "Stanley Lippman", "978-0321714114", 976); library.addBook("Effective C++", "Scott Meyers", "978-0321334879", 297); library.addBook("Design Patterns", "Gang of Four", "978-0201633610", 395); library.addBook("Clean Code", "Robert Martin", "978-0132350884", 464); library.displayAllBooks(); cout << "\n=== 借书操作 ===" << endl; library.borrowBook("C++ Primer"); library.borrowBook("Effective C++"); library.borrowBook("不存在的书"); library.displayAvailableBooks(); cout << "\n=== 还书操作 ===" << endl; library.returnBook("C++ Primer"); cout << "\n=== 统计信息 ===" << endl; cout << "总图书数: " << library.getTotalBooks() << endl; cout << "可借图书数: " << library.getAvailableBooks() << endl; library.displayAvailableBooks(); }
|