這篇文章主要講解了“C++實(shí)現(xiàn)圖書管理系統(tǒng)的代碼實(shí)例”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C++實(shí)現(xiàn)圖書管理系統(tǒng)的代碼實(shí)例”吧!
創(chuàng)新互聯(lián)建站專注于金鄉(xiāng)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供金鄉(xiāng)營(yíng)銷型網(wǎng)站建設(shè),金鄉(xiāng)網(wǎng)站制作、金鄉(xiāng)網(wǎng)頁(yè)設(shè)計(jì)、金鄉(xiāng)網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造金鄉(xiāng)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供金鄉(xiāng)網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
閑來無事,用C++做了一個(gè)圖書管理系統(tǒng),主要有借書、還書、圖書管理、用戶管理等功能,主要用到的技術(shù)有容器和文件,以及類的封裝
#include#include #include
#include #include #include using namespace std;class Mybook;class Book; class Book{public: int id; //書號(hào) char book_name[20]; //書名 int state; //圖書狀態(tài) char place[20]; //圖書所在位置 char stu_number[20]; //學(xué)號(hào) char stu_name[20]; //學(xué)生姓名public: Book(); friend class Mybook;}; class User{public: char stu_number[20]; //學(xué)號(hào) char stu_name[20]; //學(xué)生姓名public: User() { strcpy(stu_number,"000"); strcpy(stu_name,"0"); } friend class Mybook;}; class Mybook{private: list link_book; //保存書本信息 list link_user; //保存用戶信息public: int main_menu(); //主菜單 void getmenu(); //獲取菜單 int menu(); //圖書管理菜單 void getchoice(); //輸入選擇 void add_book(); //添加圖書 void display_book(); //顯示圖書信息 void del_book(); //刪除圖書信息 void search_book(); //搜索圖書信息 void update_book(); //修改圖書信息 void borrow_book(); //借書 void return_book(); //還書 int menu_user(); //管理用戶菜單 void add_user(); //添加用戶 void del_user(); //刪除用戶 void display_user(); //查看用戶 void update_user(); //修改用戶 void look_borrow(); //查看借閱圖書情況 void get_user(); //用戶管理 void recv_file(); //將數(shù)據(jù)保存到文件中 void read_file(); //將數(shù)據(jù)從文件中讀取 void recv_user(); //將用戶信息保存到文件 void read_user(); //將用戶信息從文件讀取}; Book::Book(){ state = 0; strcpy(stu_number,"000"); strcpy(stu_name,"0");}//保存圖書信息到文件void Mybook::recv_file(){ ofstream outfile("library.txt",ios::out); if(!outfile) { cout<<"文件打開失敗"< ::iterator p = link_book.begin(); while(p != link_book.end()) { outfile< id< book_name< state< place< stu_number< stu_name< ::iterator p = link_user.begin(); while(p != link_user.end()) { outfile< stu_number< stu_name< >choice; while(!(choice >= 0 && choice <= 4)) { cout<<"輸入錯(cuò)誤,請(qǐng)重新輸入:"; cin>>choice; } return choice;}//執(zhí)行主菜單void Mybook::getmenu(){ int choice = 0; read_user(); read_file(); while(1) { system("clear"); choice = main_menu(); switch(choice) { case 1: { borrow_book(); break; } case 2: { return_book(); break; } case 3: { getchoice(); break; } case 4: { get_user(); break; } case 0: { cout<<"正在保存,請(qǐng)稍后....."< >id; list ::iterator it = link_user.begin(); while(it != link_user.end()) { if(strcmp(it->stu_number,id) == 0) { strcpy(name,it->stu_name); flag2 = 1; break; } it++; } if(flag2 == 0) { cout<<"你沒有借書權(quán)限!借書失敗"< >book_name; list ::iterator p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0) { cout<<"======================================="< id< book_name< state == 0) { cout<<"圖書狀態(tài):未借閱!"< place< >book_id; while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0 && p->id == book_id && p->state == 0) { strcpy(p->stu_number,id); strcpy(p->stu_name,name); p->state = 1; cout<<"借書成功!"< >stu_id; int flag = 0; int flag1 = 0; list ::iterator p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->stu_number,stu_id) == 0) { cout<<"==========================================="< id< book_name< >id; p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->stu_number,stu_id) == 0&&p->id == id) { strcpy(p->stu_name ,"000"); strcpy(p->stu_number , "0"); p->state = 0; cout<<"還書成功!"< >choice; while(!(choice >= 1 && choice <= 6)) { cout<<"輸入錯(cuò)誤,請(qǐng)重新輸入:"; cin>>choice; } return choice; }//執(zhí)行用戶管理void Mybook::get_user(){ int choice = 0; while(1) { system("clear"); choice = menu_user(); system("clear"); switch(choice) { case 1: { add_user(); break; } case 2: { display_user(); break; } case 3: { del_user(); break; } case 4: { update_user(); break; } case 5: { look_borrow(); break; } case 6: { recv_user(); return ; } } cout< >new_user.stu_number; cout<<"請(qǐng)輸入姓名:"; cin>>new_user.stu_name; link_user.push_back(new_user); cout<<"添加成功!"; cout<<"是否繼續(xù)添加(y/n)"; char ch; cin>>ch; while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n')) { cout<<"輸入有誤,請(qǐng)重新輸入:"; cin>>ch; } if(ch == 'Y'||ch == 'y') { system("clear"); add_user(); }}//顯示用戶void Mybook::display_user(){ list ::iterator it = link_user.begin(); while(it != link_user.end()) { cout<<"====================================="< stu_number< stu_name< >id; int flag = 0; list ::iterator it = link_user.begin(); while(it != link_user.end()) { if(strcmp(it->stu_number,id) == 0) { link_user.erase(it); flag = 1; break; } it++; } if(flag == 1) { cout<<"刪除成功!"< >number; int flag = 0; list ::iterator it = link_user.begin(); while(it != link_user.end()) { if(strcmp(it->stu_number,number) == 0) { cout<<"請(qǐng)更新學(xué)號(hào):"; cin>>it->stu_number; cout<<"請(qǐng)更新姓名:"; cin>>it->stu_name; flag = 1; break; } it++; } if(flag == 1) { cout<<"修改成功!"< ::iterator p = link_book.begin(); while(p != link_book.end()) { if(p->state == 1) { cout<<"==================================================="< stu_name< stu_number< book_name< place< >choice; while(!(choice >= 1 && choice <= 6)) { cout<<"輸入錯(cuò)誤,請(qǐng)重新輸入:"; cin>>choice; } return choice; }//實(shí)行圖書管理void Mybook::getchoice(){ int choice = 0; while(1) { system("clear"); choice = menu(); system("clear"); switch(choice) { case 1: { add_book(); break; } case 2: { display_book(); break; } case 3: { del_book(); break; } case 4: { search_book(); break; } case 5: { update_book(); break; } case 6: { return ; } } cout< >new_book.id; cout<<"請(qǐng)輸入書名:"; cin>>new_book.book_name; cout<<"請(qǐng)輸入圖書書架:"; cin>>new_book.place; link_book.push_back(new_book); cout<<"添加成功!"; cout<<"是否繼續(xù)添加(y/n)"; char ch; cin>>ch; while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n')) { cout<<"輸入有誤,請(qǐng)重新輸入:"; cin>>ch; } if(ch == 'Y'||ch == 'y') { system("clear"); add_book(); }}//顯示書本信息void Mybook::display_book(){ list ::iterator p = link_book.begin(); while(p != link_book.end()) { cout<<"======================================="< id< book_name< state == 0) { cout<<"圖書狀態(tài):未借閱!"< place< ::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<"請(qǐng)輸入你要?jiǎng)h除的書號(hào):"; cin>>num; while(p != link_book.end()) { if(p->id == num) { link_book.erase(p); flag = 1; break; } p++; } if(flag == 1) { cout<<"\n\n刪除完成!"; } else { cout<<"\n\n該書不存在,刪除失??!"< ::iterator p = link_book.begin(); char book_name[20]; int flag = 0; cout<<"請(qǐng)輸入你要查找的書名:"; cin>>book_name; while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0) { cout<<"======================================="< id< book_name< state == 0) { cout<<"圖書狀態(tài):未借閱!"< place< ::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<"請(qǐng)輸入你要更新的書號(hào):"; cin>>num; while(p != link_book.end()) { if(p->id == num) { cout<<"請(qǐng)輸入書名"; cin>>p->book_name; cout<<"請(qǐng)輸入圖書書架號(hào):"; cin>>p->place; flag = 1; } p++; } if(flag == 1) { cout<<"\n\n更新完成!"; } else { cout<<"\n\n該書不存在,更新失??!"< 感謝各位的閱讀,以上就是“C++實(shí)現(xiàn)圖書管理系統(tǒng)的代碼實(shí)例”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)C++實(shí)現(xiàn)圖書管理系統(tǒng)的代碼實(shí)例這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
本文標(biāo)題:C++實(shí)現(xiàn)圖書管理系統(tǒng)的代碼實(shí)例
分享鏈接:http://weahome.cn/article/ggdesh.html