这篇文章主要讲解了“C++实现图书管理系统的代码实例”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++实现图书管理系统的代码实例”吧!
创新互联建站专注于金乡网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供金乡营销型网站建设,金乡网站制作、金乡网页设计、金乡网站官网定制、成都小程序开发服务,打造金乡网络公司原创品牌,更为您提供金乡网站排名全网营销落地服务。
闲来无事,用C++做了一个图书管理系统,主要有借书、还书、图书管理、用户管理等功能,主要用到的技术有容器和文件,以及类的封装
#include#include #include
#include #include #include 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 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(); //将数据保存到文件中 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<<"文件打开失败"< ::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<<"输入错误,请重新输入:"; 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<<"正在保存,请稍后....."< >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<<"你没有借书权限!借书失败"< >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<<"图书状态:未借阅!"< 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<<"输入错误,请重新输入:"; 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< >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 ::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<<"请更新学号:"; cin>>it->stu_number; cout<<"请更新姓名:"; 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<<"输入错误,请重新输入:"; 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< >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 ::iterator p = link_book.begin(); while(p != link_book.end()) { cout<<"======================================="< id< book_name< state == 0) { cout<<"图书状态:未借阅!"< place< ::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该书不存在,删除失败!"< ::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<<"======================================="< id< book_name< state == 0) { cout<<"图书状态:未借阅!"< place< ::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该书不存在,更新失败!"< 感谢各位的阅读,以上就是“C++实现图书管理系统的代码实例”的内容了,经过本文的学习后,相信大家对C++实现图书管理系统的代码实例这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
当前题目:C++实现图书管理系统的代码实例
转载来于:http://cdkjz.cn/article/ggdesh.html