C++实现图书管理系统的代码实例
创始人
2024-12-24 15:04:28
0次

闲来无事,用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该书不存在,更新失败!"<                
            

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...