当前位置 : 主页 > 编程语言 > c语言 >

C++实现图书管理系统

来源:互联网 收集:自由互联 发布时间:2021-05-12
闲来无事,用C++做了一个图书管理系统,主要有借书、还书、图书管理、用户管理等功能,主要用到的技术有容器和文件,以及类的封装 #include iostream#include list#include algorithm#include st

闲来无事,用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;
}

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论