C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
閑來(lái)無(wú)事,用C++做了一個(gè)圖書(shū)管理系統(tǒng),主要有借書(shū)、還書(shū)、圖書(shū)管理、用戶管理等功能,主要用到的技術(shù)有容器和文件,以及類(lèi)的封裝
#include <iostream> #include <list> #include <algorithm> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; class Mybook; class Book; class Book{ public: int id; //書(shū)號(hào) char book_name[20]; //書(shū)名 int state; //圖書(shū)狀態(tài) char place[20]; //圖書(shū)所在位置 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<Book> link_book; //保存書(shū)本信息 list<User> link_user; //保存用戶信息 public: int main_menu(); //主菜單 void getmenu(); //獲取菜單 int menu(); //圖書(shū)管理菜單 void getchoice(); //輸入選擇 void add_book(); //添加圖書(shū) void display_book(); //顯示圖書(shū)信息 void del_book(); //刪除圖書(shū)信息 void search_book(); //搜索圖書(shū)信息 void update_book(); //修改圖書(shū)信息 void borrow_book(); //借書(shū) void return_book(); //還書(shū) int menu_user(); //管理用戶菜單 void add_user(); //添加用戶 void del_user(); //刪除用戶 void display_user(); //查看用戶 void update_user(); //修改用戶 void look_borrow(); //查看借閱圖書(shū)情況 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"); } //保存圖書(shū)信息到文件 void Mybook::recv_file() { ofstream outfile("library.txt",ios::out); if(!outfile) { cout<<"文件打開(kāi)失敗"<<endl; return ; } sleep(1); list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { outfile<<p->id<<endl; outfile<<p->book_name<<endl; outfile<<p->state<<endl; outfile<<p->place<<endl; outfile<<p->stu_number<<endl; outfile<<p->stu_name<<endl; p++; } cout<<"保存完成!"<<endl; outfile.close(); } void Mybook::read_file() { ifstream infile("library.txt",ios::in); Book new_book; if(!infile) { cout<<"read fail"<<endl; return ; } char temp[20]; while(!infile.eof()) { infile.getline(temp,'\n'); new_book.id = atoi(temp); memset(temp,0,20); infile.getline(new_book.book_name,'\n'); infile.getline(temp,'\n'); new_book.state = atoi(temp); memset(temp,0,20); infile.getline(new_book.place,'\n'); infile.getline(new_book.stu_number,'\n'); infile.getline(new_book.stu_name,'\n'); if(infile.eof()) { break; } link_book.push_back(new_book); } infile.close(); } //保存用戶信息 void Mybook::recv_user() { ofstream outfile("user.txt",ios::out); if(!outfile) { cout<<"文件打開(kāi)失敗"<<endl; return ; } sleep(1); list<User>::iterator p = link_user.begin(); while(p != link_user.end()) { outfile<<p->stu_number<<endl; outfile<<p->stu_name<<endl; p++; } cout<<"保存完成!"<<endl; outfile.close(); } //讀取數(shù)據(jù)至用戶 void Mybook::read_user() { ifstream infile("user.txt",ios::in); if(!infile) { cout<<"文件打開(kāi)失敗!"<<endl; return ; } User new_user; while(!infile.eof()) { infile.getline(new_user.stu_number,'\n'); infile.getline(new_user.stu_name,'\n'); if(infile.eof()) { break; } link_user.push_back(new_user); } infile.close(); } //主菜單 int Mybook::main_menu() { int choice = 0; cout<<"\t\t\t\t===================================================="<<endl; cout<<"\t\t\t\t* 歡迎來(lái)到圖書(shū)管理系統(tǒng) *"<<endl; cout<<"\t\t\t\t*--------------------------------------------------*"<<endl; cout<<"\t\t\t\t* 1、借書(shū) *"<<endl; cout<<"\t\t\t\t* 2、還書(shū) *"<<endl; cout<<"\t\t\t\t* 3、圖書(shū)管理 *"<<endl; cout<<"\t\t\t\t* 4、用戶管理 *"<<endl; cout<<"\t\t\t\t* 0、保存并退出 *"<<endl; cout<<"\t\t\t\t*__________________________________________________*"<<endl; cout<<"\t\t\t\t\t請(qǐng)輸入選擇:"; cin>>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)稍后....."<<endl; recv_file(); exit(0); } } cout<<endl<<endl<<endl; cout<<"按任意鍵返回..."; getchar(); getchar(); } } //借書(shū) void Mybook::borrow_book() { int flag = 0; int flag1 = 0; int flag2 = 0; char id[20]; char name[20]; char book_name[20]; cout<<"請(qǐng)輸入學(xué)號(hào):"; cin>>id; list<User>::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<<"你沒(méi)有借書(shū)權(quán)限!借書(shū)失敗"<<endl; return ; } cout<<"請(qǐng)輸入書(shū)名:"; cin>>book_name; list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0) { cout<<"======================================="<<endl; cout<<"書(shū)號(hào):"<<p->id<<endl; cout<<"書(shū)名:"<<p->book_name<<endl; if(p->state == 0) { cout<<"圖書(shū)狀態(tài):未借閱!"<<endl; } else { cout<<"圖書(shū)狀態(tài):已借閱!"<<endl; } cout<<"所在書(shū)架"<<p->place<<endl; flag1 = 1; } p++; } if(flag1 == 0) { cout<<"你所借閱的書(shū)不存在,借書(shū)失??!"; return ; } p = link_book.begin(); cout<<"已查找該圖書(shū),請(qǐng)輸入書(shū)號(hào)進(jìn)行借閱:"; int book_id; cin>>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<<"借書(shū)成功!"<<endl; flag = 1; break; } p++; } if(flag == 0) { cout<<"該書(shū)已被借閱,借書(shū)失?。?; } } //還書(shū) void Mybook::return_book() { char stu_id[20]; cout<<"請(qǐng)輸入學(xué)號(hào)"; cin>>stu_id; int flag = 0; int flag1 = 0; list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->stu_number,stu_id) == 0) { cout<<"==========================================="<<endl; cout<<"書(shū)號(hào):"<<p->id<<endl; cout<<"書(shū)名:"<<p->book_name<<endl; flag = 1; } p++; } if(flag == 0) { cout<<"當(dāng)前沒(méi)有您沒(méi)有借書(shū)"<<endl; return ; } else if(flag == 1) { cout<<"已經(jīng)查詢到你借閱的圖書(shū),請(qǐng)輸入書(shū)號(hào)進(jìn)行歸還:"<<endl; } int id; cin>>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<<"還書(shū)成功!"<<endl; flag1 = 1; break; } p++; } if(flag1 == 0) { cout<<"輸入書(shū)號(hào)錯(cuò)誤,還書(shū)失敗!"<<endl; } } //用戶管理菜單 int Mybook::menu_user() { int choice = 0; cout<<"\t\t\t\t=========================================="<<endl; cout<<"\t\t\t\t* 用戶管理 *"<<endl; cout<<"\t\t\t\t*----------------------------------------*"<<endl; cout<<"\t\t\t\t* 1、添加用戶 *"<<endl; cout<<"\t\t\t\t* 2、顯示用戶 *"<<endl; cout<<"\t\t\t\t* 3、刪除用戶 *"<<endl; cout<<"\t\t\t\t* 4、修改用戶 *"<<endl; cout<<"\t\t\t\t* 5、顯示圖書(shū)借用信息 *"<<endl; cout<<"\t\t\t\t* 6、保存并返回上一層 *"<<endl; cout<<"\t\t\t\t*----------------------------------------*"<<endl; cout<<"\t\t\t\t\t請(qǐng)輸入選擇:"; cin>>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<<endl<<endl<<endl; cout<<"按任意鍵返回..."; getchar(); getchar(); } } //添加用戶 void Mybook::add_user() { User new_user; cout<<"請(qǐng)輸入學(xué)號(hào):"; cin>>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<User>::iterator it = link_user.begin(); while(it != link_user.end()) { cout<<"====================================="<<endl; cout<<"學(xué)號(hào):"<<it->stu_number<<endl; cout<<"姓名:"<<it->stu_name<<endl<<endl; it++; } } //刪除用戶 void Mybook::del_user() { char id[20]; cout<<"請(qǐng)輸入你要?jiǎng)h除的學(xué)生學(xué)號(hào):"; cin>>id; int flag = 0; list<User>::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<<"刪除成功!"<<endl; } else { cout<<"你刪除的用戶不存在,刪除失?。?<<endl; } } //修改用戶信息 void Mybook::update_user() { cout<<"請(qǐng)輸入你要修改的學(xué)號(hào):"<<endl; char number[20]; cin>>number; int flag = 0; list<User>::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<<"修改成功!"<<endl; } else { cout<<"修改失敗!"<<endl; } } //查看已借閱圖書(shū)情況 void Mybook::look_borrow() { int flag = 0; list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { if(p->state == 1) { cout<<"==================================================="<<endl; cout<<"姓名:"<<p->stu_name<<endl; cout<<"學(xué)號(hào):"<<p->stu_number<<endl; cout<<"書(shū)名:"<<p->book_name<<endl; cout<<"書(shū)架號(hào):"<<p->place<<endl; flag = 1; } p++; } if(flag == 1) { cout<<"已查詢圖書(shū)相關(guān)借閱信息"<<endl; } else { cout<<"目前暫時(shí)沒(méi)有相關(guān)圖書(shū)借閱!"<<endl; } } //圖書(shū)管理菜單 int Mybook::menu() { int choice = 0; cout<<"\t\t\t\t\t============================================"<<endl; cout<<"\t\t\t\t\t* 圖書(shū)管理 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 1、添加圖書(shū)信息 2、顯示圖書(shū)信息 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 3、刪除圖書(shū)信息 4、搜索圖書(shū)信息 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 5、更新圖書(shū)信息 6、返回上一層 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t請(qǐng)輸入選擇:"; cin>>choice; while(!(choice >= 1 && choice <= 6)) { cout<<"輸入錯(cuò)誤,請(qǐng)重新輸入:"; cin>>choice; } return choice; } //實(shí)行圖書(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<<endl<<endl<<endl; cout<<"按任意鍵返回....."<<endl; getchar(); getchar(); } } //增加書(shū)本 void Mybook::add_book() { Book new_book; cout<<"請(qǐng)輸入書(shū)號(hào):"; cin>>new_book.id; cout<<"請(qǐng)輸入書(shū)名:"; cin>>new_book.book_name; cout<<"請(qǐng)輸入圖書(shū)書(shū)架:"; 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(); } } //顯示書(shū)本信息 void Mybook::display_book() { list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { cout<<"======================================="<<endl; cout<<"書(shū)號(hào):"<<p->id<<endl; cout<<"書(shū)名:"<<p->book_name<<endl; if(p->state == 0) { cout<<"圖書(shū)狀態(tài):未借閱!"<<endl; } else { cout<<"圖書(shū)狀態(tài):已借閱!"<<endl; } cout<<"所在書(shū)架"<<p->place<<endl<<endl; p++; } } //刪除書(shū)本信息 void Mybook::del_book() { list<Book>::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<"請(qǐng)輸入你要?jiǎng)h除的書(shū)號(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該書(shū)不存在,刪除失??!"<<endl; } } //搜索書(shū)本信息 void Mybook::search_book() { list<Book>::iterator p = link_book.begin(); char book_name[20]; int flag = 0; cout<<"請(qǐng)輸入你要查找的書(shū)名:"; cin>>book_name; while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0) { cout<<"======================================="<<endl; cout<<"書(shū)號(hào):"<<p->id<<endl; cout<<"書(shū)名:"<<p->book_name<<endl; if(p->state == 0) { cout<<"圖書(shū)狀態(tài):未借閱!"<<endl; } else { cout<<"圖書(shū)狀態(tài):已借閱!"<<endl; } cout<<"所在書(shū)架"<<p->place<<endl; flag = 1; } p++; } if(flag == 1) { cout<<"\n\n查找完成!"; } else { cout<<"\n\n目前圖書(shū)館暫無(wú)此書(shū)!"<<endl; } } //修改書(shū)本信息 void Mybook::update_book() { list<Book>::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<"請(qǐng)輸入你要更新的書(shū)號(hào):"; cin>>num; while(p != link_book.end()) { if(p->id == num) { cout<<"請(qǐng)輸入書(shū)名"; cin>>p->book_name; cout<<"請(qǐng)輸入圖書(shū)書(shū)架號(hào):"; cin>>p->place; flag = 1; } p++; } if(flag == 1) { cout<<"\n\n更新完成!"; } else { cout<<"\n\n該書(shū)不存在,更新失敗!"<<endl; } } int main() { Mybook b; b.getmenu(); return 0; }
更多學(xué)習(xí)資料請(qǐng)關(guān)注專(zhuān)題《管理系統(tǒng)開(kāi)發(fā)》。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C++實(shí)現(xiàn)簡(jiǎn)單的圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)源碼
- C++編寫(xiě)實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)館管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)(面向?qū)ο?
- C++利用鏈表實(shí)現(xiàn)圖書(shū)信息管理系統(tǒng)
- C++順序表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)最新版
- C++實(shí)現(xiàn)簡(jiǎn)單版圖書(shū)管理系統(tǒng)
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06一起來(lái)學(xué)習(xí)C++的構(gòu)造和析構(gòu)
這篇文章主要為大家詳細(xì)介紹了C++構(gòu)造和析構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03wince程序防止創(chuàng)建多個(gè)實(shí)例實(shí)現(xiàn)互斥作用
什么時(shí)候用的互斥?當(dāng)你的程序只允許同時(shí)打開(kāi)一個(gè)的時(shí)候,就可以通過(guò)互斥來(lái)實(shí)現(xiàn),下面說(shuō)的互斥,主要是針對(duì)防止程序創(chuàng)建多個(gè)實(shí)例這種情況來(lái)實(shí)現(xiàn)的2014-02-02C語(yǔ)言使用廣度優(yōu)先搜索算法解決迷宮問(wèn)題(隊(duì)列)
這篇文章主要介紹了C語(yǔ)言使用廣度優(yōu)先搜索算法解決迷宮問(wèn)題,結(jié)合迷宮問(wèn)題分析了C語(yǔ)言隊(duì)列廣度優(yōu)先搜索算法的相關(guān)使用技巧,需要的朋友可以參考下2017-09-09C++設(shè)計(jì)模式之組合模式(Composite)
這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之組合模式Composite,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04C#桌面應(yīng)用開(kāi)發(fā)實(shí)現(xiàn)番茄定時(shí)器
本文主要介紹了C#桌面應(yīng)用開(kāi)發(fā)實(shí)現(xiàn)番茄定時(shí)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07用C語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的虛擬機(jī)
這篇文章主要介紹了用C語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的虛擬機(jī),其中棧數(shù)組的部分非常值得學(xué)習(xí),需要的朋友可以參考下2015-07-07