c/c++语言开发共享C++实现小型图书管理系统

本文实例为大家分享了c++实现小型图书管理系统的具体代码,供大家参考,具体内容如下因为课程设计的原因,需要实现一个小型图书管理系统包含功能:问题描述:设计一个系统,对图书信息进行管理,信息描述:有关该

c/c++开发分享C++实现小型图书管理系统实例为大家分享了c++实现小型图书管理系统的具体代码,供大家参考,具体内容如下

因为课程设计的原因,需要实现一个小型图书管理系统

包含功能:

问题描述:

设计一个系统,对图书信息进行管理,信息描述:有关该系统基本信息的描述,如:图书名称、图书编号、单价、作者、存在状态、借书人姓名、性别、学号等。

基本要求:

基本功能:

1、新进图书基本信息的输入。
2、图书基本信息的查询。
3、对撤消图书信息的删除。
4、为借书人办理注册。
5、办理借书手续(非注册会员不能借书)。
6、办理还书手续。
7、统计图书库存、已借出图书数量。

需要创建三个文c/c++开发分享C++实现小型图书管理系统件:record.txt  book.txt reader.txt

operating.h的头文件:

#include <iostream>  #include <fstream>  #include <string>  #include <time.h>   #include<sstream>  #include<vector>  #include <iomanip>     using namespace std;        int all_stock = 0;  int out_stock = 0;  int times=0;  void outdata(vector<string> res,int n)  // n为txt中 每行数据个数  {      for(int i=0;i<res.size();i+=n){          for(int j=0;j<n;j++)              cout<<setw(12)<<res[i+j]<<" ";                    cout<<endl;      }  }     void bookentry()  {      double price;      string bookname,writer;      fstream out;      out.open("book.txt",ios::app);      if(!out)      {          cerr<<"打开文件失败!"<<endl;      }         time_t tt = time(null);//这句返回的只是一个时间cuo            cout<<"请输入书籍名称"<<endl;      cin>>bookname;      cout<<"请输入书籍作者"<<endl;      cin>>writer;      cout<<"请输入书籍价格"<<endl;            while(! (cin>>price)  || price <= 0 )      {          cin.clear();          cin.ignore(100,'n');          cout<<"请输入正确的价格"<<endl;      }            out<<tt<<" "<<bookname<<" "<<writer<<" "<<price<<" "<<"0"<<"n";            out.close();        }        void bookmes()  {      fstream in;      string line;      //用于存放分割后的字符串       vector<string> res;      string temp; //暂存字符串            in.open("book.txt",ios::in);      if(!in)      {          cerr<<"打开文件失败!"<<endl;      }      all_stock = 0;      while(getline(in,line))      {          all_stock++;          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          while(input>>temp)          {                            res.push_back(temp);          }      }      // 0 书籍编号 1 书籍名称 2作者 3价格 4书籍状态      //输出res       cout<<endl<<setw(12)<<"书籍编号"<<" "<<setw(12)<<"书籍名称"<<" "<<setw(12)<<"作者"<<" "<<setw(12)<<"价格"<<" "<<setw(12)<<"在馆0,不在1"<<"n";      outdata(res,5);      in.close();  }        void delbook()  {      string del_book;      string line;      vector<string>res;      string temp;      bool flag=false;            fstream in;      in.open("book.txt",ios::in);      if(!in)      {          cerr<<"打开错误文件"<<endl;      }            cout<<"请输入需要删除的图书id"<<endl;      cin>>del_book;                while(getline(in,line))      {          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          times=0;          while(input>>temp)          {              if(del_book == temp && times==0)              {                                    for(int i=0;i<3;i++)  //因为一共五个 第一个temp已经是del_book 所以这里取得是四个                  {                      input>>temp;                  }                  input>>temp;                  if(temp != "0")                  {                      cout<<"书籍状态不对";                      in.close();                      return ;                  }                  flag=true;                  cout<<"n找到了喔,应该删除成功了n";                  continue;              }              res.push_back(temp);              times++;                        }      }            //outdata(res,5);      in.close();            if(!flag)      {          cout<<"n错误的书籍idn";          return ;      }      fstream out;            out.open("book.txt",ios::out);      if(!out)      {          cerr<<"打开文件失败!"<<endl;      }            for(int j=0;j<res.size();j+=5)      {          line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "n";          out<<line;      }      out.close();        }    void readerentry()  {            string readername,sex_str;      int sex;      fstream out;            out.open("reader.txt",ios::app);      if(!out)      {          cerr<<"打开文件失败!"<<endl;      }        time_t readerid = time(null);//这句返回的只是一个时间cuo            cout<<"请输入读者姓名"<<endl;      cin>>readername;            do      {          cout<<"请输入读者性别:0为女,1为男"<<endl;          while(! (cin>>sex) )          {              cin.clear();              cin.ignore(100,'n');              cout<<"请输入正确的0或1"<<endl;          }      }while(sex != 0 && sex!=1);            if(sex == 1)      {          sex_str = "男";      }else if (sex == 0){          sex_str = "女";      }else{          out.close();          return ;      }        out<<readerid<<" "<<readername<<" "<<sex_str<<"n";            out.close();        }  /*读者信息*/  void readermes()  {      fstream in;      string line;      //用于存放分割后的字符串       vector<string> res;      string temp; //暂存字符串      in.open("reader.txt",ios::in);      if(!in)      {          cerr<<"打开文件失败!"<<endl;      }            while(getline(in,line))      {          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          while(input>>temp)          res.push_back(temp);      }      // 0读者学号 1读者姓名 2读者性别      //输出res       cout<<endl<<setw(12)<<"读者编号"<<" "<<setw(12)<<"读者"<<" "<<setw(12)<<"性别"<<"n";      outdata(res,3);      in.close();  }       /* 借阅书籍 */  void borrowbook()  {      string book[5];      string readerid;      string readername;      string line;      vector<string>res; //取书籍状况,并且更新            string temp;      bool flag_book = false; //用于判断书籍是否存在  读者是否存在      bool flag_reader = false;            /* 取book的图书情况,并判断是否在馆*/      fstream in;      in.open("book.txt",ios::in);      if(!in)      {          cerr<<"打开错误文件"<<endl;      }        cout<<"请输入需要借的图书id"<<endl;      cin>>book[0];            while(getline(in,line))      {          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          times=0;          while(input>>temp)          {              if(book[0] == temp && times ==0)              {                  res.push_back(temp);                  for(int i=0;i<3;i++)  //从书籍名称开始取,一直取到价钱                  {                      input>>temp; //读取了书籍编号,要及时写入res,以后要写进文本                      book[1+i]=temp;                      res.push_back(temp);                  }                  input>>temp;  //取书籍状态,如果0在馆 如果1不在馆                  if(temp == "0")                  {                      book[4]="1";                      temp="1";                      res.push_back(temp);                      flag_book=true;                  }else{                      cout<<"n书籍不在馆n";                      in.close();                      return ;                  }                  continue;  //继续取              }              res.push_back(temp);              times++;                        }      }        in.close();      if(!flag_book)      {          cout<<"错误的书籍id"<<endl;          return ;      }            in.open("reader.txt",ios::in);      if(!in)      {          cerr<<"打开错误文件"<<endl;      }      cout<<"n请输入读者idn";      cin>>readerid;            while(getline(in,line))      {          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          times=0;          while(input>>temp)          {              if(readerid == temp && times==0)              {                  input>>temp;                  readername=temp;                  flag_reader=true;                  break;                    }              times++;                        }      }      if(!flag_reader)      {          cout<<"错误的读者id"<<endl;          in.close();          return ;      }         in.close();               fstream out;      out.open("record.txt",ios::app);      if(!out)      {          cerr<<"打开错误文件"<<endl;      }      line = book[0] + " " + book[1] + " " + readername + 'n';      out<<line;      cout<<"n办理借书成功n";      out.close();      out.open("book.txt",ios::out);      if(!out)      {          cerr<<"打开文件失败!"<<endl;      }            for(int j=0;j<res.size();j+=5)      {          line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "n";          out<<line;      }      out.close();  }     void borrowmes()  {      fstream in;      string line;      //用于存放分割后的字符串       vector<string> res;      string temp; //暂存字符串      in.open("record.txt",ios::in);      if(!in)      {          cerr<<"打开文件失败!"<<endl;      }      out_stock=0;      while(getline(in,line))      {          out_stock++;          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          while(input>>temp)          res.push_back(temp);      }      // 0书籍编号 1书籍名称 2读者姓名      //输出res       cout<<endl<<setw(12)<<"书籍编号"<<" "<<setw(12)<<"书籍名称"<<" "<<setw(12)<<"读者"<<"n";      outdata(res,3);            in.close();  }     void rtnbook()  {      string rtn_book;      string line;      vector<string>res;      string temp;      bool flag=false;            fstream in;      in.open("record.txt",ios::in);  //先打开record 查看是否有借这本书      if(!in)      {          cerr<<"打开错误文件"<<endl;      }        cout<<"请输入需要归还的书籍id"<<endl;      cin>> rtn_book;                while(getline(in,line))      {          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          times=0;          while(input>>temp)          {              if(rtn_book == temp && times==0) //如果有的话              {                  flag=true;                                    for(int i=0;i<2;i++)  //因为一共三个 第一个temp已经是del_book 所以这里取得是两个                  {                      input>>temp;// 将删除的东西不输出到向量中                  }                  continue;              }              res.push_back(temp);              times++;                        }      }            //outdata(res,3);      in.close();      if(!flag)      {          cout<<"该图书不存在或者没有被外借"<<endl;          return ;      }         fstream out;         out.open("record.txt",ios::out); //record已经删除成功      if(!out)      {          cerr<<"打开文件失败!"<<endl;      }            for(int j=0;j<res.size();j+=3)      {          line = res[j] + " " + res[j+1] + " " + res[j+2]  + "n";          out<<line;      }      out.close();         vector<string>res_book;        in.open("book.txt",ios::in); //开始取 被修改的书籍      if(!in)      {          cerr<<"打开错误文件"<<endl;      }                while(getline(in,line))      {          //cout<<line<<endl;          //将字符串读到input中           stringstream input(line); //将line切割 通过input存入temp,然后存入res中          times=0;          while(input>>temp)          {              if(rtn_book == temp && times==0)              {                  res_book.push_back(temp);                  for(int i=0;i<3;i++)  //因为一共五个 第一个temp已经是rtn_book 所以这里取得是四个                  {                      input>>temp;                      res_book.push_back(temp);                  }                  input>>temp;//最后一个取得是书籍状态,需要修改书籍状态                  temp = "0";                  res_book.push_back(temp);                  continue;              }              res_book.push_back(temp);              times++;          }      }            //outdata(res,5);      in.close();            out.open("book.txt",ios::out); //再存入文本中;      if(!out)      {          cerr<<"打开文件失败!"<<endl;      }            for(int j=0;j<res_book.size();j+=5)      {          line = res_book[j] + " " + res_book[j+1] + " " + res_book[j+2] + " " + res_book[j+3] + " " + res_book[j+4] + "n";          out<<line;      }      out.close();            cout<<"n找到了喔,应该还书成功了n";  }        void countbook()  {          cout<<"n图书馆书籍情况";      bookmes();      cout<<"图书馆一共有:"<<all_stock<<" 本书nnn";      cout<<"n图书馆书籍外借情况";      borrowmes();      cout<<"图书馆目前外借:"<<out_stock<<" 本书nn";      cout<<"nn图书馆当前在馆书籍还有:"<<all_stock - out_stock<<" 本书n";  }

main.cpp的主函数

#include "operating.h"     int main()  {      int order;      do      {          order = -1;          cout<<"n";          cout<<"----------------------------------------------------------n";          cout<<"| 1. 图书信息录入    2. 图书信息查询    3. 图书信息删除  |n";          cout<<"| 4. 读者办理注册    5. 读者信息查询    6. 办理借书手续  |n";          cout<<"| 7. 办理还书手续    8  已借出图书      9.统计图书库存  |n";          cout<<"|                                        按 "0"退出    |n";          cout<<"----------------------------------------------------------n";          cout<<"  请输入相应序号进行相应操作:";          cin>>order;          cin.clear();//清除缓冲区中后面的字符          cin.ignore(100,'n');             switch(order)          {          case 1:              bookentry();              break;          case 2:              bookmes();              break;          case 3:              delbook();              break;          case 4:              readerentry();              break;          case 5:              readermes();              break;          case 6:              borrowbook();              break;          case 7:              rtnbook();              break;          case 8:              borrowmes();              break;          case 9:              countbook();              break;          case 0:              break;          default:              cout<<"错误的命令行"<<endl;              break;          }                }while(order != 0);         system("pause");      return 0;           }

以上就是c/c++开发分享C++实现小型图书管理系统的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多c/c++开发分享C++实现小型图书管理系统,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1082370.html

(0)
上一篇 2022年4月24日
下一篇 2022年4月24日

精彩推荐