您好,登錄后才能下訂單哦!
閑來無事,用C++做了一個圖書管理系統,主要有借書、還書、圖書管理、用戶管理等功能,主要用到的技術有容器和文件,以及類的封裝
#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; //書號 char book_name[20]; //書名 int state; //圖書狀態 char place[20]; //圖書所在位置 char stu_number[20]; //學號 char stu_name[20]; //學生姓名 public: Book(); friend class Mybook; }; class User{ public: char stu_number[20]; //學號 char stu_name[20]; //學生姓名 public: User() { strcpy(stu_number,"000"); strcpy(stu_name,"0"); } friend class Mybook; }; class Mybook{ private: list<Book> link_book; //保存書本信息 list<User> 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(); //將數據保存到文件中 void read_file(); //將數據從文件中讀取 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<<"文件打開失敗"<<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<<"文件打開失敗"<<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(); } //讀取數據至用戶 void Mybook::read_user() { ifstream infile("user.txt",ios::in); if(!infile) { cout<<"文件打開失敗!"<<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* 歡迎來到圖書管理系統 *"<<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* 0、保存并退出 *"<<endl; cout<<"\t\t\t\t*__________________________________________________*"<<endl; cout<<"\t\t\t\t\t請輸入選擇:"; cin>>choice; while(!(choice >= 0 && choice <= 4)) { cout<<"輸入錯誤,請重新輸入:"; cin>>choice; } return choice; } //執行主菜單 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<<"正在保存,請稍后....."<<endl; recv_file(); exit(0); } } cout<<endl<<endl<<endl; cout<<"按任意鍵返回..."; getchar(); getchar(); } } //借書 void Mybook::borrow_book() { int flag = 0; int flag1 = 0; int flag2 = 0; char id[20]; char name[20]; char book_name[20]; cout<<"請輸入學號:"; 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<<"你沒有借書權限!借書失敗"<<endl; return ; } cout<<"請輸入書名:"; 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<<"書號:"<<p->id<<endl; cout<<"書名:"<<p->book_name<<endl; if(p->state == 0) { cout<<"圖書狀態:未借閱!"<<endl; } else { cout<<"圖書狀態:已借閱!"<<endl; } cout<<"所在書架"<<p->place<<endl; flag1 = 1; } p++; } if(flag1 == 0) { cout<<"你所借閱的書不存在,借書失敗!"; return ; } p = link_book.begin(); cout<<"已查找該圖書,請輸入書號進行借閱:"; 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<<"借書成功!"<<endl; flag = 1; break; } p++; } if(flag == 0) { cout<<"該書已被借閱,借書失敗!"; } } //還書 void Mybook::return_book() { char stu_id[20]; cout<<"請輸入學號"; 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<<"書號:"<<p->id<<endl; cout<<"書名:"<<p->book_name<<endl; flag = 1; } p++; } if(flag == 0) { cout<<"當前沒有您沒有借書"<<endl; return ; } else if(flag == 1) { cout<<"已經查詢到你借閱的圖書,請輸入書號進行歸還:"<<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<<"還書成功!"<<endl; flag1 = 1; break; } p++; } if(flag1 == 0) { cout<<"輸入書號錯誤,還書失敗!"<<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、顯示圖書借用信息 *"<<endl; cout<<"\t\t\t\t* 6、保存并返回上一層 *"<<endl; cout<<"\t\t\t\t*----------------------------------------*"<<endl; cout<<"\t\t\t\t\t請輸入選擇:"; cin>>choice; while(!(choice >= 1 && choice <= 6)) { cout<<"輸入錯誤,請重新輸入:"; cin>>choice; } return choice; } //執行用戶管理 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<<"請輸入學號:"; cin>>new_user.stu_number; cout<<"請輸入姓名:"; cin>>new_user.stu_name; link_user.push_back(new_user); cout<<"添加成功!"; cout<<"是否繼續添加(y/n)"; char ch; cin>>ch; while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n')) { cout<<"輸入有誤,請重新輸入:"; 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<<"學號:"<<it->stu_number<<endl; cout<<"姓名:"<<it->stu_name<<endl<<endl; it++; } } //刪除用戶 void Mybook::del_user() { char id[20]; cout<<"請輸入你要刪除的學生學號:"; 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<<"請輸入你要修改的學號:"<<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<<"請更新學號:"; cin>>it->stu_number; cout<<"請更新姓名:"; cin>>it->stu_name; flag = 1; break; } it++; } if(flag == 1) { cout<<"修改成功!"<<endl; } else { cout<<"修改失敗!"<<endl; } } //查看已借閱圖書情況 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<<"學號:"<<p->stu_number<<endl; cout<<"書名:"<<p->book_name<<endl; cout<<"書架號:"<<p->place<<endl; flag = 1; } p++; } if(flag == 1) { cout<<"已查詢圖書相關借閱信息"<<endl; } else { cout<<"目前暫時沒有相關圖書借閱!"<<endl; } } //圖書管理菜單 int Mybook::menu() { int choice = 0; cout<<"\t\t\t\t\t============================================"<<endl; cout<<"\t\t\t\t\t* 圖書管理 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 1、添加圖書信息 2、顯示圖書信息 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 3、刪除圖書信息 4、搜索圖書信息 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 5、更新圖書信息 6、返回上一層 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t請輸入選擇:"; cin>>choice; while(!(choice >= 1 && choice <= 6)) { cout<<"輸入錯誤,請重新輸入:"; cin>>choice; } return choice; } //實行圖書管理 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(); } } //增加書本 void Mybook::add_book() { Book new_book; cout<<"請輸入書號:"; cin>>new_book.id; cout<<"請輸入書名:"; cin>>new_book.book_name; cout<<"請輸入圖書書架:"; cin>>new_book.place; link_book.push_back(new_book); cout<<"添加成功!"; cout<<"是否繼續添加(y/n)"; char ch; cin>>ch; while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n')) { cout<<"輸入有誤,請重新輸入:"; cin>>ch; } if(ch == 'Y'||ch == 'y') { system("clear"); add_book(); } } //顯示書本信息 void Mybook::display_book() { list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { cout<<"======================================="<<endl; cout<<"書號:"<<p->id<<endl; cout<<"書名:"<<p->book_name<<endl; if(p->state == 0) { cout<<"圖書狀態:未借閱!"<<endl; } else { cout<<"圖書狀態:已借閱!"<<endl; } cout<<"所在書架"<<p->place<<endl<<endl; p++; } } //刪除書本信息 void Mybook::del_book() { list<Book>::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<"請輸入你要刪除的書號:"; 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該書不存在,刪除失敗!"<<endl; } } //搜索書本信息 void Mybook::search_book() { list<Book>::iterator p = link_book.begin(); char book_name[20]; int flag = 0; cout<<"請輸入你要查找的書名:"; cin>>book_name; while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0) { cout<<"======================================="<<endl; cout<<"書號:"<<p->id<<endl; cout<<"書名:"<<p->book_name<<endl; if(p->state == 0) { cout<<"圖書狀態:未借閱!"<<endl; } else { cout<<"圖書狀態:已借閱!"<<endl; } cout<<"所在書架"<<p->place<<endl; flag = 1; } p++; } if(flag == 1) { cout<<"\n\n查找完成!"; } else { cout<<"\n\n目前圖書館暫無此書!"<<endl; } } //修改書本信息 void Mybook::update_book() { list<Book>::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<"請輸入你要更新的書號:"; cin>>num; while(p != link_book.end()) { if(p->id == num) { cout<<"請輸入書名"; cin>>p->book_name; cout<<"請輸入圖書書架號:"; cin>>p->place; flag = 1; } p++; } if(flag == 1) { cout<<"\n\n更新完成!"; } else { cout<<"\n\n該書不存在,更新失敗!"<<endl; } } int main() { Mybook b; b.getmenu(); return 0; }
更多學習資料請關注專題《管理系統開發》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。