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;
 
    
}

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

(0)

相关推荐

  • C++实现图书管理系统

    闲来无事,用C++做了一个图书管理系统,主要有借书.还书.图书管理.用户管理等功能,主要用到的技术有容器和文件,以及类的封装 #include <iostream> #include <list> #include <algorithm> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; class Mybook; class

  • C++实现图书馆管理系统

    本文实例为大家分享了C++实现图书馆管理系统的具体代码,供大家参考,具体内容如下 一.实验名称 图书馆管理系统 二.实验目的 利用C++语言设计开发一个小型的图书馆管理系统模拟程序,具有如下功能:退出系统.增加图书.删除图书.借阅图书.归还图书.显示图书信息.查询图书等功能.实验中应掌握继承结构,并掌握对象.类.链表的使用和成员函数.构造函数的定义及调用,并掌握使用实验设备的技能技巧和程序的调试方法. 三.实验平台 运行环境:VC++6.0 四.问题分析 图书馆管理系统模拟程序可划分为7个模块:

  • C++实现简单图书馆管理系统

    本文实例为大家分享了C++实现简单图书馆管理系统的具体代码,供大家参考,具体内容如下 写了一个小项目,图书馆系统,功能如下: 1,添加书籍2,删除书籍(可删除还没外借的书籍)3,读者借书4,读者还书5,按书籍登入号查看信息(每一本书的书籍登入号唯一,如有5本相同书名作者的书,那就有5个不同的书籍登入号)6,查询所有图书信息(可以直接查看到同本书在图书馆中的剩余和借出情况)7,查看指定读者的借书详情8,注册新读者9,查看所有书籍信息(可以详细到每个登入号和此登入号书籍的借阅情况:如谁借的,借阅日期

  • C++利用链表实现图书信息管理系统

    C++利用链表实现一个简单的图书信息管理系统,供大家参考,具体内容如下 (1)图书信息包括ISBN号.书名.作者名.价格和数量等: (2)系统的主要功能包括:图书信息的创建.输出图书信息.查询图书信息.增加图书信息.删除图书信息. #include <stdio.h> #include <stdlib.h> #include <string.h> //创建结构体及其成员 typedef struct Node { int num;//编号 char name[20];/

  • C++课程设计之图书馆管理系统

    本文实例为大家分享了C++课程设计之图书馆管理系统的具体代码,供大家参考,具体内容如下 一.代码 #include<bits/stdc++.h> using namespace std; class Date {     int year,month,day; public:     Date(int x,int y,int z):year(x),month(y),day(z){}     Date(){year=month=day=0;}     void setDate(int x,int

  • C/C++实现图书信息管理系统

    本文实例为大家分享了c/c++实现图书信息管理系统的具体代码,供大家参考,具体内容如下 程序流程图 源代码 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define N 100 struct type{ char ISBN[N];//ISBN编号(一般为13位) char bookName[N

  • C++实现图书管理系统最新版

    图书管理系统设计,供大家参考,具体内容如下 一.问题描述及功能要求 (1)图书信息录入功能(图书信息用文件保存) (2)图书信息浏览功能 (3)查询和排序功能:(至少一种查询方式) .按书名查询 .按作者名查询 (4)图书信息的删除与修改 二.代码实现 带有注释 废话不说,直接代码,欢迎指正. 大家CV可能有不兼容的情况,可以滴滴,尽可能解决问题地回复. #include<iostream> #include<stdio.h> #include <stdlib.h> #

  • C++实现简易图书馆管理系统

    本文实例为大家分享了C++实现简易图书馆管理系统的具体代码,供大家参考,具体内容如下 思路 在本程序中共有四个类: book类:此类有书的基本信息:书名,编号,作者,价格等,和基本的get()和set()方法.类图如下: library类:此类中有一个存放的书的数组,并且可以对书进行,查询,借阅,归还,添加,等相关操作.类图如下: reader_infor类:此类中有读者的相关信息:名字,学号,年级,借书数量.和基本的get()和set()方法,类图如下: reader_admin类:此类中有一

  • C++实现图书馆管理系统源码

    本文实例为大家分享了C++实现图书馆管理系统的具体代码,供大家参考,具体内容如下 总体思想 用C++开发图书馆管理系统需要对学生和图书分别建立class,调用class中的方法实现学生登陆账号借书,还书,图书馆管理员查看信息等操作. Student.h #pragma once #include<string> #include<vector> #include<iostream> #include<fstream> #include<sstream&

  • C++实现简单的图书管理系统

    今天再为大家介绍另一个常用的管理系统--图书管理系统,希望大家可以亲自动手实践一下,下面就与大家一起分享我的劳动成果. 图书信息包括:登录号.书名.作者名.分类号.出版单位.出版时间.价格等.试设计一图书信息管理系统,使之能提供以下功能: (1)图书信息录入功能(图书信息用文件保存) (2)图书信息浏览功能 (3)查询和排序功能:(至少一种查询方式)         .按书名查询         .按作者名查询 (4)图书信息的删除与修改 分享代码如下 #include<iostream.h>

  • C++顺序表实现图书管理系统

    本文为大家分享了C++顺序表实现图书管理系统的具体代码,供大家参考,具体内容如下 图书信息表包括以下10项常用的基本操作:图书信息表的创建和输出.排序.修改.逆序存储.最贵图书的查找.最爱图书的查找.最佳位置图书的查找.新图书的入库.旧图书的出库.图书去重. 代码: #include<iostream> #include<iomanip> #include<string> using namespace std; //函数结果状态代码 #define OK 1 #def

随机推荐