zoukankan      html  css  js  c++  java
  • 用C++面向对象做的图书管理系统

       祝您有个好心情。。

    问题:

    图书借阅系统
    要求:
    l 设计一个类book,数据成员有书名name,编号no,作者author;
    l 设计一个类reader,数据成员有学号number,姓名name,借书证号no;
    l 每位读者最多借5本书;输入学生学号,可显示学生姓名和学生的借书情况;
    l 实现学生还书功能;
    l 图书信息、读者信息和借阅信息均存于数据文件中。

    1,请将 reader.txt,books.txt,yuedu.txt这三个文本文档添加到D盘中。。程序运行时会出现一个temp.txt文档,它也会动添加到D盘中的。。。

    books.txt

    1  JAVA基础入门  孙鑫
    2  计算机组成原理  百中英
    3  C语言程序设计  谭浩强
    4  C++程序语言设计  刘怀亮
    5  ASP.net3.5  刘好增
    6  C#程序设计  孙鑫
    7  大学英语   陈美
    8  哲学与儒学  张世高
    9  计算机仿真  周才根
    10  MSDN自学教程 唐仕喜

    reader.txt

    1 zhangyongbin  111111
    2 leiyu 22222
    3 fanyouyu 3333333

    yuedu.txt

    1
    1 2 3 4 99
    2
    2  3  4  5  99
    3
    3  4  5  99
    源代码:

    包括:books.h  books.cpp    reader.h    reader.cpp  学生信息管理系统.cpp

    books.h中的代码:

    #ifndef xxx
    #define xxx
    class books{
    private:
     int no;//定义了一个书本的编号
     char name[20];//定义了书本的名称
     char author[20];//定义了书本的作者
    public:
     void showbooks(int i);//用来显示数据库中所有的书本
    };
    #endif

    books.cpp   中包括:

    #include"books.h"
    #include<fstream.h>
    #include<stdlib.h>
    #include<string.h>
    #include<iostream.h>
    #include<iomanip.h>
    void books::showbooks(int i)
    {
     int no1;
     char name1[20];
     char author1[20];
     //char temp[80];
     int temp1;
     ifstream ifp("D:\\books.txt",ios::in);
     if(!ifp.is_open())
     {
      cout<<"找不到图书数据库"<<endl;
      exit(1);
     }
     for(temp1=1;temp1<i;temp1++)//跳过之前的书对象的信息
     {
      ifp>>no1;
         ifp>>name1;
         ifp>>author1;
     }
     ifp>>no;
     ifp>>name;
     ifp>>author;
     cout<<"图书编号:"<<setw(5)<<no;
     cout<<"   图书名:"<<setw(20)<<name;
     cout<<setw(10)<<"   图书作者:";
     cout<<setw(10)<<author<<endl;
     ifp.close();
    }

    reader.h 中包括:

    #include"books.h"
    #ifndef xx
    #define xx
    class reader:public books{
    private:
     bool Is_reader;//用于表示学生是否有过借阅的经历
     int all;//显示学生已经借阅了多少本书籍
     int number;//显示学生的学号
     char name[20];//显示学生的姓名
     char no[10];//显示学生的借书证号
     books bks[5];//显示学生的借阅的书名的对象数组
    public:
     void Get_number(int number);//获取
     int Get_all();//获取学生当前一共借了多少本书
     bool Get_Isreader();//用来显示学生是不是第一次借阅书籍。true为不是,false为是。
     void show_personal_information();//用来显示学生的基本信息
     void show_personal_jieyue();//用来显示用户的借阅书本的信息
     void Addbooks();//用来增加书本
     void Delbooks();//用来减少书本
    };
    #endif

     reader.cpp 中包括:

    #include"reader.h"
    #include<fstream>
    #include<iostream>
    #include<stdlib.h>
    #include<string>
    using namespace std;
    void reader::Get_number(int number)
    {
     this->number=number;
     this->all=0;
     this->Is_reader=false;

    }
    int reader::Get_all()
    {
     return this->all;
    }
    bool reader::Get_Isreader()
    {
     return this->Is_reader;
    }
    void reader::show_personal_information()
    {

     int temp=0;
     int number1;
     char name1[20];
     char no1[10];
     ifstream irfp("D:\\reader.txt",ios::in);
     if(!irfp.is_open())
     {
      cout<<"没有发现用户数据库"<<endl;
      cout<<"你还没有借书"<<endl;  exit(1);
     }
     while(!irfp.eof())
     {
      irfp>>number1;
      irfp>>name1;
      irfp>>no1;
      if(number1==this->number)
      {
       this->Is_reader=true;
       number=number1;
       strcpy(name,name1);
       strcpy(no,no1);
       cout<<"学生信息为:"<<endl;
       cout<<"学号:   "<<number<<"  姓名:  "<<name<<"   证件编号: "<<no<<endl;
       temp=1;
       break;
      }
     }
     if(temp==0)
     {
      cout<<"你还没有借书记录"<<endl;
     }
     irfp.close();
    }
    void reader::show_personal_jieyue()
    {
     int out=0;
     int books[5];//用来放五本书的序号
     int temp;
     int number1;
     ifstream iyfp("D:\\yuedu.txt",ios::in);
     if(!iyfp.is_open())
     {
      cout<<"没有发现用户阅读数据库"<<endl;
      cout<<"你还没有借书阅读的记录"<<endl;
      exit(1);
     }
     while(!iyfp.eof())
     {
      int i=0;
      int j=0;
      iyfp>>number1;
      iyfp>>temp;
      while(temp!=99)
      {
       books[i]=temp;
       i++;
       iyfp>>temp;
      }
      if(number1==this->number)
      {
       this->Is_reader=true;
       for(j=0;j<i;j++)
       {
        bks[j].showbooks(books[j]);
       }
       out=1;
       this->all=i;//对读者的对象变量进行赋值
       int loo;
       loo=5-all;
       cout<<"你目前所借的书籍数目有:"<<i<<"本!!!"<<"您需要还"<<this->all<<"书!!! "<<"   您还能借"<<loo<<"本!!!"<<endl;
       break;
      }
      i=0;
      for(int jj=0;jj<5;jj++)
      {
       books[jj]=0;
      }
     }
     if(out==0)
      cout<<"您还没有借阅书籍"<<endl;
     iyfp.close();
    }

    void reader::Addbooks()
    {
     bool pub=false;
     bool rr=false;
     int bianhao;
     string str;
     int kk[5];


     ifstream ifp("D:\\books.txt",ios::in);
     if(!ifp.is_open())
     {
      cout<<"找不到图书数据库"<<endl;
      exit(1);
     }
     //显示您能借到的书籍
     cout<<"目前您能借到的书籍为:"<<endl;
     while(getline(ifp,str))
     {
      cout<<str<<endl;
     }
     ifp.close();

     //如果你是第一次借书的话,那么我得把你的信息加到我的数据库中
     if(this->Is_reader==false)
     {
      ofstream ofp("D:\\reader.txt",ios::out|ios::app);
      if(!ofp.is_open())
      {
       cout<<"没有找到文件"<<endl;
      }
      cout<<endl<<"由于您是第一次登入到盐城师范学院图书馆,所以请先输入您的个人信息"<<endl;
      //this->Is_reader=true;
      pub=true;
      this->all=0;
      ofp<<'\n';
      ofp<<this->number;
      ofp<<"  ";
      cout<<"请输入您的姓名:"<<endl;
      cin>>this->name;
      ofp<<this->name;
      ofp<<"  ";
      cout<<"请输入您的借书证号"<<endl;
      cin>>this->no;
      ofp<<this->no;
      ofp.close();
     }

     //进行借书的过程
     cout<<endl<<"请输入你想借的书籍的编号:"<<endl;
     cin>>bianhao;
     ifstream temp_yuedusource("D:\\yuedu.txt",ios::in);
     ofstream temp_tempsource("D:\\temp.txt",ios::out);
     if(!temp_yuedusource.is_open())
     {
      cout<<"没有发现文件"<<endl;
      return;
     }
     int temp_number;
     int ss;
     int i=0;


     while(temp_yuedusource>>temp_number)
     {

      temp_yuedusource>>ss;
      while(ss!=99)
      {
       kk[i]=ss;
       i++;
       temp_yuedusource>>ss;
      }
      
      if(temp_number==number)
      {
       pub=true;
       rr=true;
       kk[i]=bianhao;
       kk[i+1]=99;
       break;
      }
      kk[i]=99;
      //当没有在yuedu.txt中找到读者的记录的时候
      temp_tempsource<<temp_number;
      temp_tempsource<<'\n';
      i=0;
      while(kk[i]!=99)
      {
       temp_tempsource<<kk[i];
       temp_tempsource<<"  ";
                i++;
      }
      temp_tempsource<<kk[i];
      //回复初始状态
      i=0;
      for(int j=0;j<5;j++)
      {
       kk[j]=0;
      }
      temp_tempsource<<'\n';
     }


        //将记录的后面部分写入零时book文件中
     string std;
     while(getline(temp_yuedusource,std))
     {
      temp_tempsource<<std;
      temp_tempsource<<'\n';
     }
        //将添加的一行添加到yuedu.txt文件中

     if(rr==true||pub==true)
     {
      //无论你是否为第一次添加到数据库,你都要将你的学号添加到零时阅读文件中
      temp_tempsource<<this->number;
         temp_tempsource<<'\n';
            //当你是第一次添加数据的时候,不能用kk这个数字来添加零时阅读文件
      if(Is_reader==false)
      {
       temp_tempsource<<bianhao;
       temp_tempsource<<"  ";
       temp_tempsource<<99;

      }
      //如果你不是第一次添加阅读数据的时候,就要用到kk来添加数的编号
      else
      {

           i=0;
           while(kk[i]!=99)
        {
               temp_tempsource<<kk[i];
            temp_tempsource<<"  ";
         i++;  
        }
           temp_tempsource<<99;

      }
         temp_tempsource.close();
         temp_yuedusource.close();
        ////
     }
     ofstream aa("D:\\yuedu.txt",ios::in);
     ifstream bb("D:\\temp.txt",ios::out);
     if((!aa.is_open())||(!bb.is_open()))
     {
      cout<<"没有发现文件"<<endl;
      return;
     }
     //将零时文件从新写入到yuedu.txt文件中
     string qq;
     while(getline(bb,qq))
     {
      aa<<qq;
      aa<<'\n';
     }
     cout<<"添加书籍成功"<<endl;
     aa.close();
     bb.close();
    }


    void reader::Delbooks()
    {
     int kk[5];//用来存放书的编号
     bool rr=false;//若为true则你输入的学号存在我的yuedu.txt文件中
     int bianhao;//用来存放你想要删除的书的编号
     cout<<endl<<"请输入那本你要还书的编号"<<endl;
     cin>>bianhao;
     ifstream temp_yuedusource("D:\\yuedu.txt",ios::in);
     ofstream temp_tempsource("D:\\temp.txt",ios::out);
     if(!temp_yuedusource.is_open())
     {
      cout<<"没有发现文件"<<endl;
      return;
     }
     //将yuedu.txt中的内容填入到文件temp.txt文件中
     int temp_number;
     int ss;
     int i=0;
     while(temp_yuedusource>>temp_number)
     {

      temp_yuedusource>>ss;
      while(ss!=99)
      {
       kk[i]=ss;
       i++;
       temp_yuedusource>>ss;
      }
      //你的学号在我的yuedu.txt文件中
      if(temp_number==number)
      {
       rr=true;
          kk[i]=99;
       break;
      }
      kk[i]=99;

    //当没有在yuedu.txt中还没有到读者的记录的时候,继续读下一行来判断,但是要把我现在读的这行记录写到temp.txt文件中
      temp_tempsource<<temp_number;
      temp_tempsource<<'\n';
      i=0;
      while(kk[i]!=99)
      {
       temp_tempsource<<kk[i];
       temp_tempsource<<"  ";
                i++;
      }
      temp_tempsource<<kk[i];
    //回复初始状态
      i=0;
      for(int j=0;j<5;j++)
      {
       kk[j]=0;
      }
      temp_tempsource<<'\n';
     }
     if(rr==true)
     {
        //将记录的后面部分写入零时book文件中
      string std;
         while(getline(temp_yuedusource,std))
      {
      temp_tempsource<<std;
      temp_tempsource<<'\n';
      }


     //用来将你输入要还得数的编号从我的记录中删除
      int x=0,y=0;
      for(x=0;kk[x]!=99;x++)
      {
       if(kk[x]==bianhao)
       {
        y=1;break;
       }

      }
      if(y==0)
      {
       cout<<"您输入的图书编号,不在你的图书阅读的列表中"<<endl;
       return;
      }
      int z=0;
      for(z=x;kk[z]!=99;z++)
      {
       kk[z]=kk[z+1];
      }
    //运用kk数组将学生的新纪录添加到temp.txt的末尾
      temp_tempsource<<temp_number;
         temp_tempsource<<'\n';
          i=0;
          while(kk[i]!=99)
       {
                temp_tempsource<<kk[i];
             temp_tempsource<<"  ";
          i++;  
        }
         temp_tempsource<<99;
         temp_tempsource.close();
         temp_yuedusource.close();

    //将我的temp.txt文件返回到我的yuedu.txt文件中

         ofstream aa("D:\\yuedu.txt",ios::in);
         ifstream bb("D:\\temp.txt",ios::out);
         if((!aa.is_open())||(!bb.is_open()))
      {
           cout<<"没有发现文件"<<endl;
          return;
      }
    //temp.txt中的文件一行一行的复制到yuedu.txt文件中
         string qq;
         while(getline(bb,qq))
      {
           aa<<qq;
           aa<<'\n';
      }
         cout<<"删除书籍成功"<<endl;
         aa.close();
         bb.close();

     }
     else
      cout<<"没有发现你有阅读的信息!请确认你是否借过书!!!!"<<endl;


    }

    学生信息管理系统.cpp中包括:

    #include<iostream.h>
    #include<string.h>
    #include<fstream.h>
    #include<stdlib.h>
    #include"reader.h"
    #include"books.h"
    void read_need()
    {
     cout<<"首先欢迎您进入本图书系统"<<endl;
     cout<<"每个同学只能借五本书,超过五本就不能再借了,须及时还书才能继续借书"<<endl;
     cout<<"每本书都有时间限制,你必须在一个月内还书,超过一个月你可以续借,但是超过两个月,我们就将于你罚钱"<<endl;
    }
    void Look_books()
    {
     int i=1;
     books Shu[10];
     cout<<"本图书目前一共有十本书,分别为:"<<endl;
     for(i=0;i<10;i++)
     {
      Shu[i].showbooks(i+1);//用书这个对象数组中的每一个对象,对每个对象进行访问他的showbooks对象函数
     }
    }
    void Personal_infomation()
    {
     int num;
     cout<<"请输入您的学号"<<endl;
     cin>>num;
     reader reader_temp;
     reader_temp.Get_number(num);
     reader_temp.show_personal_information();
     reader_temp.show_personal_jieyue();
    }
    void reader_Addbooks()
    {
     int temp_all;
     int num;
     int pp;
     cout<<"请输入您的学号"<<endl;
     cin>>num;
     reader reader_temp;
     reader_temp.Get_number(num);
     reader_temp.show_personal_information();
     reader_temp.show_personal_jieyue();
     temp_all=reader_temp.Get_all();
     pp=5-temp_all;
     if(pp>0)
     {
      reader_temp.Addbooks();

     }
     else
       cout<<endl<<"您已经借书超过五本,请及时还书!!!"<<endl;
    }
    void reader_Delbooks()
    {
     int num;
     cout<<"请输入您的学号"<<endl;
     cin>>num;
     reader reader_temp;
     reader_temp.Get_number(num);
     reader_temp.show_personal_information();
     if(!reader_temp.Get_Isreader())
     {
      cout<<"您没有借书,当然不需要还书"<<endl;
      return;
     }
     reader_temp.show_personal_jieyue();
     reader_temp.Delbooks();
    }
    void main()
    {
     int out=1;
     int choose;
     for(;;)
     {
      cout<<"欢迎来到盐城师范学院图书管理系统"<<endl;
      cout<<"1    借阅须知"<<endl;
      cout<<"2    图书馆珍藏书籍"<<endl;
      cout<<"3    学生个人信息查询"<<endl;
      cout<<"4    学生借书"<<endl;
      cout<<"5    学生还书"<<endl;
      cout<<"6    退出本系统"<<endl;
      cout<<"请选择"<<endl;
      cin>>choose;
      switch(choose)
      {
      case 1:
       cout<<endl;
       system("cls");
       read_need();
       break;
      case 2:
       cout<<endl;
       system("cls");
       Look_books();
       break;
      case 3:
       cout<<endl;
       system("cls");
       Personal_infomation();
       break;
      case 4:
       cout<<endl;
       system("cls");
       reader_Addbooks();
       break;
      case 5:
       cout<<endl;
       system("cls");
       reader_Delbooks();
       break;
      case 6:
       out=0;
       break;
      }
      if(out==0)
       break;
     }
    }

    寻找21世纪的伯牙
  • 相关阅读:
    POJ 3009
    POJ 3253
    POJ 3617
    POJ 3984
    UVA10012
    HDU5100
    HDU 5101
    UVA301 运输
    UVA 331 交换的方案数
    uva 10344 算23点
  • 原文地址:https://www.cnblogs.com/2814/p/1757814.html
Copyright © 2011-2022 走看看