C++实现小型图书管理系统

 更新时间:2022年03月12日 10:03:36   作者:GroovRain  
这篇文章主要为大家详细介绍了C++实现小型图书管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

包含功能:

问题描述:

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

基本要求:

基本功能:

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

需要创建三个文本文件: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错误的书籍ID\n";
        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请输入读者ID\n";
    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<<" 本书\n\n\n";
    cout<<"\n图书馆书籍外借情况";
    BorrowMes();
    cout<<"图书馆目前外借:"<<out_stock<<" 本书\n\n";
    cout<<"\n\n图书馆当前在馆书籍还有:"<<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语言结构数组实现贪吃蛇小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • C++ STL 序列式容器与配接器的简单使用

    C++ STL 序列式容器与配接器的简单使用

    本文主要介绍了C++ STL 序列式容器与配接器的简单使用,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-06-06
  • VS C++头文件引用提示“未定义标识符”的问题解决

    VS C++头文件引用提示“未定义标识符”的问题解决

    本文主要介绍了VS C++头文件引用提示“未定义标识符”的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • C++类与对象深入之静态成员与友元及内部类详解

    C++类与对象深入之静态成员与友元及内部类详解

    朋友们好,这篇播客我们继续C++的初阶学习,现在对我们对C++的静态成员,友元,内部类知识点做出总结,整理出来一篇博客供我们一起复习和学习,如果文章中有理解不当的地方,还希望朋友们在评论区指出,我们相互学习,共同进步
    2022-06-06
  • C语言指针教程示例详解

    C语言指针教程示例详解

    这篇文章主要为大家介绍了C语言指针教程的示例详解
    2022-02-02
  • C++ delete之静态变量问题详解

    C++ delete之静态变量问题详解

    这篇文章主要为大家详细介绍了C++delete的一些问题,学习如何动态创建对象,动态创建的对象与一般对象的区别,动态创建的对象的初始化以及释放动态分配的内存等知识点,感兴趣的朋友可以参考一下
    2021-09-09
  • 函数式宏定义与普通函数的区别

    函数式宏定义与普通函数的区别

    尽管函数式宏定义和普通函数相比有很多缺点,但只要小心使用还是会显著提高代码的执行效率,毕竟省去了分配和释放栈帧、传参、传返回值等一系列工作,因此那些简短并且被频繁调用的函数经常用函数式宏定义来代替实现
    2013-10-10
  • C/C++根据年月日计算星期几(蔡勒公式篇)

    C/C++根据年月日计算星期几(蔡勒公式篇)

    这篇文章主要给大家介绍了关于C/C++根据年月日计算星期几(蔡勒公式篇)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • C语言将音视频时钟同步封装成通用模块的方法

    C语言将音视频时钟同步封装成通用模块的方法

    视频的时钟基于视频帧的时间戳,由于视频是通过一定的帧率渲染的,采用直接读取当前时间戳的方式获取时钟会造成一定的误差,精度不足,这篇文章主要介绍了c语言将音视频时钟同步封装成通用模块,需要的朋友可以参考下
    2022-09-09
  • 用c语言根据可变参数合成字符串的实现代码

    用c语言根据可变参数合成字符串的实现代码

    本篇文章是对用c语言根据可变参数合成字符串的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05

最新评论