2014-04-23 18:42
题目:设计一个在线阅读系统的数据结构。
解法:这题目太大了,我的个亲娘。显然你不可能一次加载一整本书,做到单页纸加载的粒度是很必要的。为了读书的连贯效果,预取个几页也很必要。
代码:
1 // 8.5 Deign a class for an online book reader system. 2 #include <unordered_map> 3 using namespace std; 4 5 class BookReaderSystem { 6 public: 7 BookReaderSystem() {}; 8 9 // find a book by its name and read it. 10 void readBook(string book_name) { 11 Page page; 12 13 if (books.find(book_name) != books.end()) { 14 i = 0; 15 // load the book page by page 16 while (page = loadPage(books[book_name], i, page)) { 17 displayPage(page); 18 ++i; 19 } 20 } 21 }; 22 23 ~BookReaderSystem() { 24 books.clear(); 25 }; 26 private: 27 // the K-V database 28 unordered_map<string, Book> books; 29 // load one page from a book 30 bool loadPage(Book &book, int page_no, Page &page); 31 // display this page 32 void displayPage(Page &page); 33 };