圖書館
模擬一個生活中小型圖書館的使用程序,功能如下:
1. 向圖書館添加書籍
2. 向圖書館借出書籍
3. 向圖書館歸還書籍
4. 顯示圖書館當前狀態
5. 退出程序
簡單分析如下:
我們需要知道書籍,作者以及讀者之間的聯繫。一本書有作者,有書籍名,如果直接把書歸爲一類,作者和書籍名都是其類內成員,然後用容器裝上書類,再進行相關操作,就會顯得較爲複雜。根據分而治之的思想,我們把其分爲兩類單獨表示,一類爲作者,一類爲書籍名,再將兩類設爲各自的友元類,這樣就能夠爲功能的實現減輕負擔。再根據現實生活中的模擬,每個圖書館都會有借書單,用來顯示書籍的借出情況,這裏,我們也需要對其進行抽象歸類,這樣案例的實現需要定義四類:Author,Book,Patron和CheckedOutBook。
功能的實現則可以抽象爲函數進行操作。所以最後剩下的問題就是容器的選用。這裏選用STL庫裏面的list庫,這麼做的目的是爲了讓功能實現時更加高效方便。
案例分析標準的參考程序如下:
#include "pch.h"
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
class Patron; //前向聲明,方便使用Patron類
class Book {
public:
Book() {
patron = 0;
}
bool operator== (const Book& bk) const {
return strcmp(title,bk.title) == 0;
}
private:
char *title; //書籍名
Patron *patron; //用來判斷書籍是否被讀者借走,方便後續書籍狀態顯示
ostream& printBook(ostream&) const;
friend ostream& operator<< (ostream& out, const Book& bk) {
return bk.printBook(out);
}
friend class CheckedOutBook; //改變書籍狀態
friend Patron;
friend void includeBook();
friend void checkOutBook();
friend void returnBook();
};
class Author {
public:
Author() {
}
bool operator== (const Author& ar) const {
return strcmp(name,ar.name) == 0;
}
private:
char *name;
list<Book> books; //作者的書籍清單
ostream& printAuthor(ostream&) const;
friend ostream& operator<< (ostream& out,const Author& ar) {
return ar.printAuthor(out);
}
friend void includeBook();
friend void checkOutBook();
friend void returnBook();
friend class CheckedOutBook;
friend Patron;
};
class CheckedOutBook {
public:
CheckedOutBook(list<Author>::iterator ar = 0,
list<Book>::iterator bk = 0) {
author = ar;
book = bk;
}
bool operator== (const CheckedOutBook& bk) const {
return strcmp(author->name,bk.author->name) == 0 &&
strcmp(book->title,bk.book->title) == 0;
}
private:
list<Author>::iterator author; //作者列表迭代器
list<Book>::iterator book; //書籍列表迭代器
friend void checkOutBook();
friend void returnBook();
friend Patron;
};
class Patron {
public:
Patron() {
}
bool operator== (const Patron& pn) const {
return strcmp(name,pn.name) == 0;
}
private:
char *name;
list<CheckedOutBook> books; //借書清單
ostream& printPatron(ostream&) const;
friend ostream& operator<< (ostream& out, const Patron& pn) {
return pn.printPatron(out);
}
friend void checkOutBook();
friend void returnBook();
friend Book;
};
list<Author> catalog['Z'+1];
list<Patron> people['Z'+1];
//輸出重載
ostream& Author::printAuthor(ostream& out) const {
out << name << endl;
list<Book>::const_iterator ref = books.begin();
for ( ; ref != books.end(); ref++)
out << *ref; // overloaded <<
return out;
}
ostream& Book::printBook(ostream& out) const {
out << " * " << title;
if (patron != 0)
out << " - checked out to " << patron->name; // overloaded <<
out << endl;
return out;
}
ostream& Patron::printPatron(ostream& out) const {
out << name;
if (!books.empty()) {
out << " has the following books:\n";
list<CheckedOutBook>::const_iterator bk = books.begin();
for ( ; bk != books.end(); bk++)
out << " * " << bk->author->name << ", "
<< bk->book->title << endl;
}
else out << " has no books\n";
return out;
}
template<class T>
ostream& operator<< (ostream& out, const list<T>& lst) {
for (list<T>::const_iterator ref = lst.begin(); ref != lst.end(); ref++)
out << *ref; // overloaded <<
return out;
}
//輸入輸出獲取
char* getString(char *msg) {
char s[82], i, *destin;
cout << msg;
cin.get(s,80);
while (cin.get(s[81]) && s[81] != '\n'); // discard overflowing
destin = new char[strlen(s)+1]; // characters;
for (i = 0; destin[i] = toupper(s[i]); i++);
return destin;
}
//功能4
void status() {
register int i;
cout << "Library has the following books:\n\n";
for (i = 'A'; i <= 'Z'; i++)
if (!catalog[i].empty())
cout << catalog[i];
cout << "\nThe following people are using the library:\n\n";
for (i = 'A'; i <= 'Z'; i++)
if (!people[i].empty())
cout << people[i];
}
//功能1
void includeBook() {
Author newAuthor;
Book gnewBook;
newAuthor.name = getString("Enter author's name: ");
newBook.title = getString("Enter the title of the book: ");
list<Author>::iterator oldAuthor =
find(catalog[newAuthor.name[0]].begin(),
catalog[newAuthor.name[0]].end(),newAuthor);
if (oldAuthor == catalog[newAuthor.name[0]].end()) {
newAuthor.books.push_front(newBook);
catalog[newAuthor.name[0]].push_front(newAuthor);
}
else (*oldAuthor).books.push_front(newBook);
}
//功能2
void checkOutBook() {
Patron patron;
Author author;
Book book;
list<Author>::iterator authorRef;
list<Book>::iterator bookRef;
patron.name = getString("Enter patron's name: ");
while (true) {
author.name = getString("Enter author's name: ");
authorRef = find(catalog[author.name[0]].begin(),
catalog[author.name[0]].end(),author);
if (authorRef == catalog[author.name[0]].end())
cout << "Misspelled author's name\n";
else break;
}
while (true) {
book.title = getString("Enter the title of the book: ");
bookRef = find((*authorRef).books.begin(),
(*authorRef).books.end(),book);
if (bookRef == (*authorRef).books.end())
cout << "Misspelled title\n";
else break;
}
list<Patron>::iterator patronRef;
patronRef = find(people[patron.name[0]].begin(),
people[patron.name[0]].end(),patron);
CheckedOutBook checkedOutBook(authorRef,bookRef);
if (patronRef == people[patron.name[0]].end()) { // a new patron
patron.books.push_front(checkedOutBook); // in the library;
people[patron.name[0]].push_front(patron);
(*bookRef).patron = &*people[patron.name[0]].begin();
}
else {
(*patronRef).books.push_front(checkedOutBook);
(*bookRef).patron = &*patronRef;
}
}
//功能3
void returnBook() {
Patron patron;
Book book;
Author author;
list<Patron>::iterator patronRef;
list<Book>::iterator bookRef;
list<Author>::iterator authorRef;
while (true) {
patron.name = getString("Enter patron's name: ");
patronRef = find(people[patron.name[0]].begin(),
people[patron.name[0]].end(),patron);
if (patronRef == people[patron.name[0]].end())
cout << "Patron's name misspelled\n";
else break;
}
while (true) {
author.name = getString("Enter author's name: ");
authorRef = find(catalog[author.name[0]].begin(),
catalog[author.name[0]].end(),author);
if (authorRef == catalog[author.name[0]].end())
cout << "Misspelled author's name\n";
else break;
}
while (true) {
book.title = getString("Enter the title of the book: ");
bookRef = find((*authorRef).books.begin(),
(*authorRef).books.end(),book);
if (bookRef == (*authorRef).books.end())
cout << "Misspelled title\n";
else break;
}
CheckedOutBook checkedOutBook(authorRef,bookRef);
(*bookRef).patron = 0;
(*patronRef).books.remove(checkedOutBook);
}
int menu() {
int option;
cout << "\nEnter one of the following options:\n"
<< "1. Include a book in the catalog\n2. Check out a book\n"
<< "3. Return a book\n4. Status\n5. Exit\n"
<< "Your option? ";
cin >> option;
cin.get(); // discard '\n';
return option;
}
int main() {
while (true)
switch (menu()) {
case 1: includeBook(); break;
case 2: checkOutBook(); break;
case 3: returnBook(); break;
case 4: status(); break;
case 5: return 0;
default: cout << "Wrong option, try again: ";
}
return 0;
}