1. 程式人生 > 程式設計 >C++實現圖書管理系統

C++實現圖書管理系統

閒來無事,用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,20);
 infile.getline(new_book.book_name,'\n');
 infile.getline(temp,'\n');
 new_book.state = atoi(temp);
 memset(temp,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;
}

更多學習資料請關注專題《管理系統開發》。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。