C++實現(xiàn)圖書館管理系統(tǒng)
本文實例為大家分享了C++實現(xiàn)圖書館管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、實驗名稱
圖書館管理系統(tǒng)
二、實驗目的
利用C++語言設計開發(fā)一個小型的圖書館管理系統(tǒng)模擬程序,具有如下功能:退出系統(tǒng)、增加圖書、刪除圖書、借閱圖書、歸還圖書、顯示圖書信息、查詢圖書等功能。實驗中應掌握繼承結(jié)構(gòu),并掌握對象、類、鏈表的使用和成員函數(shù)、構(gòu)造函數(shù)的定義及調(diào)用,并掌握使用實驗設備的技能技巧和程序的調(diào)試方法。
三、實驗平臺
運行環(huán)境:VC++6.0
四、問題分析
圖書館管理系統(tǒng)模擬程序可劃分為7個模塊:退出模塊、增加圖書模塊、刪除圖書模塊、借閱圖書模塊、歸還圖書模塊、顯示圖書信息模塊、查詢圖書模塊。各模塊之間均有著或多或少的聯(lián)系,比如: 借閱圖書模塊、顯示圖書信息模塊、查詢圖書模塊都需要在進行增加圖書模塊后進行。理解了各模塊之間的主要關(guān)系有利于程序的設計與完成,使程序的層次結(jié)構(gòu)清晰,便于程序的編寫、閱讀和調(diào)試。以下為本次試驗的項目構(gòu)架圖:
本次實驗定義了三個類:Item類、Person類、Library類
Item類中有public函數(shù): name、item_type、bool Register(bool函數(shù)輸出值只有ture和force,用來判斷是否注冊)。
Person類中public函數(shù):Name ()、Adress ()、Regist_items。
Library類中public函數(shù):addBook()向圖書館里加書籍、deleteBook()刪除無用書籍、brrowBook()借書,之前先判斷書籍是否存在、returnBook()還書、getReader()查詢某編號的書是誰借了、indexOfNum(string num) 根據(jù)編號得到書在數(shù)組中的下標;
private函數(shù):vector books所有書籍、map<string, int> readers存儲讀者及其所借的書籍數(shù)目、currentNum庫存書籍數(shù)目、brrowNum借出書籍數(shù)目。
附錄:
程序源代碼:
#include<iostream> #include<string> #include<vector> #include<map> #include<iomanip> #include <list> using namespace std; class item { ?? ?public: ?? ?string name; ?? ?string item_type; ?? ?bool Register; }; //雜志類 class magazine :public item { ?? ?string Type; ?? ?string Writer; }; //MusicCd類 class MusicCd :public item { ?? ?string Singer; }; //電影類 class Movie :public item { ?? ?string Type; ?? ?string Director; ?? ?string Actor; }; //書籍類 class Book : public item { public: ?? ?Book() { borrow_flag = false; } ? //無參構(gòu)造函數(shù) ?? ?Book(string name, string num, string auther) ?? ??? ?:name(name), num(num), auther(auther) { ?? ??? ?borrow_flag = false; ?? ?} ?//有參構(gòu)造函數(shù) ?? ?void setReader(string reader, int lcn, string data); //設置讀者 ?? ?void setInfo(string name, string num, string auther); //設置書籍信息 ?? ?string getName() { ?? ??? ?return name; ?? ?} ?? ?string getNum() { return num; } ?? ?string getAuther() { ?? ??? ?return auther; ?? ?} ?? ?bool getBorrow_flag() { ?? ??? ?return borrow_flag; ?? ?} ?? ?string getReader() { ?? ??? ?return reader; ?? ?} ?? ?int getLcn() { ?? ??? ?return lcn; ?? ?} ?? ?string getData() { ?? ??? ?return data; ?? ?} ?? ?bool isBorrow() { return borrow_flag; } ? ? ? ?//判斷書籍是否借出 ?? ?void setBorrow_flag(bool b) { borrow_flag = b; } ?? ?void showInfo(); ? ? ? ?//顯示數(shù)據(jù)信息 private: ?? ?string name; ?//書名 ?? ?string num; ? //編號(唯一標示) ?? ?string auther; //作者 ?? ?bool borrow_flag; ?? ?string reader; //讀者 ?? ?int lcn; ? ? ? //借書證號 ?? ?string data; ? //借書日期 }; //DVD電影類 class DVD :public Movie { }; //藍光電影類 class Blue_ligh :public Movie { }; //用戶 class Person { public: ?? ?string Name; ?? ?string Adress; ?? ?list<item> Regist_items; }; void Book::setReader(string reader, int lcn, string data) { ?? ?borrow_flag = true; ?? ?this->reader.assign(reader); ?? ?this->lcn = lcn; ?? ?this->data.assign(data); } void Book::setInfo(string name, string num, string auther) { ?? ?this->name.assign(name); ?? ?this->num.assign(num); ?? ?this->auther.assign(auther); } void Book::showInfo() { ?? ?cout << "書籍名稱:" << setiosflags(ios_base::left) << setw(56) << name << endl ?? ??? ? << "書籍編號:" << setw(56) << num<< endl ?? ??? ? << "書籍作者:" << setw(56) << auther ?<< endl; ?? ?if (borrow_flag) ?? ?{ ?? ? ? cout << "書籍被借出。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n" ?? ??? ??? ?<< "讀者姓名:" << setw(56) << reader<< endl ?? ??? ??? ?<< "借書證號:" << setw(56) << lcn << endl ?? ??? ??? ?<< "借書日期:" << setw(56) << data << endl; ?? ?} ?? ?else { ?? ??? ?cout << "書籍未被借出。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ?} } class Library { public: ?? ?//書籍庫 ?? ?list<item> itemList; ?? ?Library() { currentNum = 0; brrowNum = 0; } ?? ?void addBook(); ? ? ? ? ? ? ? ?//向圖書館里加書籍 ?? ?void addBook(string name, string num, string auther); ?? ?void deleteBook(); ? //刪除無用書籍 ?? ?void brrowBook(); ? //借書,之前先判斷書籍是否存在 ?? ?void returnBook(); ? //還書 ?? ?void getReader(); ?//查詢某編號的書是誰借了 ?? ?int indexOfNum(string num); //根據(jù)編號得到書在數(shù)組中的下標 ?? ?vector<Book> getBooks() { ?? ??? ?return books; ?? ?} ?? ?void showInfo(); ?? ?int getTotalBooks() { return currentNum + brrowNum; } private: ?? ?vector<Book> books;//所有書籍 ?? ?map<string, int> readers; ?//存儲讀者及其所借的書籍數(shù)目? ?? ?int currentNum; ? //庫存書籍數(shù)目(不包括借出的) ?? ?int brrowNum; ? ? //借出書籍數(shù)目 }; void Library::showInfo() { ?? ?cout << " ?***************************所有圖書信息***************************\n\n"; ?? ?for (int i = 0; i < books.size(); i++) ?? ?{ ?? ??? ?cout << "第" << i + 1 << "本書籍的信息。" << endl; ?? ??? ?books[i].showInfo(); ?? ?} ?? ?system("pause"); ?? ?system("cls"); } int Library::indexOfNum(string num) { ?? ?int i; ?? ?for (i = 0; i < books.size(); i++) ?? ?{ ?? ??? ?if (books[i].getNum() == num) ?? ??? ??? ?return i; ?? ?} ?? ?return -1; } void Library::addBook() { ?? ?Book b; ?? ?int temp; ?? ?string name, num, auther; ?? ?cout << " ?*****************************增加界面*****************************\n\n"; ?? ?do { ?? ??? ?cout << "輸入書籍名稱,編號,作者:"; ?? ??? ?cin >> name >> num >> auther; ?? ??? ?b.setInfo(name, num, auther); ?? ??? ?if (indexOfNum(num) == -1) { ?? ??? ??? ?books.push_back(b); ?? ??? ??? ?currentNum++; ?? ??? ??? ?cout << "\n添加成功。" << endl; ?? ??? ??? ?cout << "輸入1繼續(xù)增加,返回上一層輸入2:"; ?? ??? ??? ?cin >> temp; ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?cout << "已存在該編號的書籍,添加失敗。" << endl; ?? ??? ??? ?cout << "輸入1繼續(xù)重新增加,返回上一層輸入2:"; ?? ??? ??? ?cin >> temp; ?? ??? ?} ?? ?} while (temp == 1); ?? ?system("pause"); ?? ?system("cls"); } void Library::addBook(string name, string num, string auther) { ?? ?Book b; ?? ?b.setInfo(name, num, auther); ?? ?books.push_back(b); } void Library::deleteBook() { ?? ?int index, temp; ?? ?string num; ?? ?cout << " ?*****************************刪除界面*****************************\n\n"; ?? ?do { ?? ??? ?cout << "輸入要刪除的書籍的編號:"; ?? ??? ?cin >> num; ?? ??? ?index = indexOfNum(num); ?? ??? ?if (index != -1) { ?? ??? ??? ?if (!books[index].getBorrow_flag()) { ?? ??? ??? ??? ?cout << "刪除的書籍的信息:\n"; ?? ??? ??? ??? ?books[index].showInfo(); ?? ??? ??? ??? ?books.erase(books.begin() + index); ?? ??? ??? ??? ?currentNum--; ?? ??? ??? ??? ?cout << "刪除成功。" << endl; ?? ??? ??? ??? ?cout << "輸入1繼續(xù)繼續(xù)刪除,返回上一層輸入2:"; ?? ??? ??? ??? ?cin >> temp; ?? ??? ??? ?} ?? ??? ??? ?else { ?? ??? ??? ??? ?cout << "刪除失??!書籍已經(jīng)被借出。" << endl; ?? ??? ??? ??? ?cout << "輸入1繼續(xù)繼續(xù)刪除,返回上一層輸入2:"; ?? ??? ??? ??? ?cin >> temp; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?cout << "刪除失敗。未找到編號為" << num << "的書籍。\n"; ?? ??? ??? ?cout << "輸入1繼續(xù)繼續(xù)刪除,返回上一層輸入2:"; ?? ??? ??? ?cin >> temp; ?? ??? ?} ?? ?} while (temp == 1); ?? ?system("pause"); ?? ?system("cls"); } void Library::brrowBook() { ?? ?string num; ?? ?int index; ?? ?cout << " ?*****************************借閱界面*****************************\n\n"; ?? ?cout << "輸入要借閱的書籍的編號:"; ?? ?cin >> num; ?? ?index = indexOfNum(num); ?? ?if (index != -1) { ?? ??? ?if (books[index].isBorrow()) { ?? ??? ??? ?cout << "借閱失敗,書籍以及被借出。\n"; ?? ??? ??? ?system("pause"); ?? ??? ??? ?system("cls"); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?cout << "要借的書籍的信息:\n"; ?? ??? ??? ?books[index].showInfo(); ?? ??? ??? ?string reader, data; ?? ??? ??? ?int lcn; ?? ??? ??? ?cout << "輸入讀者姓名,借書證號,借書日期:"; ?? ??? ??? ?cin >> reader >> lcn >> data; ?? ??? ??? ?if (readers[reader] != 2) { ?? ??? ??? ??? ?books[index].setReader(reader, lcn, data); ?? ??? ??? ??? ?cout << "借書完成。\n"; ?? ??? ??? ??? ?currentNum--; ?? ??? ??? ??? ?brrowNum++; ?? ??? ??? ??? ?readers[reader]++; ?? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?cout << "借書失敗,該讀者以借超過兩本書籍。\n"; ?? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?else ?? ?{ ?? ??? ?cout << "借書失敗。未找到編號為" << num << "的書籍.\n"; ?? ??? ?system("pause"); ?? ??? ?system("cls"); ?? ?} } void Library::returnBook() { ?? ?string num; ?? ?cout << " ?*****************************還書界面*****************************\n\n"; ?? ?cout << "輸入要還的書籍的編號:"; ?? ?cin >> num; ?? ?int index; ?? ?index = indexOfNum(num); ?? ?if (index != -1) ?? ?{ ?? ??? ?cout << "要還的書籍的信息為:\n"; ?? ??? ?books[index].showInfo(); ?? ??? ?books[index].setBorrow_flag(false); ?? ??? ?readers[books[index].getReader()]--; ?? ??? ?cout << "還書成功。\n"; ?? ??? ?system("pause"); ?? ??? ?system("cls"); ?? ?} ?? ?else ?? ?{ ?? ??? ?cout << "還書失敗,請檢查書籍編號是否輸入錯誤!\n"; ?? ??? ?system("pause"); ?? ??? ?system("cls"); ?? ?} } void Library::getReader() { ?? ?string num; ?? ?cout << " ?*****************************查詢界面*****************************\n\n"; ?? ?cout << "輸入要查找的書籍編號:"; ?? ?cin >> num; ?? ?int index; ?? ?index = indexOfNum(num); ?? ?if (index != -1) ?? ?{ ?? ??? ?if (books[index].getBorrow_flag()) ?? ??? ??? ?cout << "讀者為:" << books[index].getReader() << endl; ?? ??? ?else { ?? ??? ??? ?cout << "無讀者。" << endl; ?? ??? ?} ?? ??? ?system("pause"); ?? ??? ?system("cls"); ?? ?} ?? ?else ?? ?{ ?? ??? ?cout << "查詢失敗,請檢查書籍編號是否輸入錯誤!\n"; ?? ??? ?system("pause"); ?? ??? ?system("cls"); ?? ?} } Library l; void menu() { ?? ?int temp; ?? ? ?? ?while (1) ?? ?{ ?? ??? ?cout << "___________________________ 圖書館管理系統(tǒng)____________________________\n"; ?? ? ? ?cout << " ? ? ? ? ? ? ? ? ?┏━━━━━━━━━━━━━┓ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┃ [0]退出系統(tǒng)。 ? ? ? ? ? ?┃ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┃ [1]增加圖書。 ? ? ? ? ? ?┃ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┃ [2]刪除圖書。 ? ? ? ? ? ?┃ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┃ [3]借閱圖書。 ? ? ? ? ? ?┃ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┃ [4]歸還圖書。 ? ? ? ? ? ?┃ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┃ [5]顯示圖書信息。 ? ? ? ?┃ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┃ [6]查詢圖書。 ? ? ? ? ? ?┃ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << " ? ? ? ? ? ? ? ? ?┗━━━━━━━━━━━━━┛ ? ? ? ? ? ? ? ? ? ? ?\n"; ?? ??? ?cout << "輸入要進行的操作:"; ?? ??? ?cin >> temp; ?? ??? ?switch (temp) { ?? ??? ?case 1: ?? ??? ??? ?system("cls"); ?? ??? ??? ?l.addBook();? ?? ??? ??? ?break; ?? ??? ?case 2:system("cls"); ?? ??? ??? ?l.deleteBook();? ?? ??? ??? ?break; ?? ??? ?case 3:system("cls"); ?? ??? ??? ?l.brrowBook();? ?? ??? ??? ?break; ?? ??? ?case 4:system("cls"); ?? ??? ??? ?l.returnBook();? ?? ??? ??? ?break; ?? ??? ?case 5:system("cls"); ?? ??? ??? ?l.showInfo(); ?? ??? ??? ?break; ?? ??? ?case 6:system("cls"); ?? ??? ??? ?l.getReader(); ?? ??? ??? ?break; ?? ??? ?case 0: ?? ??? ??? ? ?? ??? ??? ?exit(1); ?? ??? ??? ?break; ?? ??? ?default: ?? ??? ??? ?cout << "輸入錯誤!" << endl; ?? ??? ??? ?system("pause"); ?? ??? ??? ?system("cls"); ?? ??? ?} ?? ?} } int main() { ?? ?menu(); ?? ?return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++產(chǎn)生隨機數(shù)的幾種方法小結(jié)
本文主要介紹了C++產(chǎn)生隨機數(shù)的幾種方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03C/C++實現(xiàn)磁盤相關(guān)操作的示例代碼
這篇文章主要為大家詳細介紹了C/C++如何實現(xiàn)磁盤相關(guān)操作,例如遍歷磁盤容量、實現(xiàn)磁盤格式化、移除指定磁盤等,感興趣的小伙伴可以跟隨小編一起學習一下2023-11-11C語言中自動隱式轉(zhuǎn)換與類型強制轉(zhuǎn)換實例分析
這篇文章主要介紹了C語言中自動隱式轉(zhuǎn)換與類型強制轉(zhuǎn)換實例分析,需要的朋友可以參考下2014-07-07