基于数据结构的图书管理系统

来源:互联网 发布:spss数据输入 编辑:程序博客网 时间:2024/05/29 12:58

首先看到的是存储信息的Book.txtRecord.txt的初始内容

 



接下来运行程序,并执行功能1

 


执行功能2来添加10本格林童话,并且此书本来不存在 添加在了末尾。

 


执行功能3,以书证号202150315借阅一本格林童话,归还日期默认两个月后,当前为2017/1/5,则归还时间为2017/3/5. 


执行功能4,以书证号202150316归还的书为1001,系统显示该书归还过期,输入已经缴费,删除记录。

 


最后执行功能5,浏览借书记录会发现,书证号202150315原本那条关于1001的借阅记录不见了,增加了1010的借阅记录.

 

 

 

最后退出程序,再打开Book.txt文件或者不退出再执行功能1,会看到格林童话成功入库10本,并由于借出一本,还剩9本,而1001由于归还一本 相比之下多了一本。

  


// bookManagement.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "bookManagement.h"int main(){while(1){cout<<"\n图书管理系统\n";cout<选择功能:";int f;cin>>f;switch(f){case 1:scan();               break;case 2:insert();                break;case 3:borrow();               break;case 4:give_back();               break;case 5:record();   break;    case 6:exit(1);default:cout<<"输入有误,请重新输入"<#include // 包含setw设置输出格式#include using namespace std;class Information{   public:int num;      //存放书的书号string name;   //书名string aut;    //作者int stock;      //库存Information(){}Information (int num,string name,string aut,int stock){this->num=num;this->name=name;this->aut=aut;this->stock=stock;}    friend ostream& operator<<(ostream& out,Information &book);};ostream& operator<<(ostream& out,Information &book){out<#include // 包含setw设置输出格式#include using namespace std;class Record{   public:int stu_Num;        //存放学生的借书号int book_Num;       //存放借阅的书号string book_Name;   //存放书名string T;Record(){}Record (int stu_Num,int book_Num,string book_Name,string T){this->stu_Num=stu_Num;this->book_Num=book_Num;this->book_Name=book_Name;this->T=T;}    friend ostream& operator<<(ostream& out,Record &stu);Record & operator=(Record &stu){this->book_Num=stu.book_Num;this->stu_Num=stu.stu_Num;this->book_Name=stu.book_Name;this->T=T;return *this;}};ostream& operator<<(ostream& out,Record &stu){out<using namespace std;#includetemplateclass SeqList{private:void init(T values[], int n);         //初始化顺序表public:T *element;                            //动态数组存储顺序表的数据元素int length;                            //顺序表的数组容量int n;                                 //顺序表元素个数SeqList(int length=40);               //构造空表,length指定容量SeqList(int length, T x);             //构造顺序表,length个元素值为xSeqList(T values[], int n);           //构造顺序表,由values数组提供元素~SeqList();                           //析构函数T get(int i);T& operator[](int i);bool empty();                         //判断顺序表是否为空int count();                          //求元素个数void insert(int i, T x);              //插入x作为第i个元素virtual void insert(T x);T remove(int i);                      //删除第i个元素并返回其值void removeALL();                     //删除所有元素virtual int search(T key, int start = 0);//从start位置开始查找keyfriend ostream& operator<<<>(ostream&,SeqList&);      //重载输出流,输出所有元素};//seqlist的构造与析构templateSeqList::SeqList(int length)            //构造一个空表{this->element=new T[length];         //若length<0,c++停止运行this->length=length;this->n = 0;}templateSeqList::SeqList(int length, T x)       //构造一个顺序表,且表内所有元素相同{this->element=new T[length];this->length=this->n = length;for (int i=0;ilength;i++)this->element[i] = x;}templateSeqList::SeqList(T values[], int n)       //构造顺序表,由values数组提供元素,n指定元素个数{this->init(values, n);}templatevoid SeqList::init(T values[], int n)      //初始化顺序表{this->length=n*2;this->element=new T[this->length];this->n=n;for (int i=0;ielement[i]=values[i];}templateSeqList::~SeqList(){delete[]this->element;}templatebool SeqList::empty()                     //判断是否为空,若空返回true{return this->n == 0;}templateint SeqList::count(){return this->n;}templateT SeqList::get(int i){T old=this->element[i];return old;}template  void SeqList::insert(int i, T x)         //在i位置插入X,并进行容错处理{if(i<0)i=0;if(i>this->n)i=this->n;T *temp=this->element;if(this->n==this->length){this->length*=2;this->element=new T[this->length];for(int j=0;jelement[j]=temp[j];}for(int j=this->n-1;j>=i;j--)this->element[j+1]=temp[j];if(temp!= this->element)delete[] temp;this->element[i]=x;this->n++;}templatevoid SeqList::insert(T x){insert(this->n, x);}templateint SeqList::search(T key,int start){for (int i=start; in; i++)if(this->element[i] == key)return i;return -1;}templateT& SeqList::operator[](int i){if (i>=0&&in)return this->element[i];throw out_of_range("i超出范围");}templateT SeqList::remove(int i){if (this->n>0&&i>=0&&ielement[i];for (int j=i;jn;j++)this->element[j]=element[j+1];this->n--;return old;}throw out_of_range("指定参数i超出范围!");           //抛出异常}templatevoid SeqList::removeALL(){this->n=0;}templateostream& operator<<<>(ostream& out,SeqList&list){out<<"(";if (list.n > 0)out<#include // 包含setw设置输出格式#include using namespace std;class Date{   public:int Year;        int Month;       int Day;Date(){}Date(int Year,int Month,int Day){this->Year=Year;this->Month=Month;this->Day=Day;}    friend ostream& operator<<(ostream& out,Date &d);friend istream& operator>>(istream& out,Date &d);};ostream& operator<<(ostream& out,Date &d){out<>(istream& in,Date &d){in>>d.Year>>d.Month>>d.Day;return in;}
#include <iostream>#include <fstream>#include "SeqList.h"#include "Information.h"#include "Record.h"#include <time.h>using namespace std;int scan(){cout<<setw(5)<<"书号"<<setw(20)<<"书名"<<setw(20)<<"作者"<<setw(5)<<"库存"<<endl;cout<<endl;char buffer[256];    ifstream B_read("Book.txt");    if (! B_read.is_open())    {        cout<<"Error opening B_file"; system("pause");return 0;    }    while (!B_read.eof())    {        B_read.getline(buffer,100);        cout<<buffer<< endl;    }system("pause");    return 0;}int insert(){Information R[20];ifstream B_read("Book.txt");if (! B_read.is_open())    {        cout << "Error opening B_file"; system("pause");return 0;    }int i=0;                                              //读取书库文件,并将文件中数据附给相应的成员函数;while(B_read>>R[i].num){B_read>>R[i].name>>R[i].aut>>R[i].stock;i++;                                              //共有i种书;}B_read.close();                                       //关闭文件;int num,stock;cout<<"\n请输入书本序号:";cin>>num;SeqList<int> book_num;                                //用读取出来的序号数组为线性表提供元素建立索引表;for(int n=0;n<i;n++)book_num.insert(R[n].num);if(book_num.search(num,0)+1)                          //SeqList<T>::search,若未查找到,返回-1;{cout<<"该书在书库中的信息为:"<<endl;cout<<setw(5)<<"书号"<<setw(20)<<"书名"<<setw(20)<<"作者"<<setw(5)<<"库存"<<endl;cout<<R[book_num.search(num,0)]<<endl;cout<<"\n请输入添加的本数:";cin>>stock;R[book_num.search(num,0)].stock+=stock;ofstream B_output("Book.txt");                    //将书本采编后的新的书库信息入库;if (B_output.is_open()){for(int n=0;n<i;n++)B_output <<R[n]<<endl;B_output.close();}}else{cout<<"\n该书未在书库中,需输入以重新添加"<<endl;R[i].num=num;cout<<"\n请输入书名:";cin>>R[i].name;cout<<"请输入作者:";cin>>R[i].aut;cout<<"请输入库存:";    cin>>R[i].stock;ofstream B_output("Book.txt",ios::app);              //以写入|添加的方式打开文件,文件指针默认在尾端;if (B_output.is_open()){B_output<<R[i]<<endl;B_output.close();}}cout<<"\n图书信息已成功入库"<<endl;system("pause");return 0;}int borrow(){Information R[20];                                        //用于存放从文件中读取出的数据;Record B[20],stu;int i=0,j=0,num;ifstream B_read("Book.txt");         if (! B_read.is_open())    {        cout << "Error opening B_file"; system("pause");return 0;    }while(B_read>>R[i].num)                                 //读取书库文件,并将文件中数据附给相应的成员函数;{B_read>>R[i].name>>R[i].aut>>R[i].stock;i++;                                                //共有i种书;}B_read.close();  ifstream R_read("Record.txt");                         //读取借阅记录      if (! R_read.is_open())    {        cout << "Error opening R_file"; return 0;    }    while(R_read>>B[j].stu_Num)                            //读取书库文件,并将文件中数据附给相应的成员函数;{R_read>>B[j].T>>B[j].book_Num>>B[j].book_Name;j++;                                               //共有j条记录;}R_read.close();   //关闭文件;cout<<"\n请输入想借阅的书本序号:";cin>>num;SeqList<int> book_num;                                //用读取出来的序号数组为线性表提供元素建立索引表;for(int n=0;n<i;n++)book_num.insert(R[n].num);if(book_num.search(num,0)+1)                          //SeqList<T>::search,若未查找到,返回-1;{if(R[book_num.search(num,0)].stock){cout<<"\n书本尚有库存,允许借出,在书库中的信息为"<<endl;cout<<setw(5)<<"书号"<<setw(20)<<"书名"<<setw(20)<<"作者"<<setw(5)<<"库存"<<endl;cout<<R[book_num.search(num,0)]<<endl;cout<<"\n请登记借阅者的书证号:";cin>>stu.stu_Num;int k=0;for(k;!(B[k].stu_Num==stu.stu_Num&&B[k].book_Num==num)&&k<j;k++);    if(!(k<j)){R[book_num.search(num,0)].stock-=1;ofstream B_output("Book.txt");if (B_output.is_open())                         //将书本借出后的新的书库信息入库;{for(int n=0;n<i;n++)B_output <<R[n]<<endl;B_output.close();}stu.book_Num=num;stu.book_Name=R[book_num.search(num,0)].name;int Year,Month,Day;time_t tt = time(NULL);//这句返回的只是一个时间cuo                tm* t= localtime(&tt);if((t->tm_mon+3)>12)                   //规定一本书只能借两个月{Year=(t->tm_year+1901);Month=(t->tm_mon-10);}else{Year=(t->tm_year+1900);Month=(t->tm_mon+3);}Day=t->tm_mday;    char *a=new char[30],*b=new char[4];itoa(Year,a,10);strcat(a,"/");strcat(a,itoa(Month,b,10));strcat(a,"/");strcat(a,itoa(Day,b,10));stu.T=a;delete[] a;delete[] b;ofstream R_output("Record.txt",ios::app);if (R_output.is_open())                         //将借阅的信息入库;{R_output <<stu<<endl;R_output.close();cout<<"\n登记成功,借阅手续办理已经完成"<<endl;}elsecout<<"\nError opening txt"<<endl;}elsecout<<"\nError,同一个人不可借阅多本相同的书"<<endl;}elsecout<<"\n库存不足,无法借阅";}elsecout<<"\n书号不存在,借阅失败";system("pause");return 0;}int give_back(){int stu,book;Information A[20];Record B[20];int i=0,j=0;                                   //全局变量ifstream B_read("Book.txt");                   //读取书库if (! B_read.is_open())    {        cout << "Error opening B_file"; system("pause");return 0;    }    while(B_read>>A[i].num)                                //读取书库文件,并将文件中数据附给相应的成员函数;{B_read>>A[i].name>>A[i].aut>>A[i].stock;i++;                                               //共有i种书;}B_read.close();                                        //关闭文件;ifstream R_read("Record.txt");                         //读取借阅记录      if (! R_read.is_open())    {        cout << "Error opening R_file"; system("pause");return 0;    }    while(R_read>>B[j].stu_Num)                            //读取书库文件,并将文件中数据附给相应的成员函数;{R_read>>B[j].T>>B[j].book_Num>>B[j].book_Name;j++;                                               //共有j条记录;}R_read.close();                                        //关闭文件;cout<<"\n请输入归还者的书证号:";cin>>stu;SeqList<int> stu_num;                                 //用读取出来的序号数组为线性表提供元素建立索引表;for(int n=0;n<i;n++)stu_num.insert(B[n].stu_Num);if(stu_num.search(stu,0)+1)                            //SeqList<T>::search,若未查找到,返回-1;{cout<<"\n请输入归还的书号:";    cin>>book;int k=0;for(k;!(B[k].stu_Num==stu&&B[k].book_Num==book)&&k<j;k++);if(k<j){int b[3],h=0;char *c=new char[10],*p=new char[10];for(int n=0;n<B[k].T.length();n++)c[n]=B[k].T[n];p=strtok(c,"/");while(p!=NULL){b[h]=atoi(p);p=strtok(NULL,"/");h++;}delete[] p;delete[] c;time_t tt = time(NULL);//这句返回的只是一个时间cuo            tm* t= localtime(&tt);int judge=0;if(b[0]<(t->tm_year+1900))judge=1;elseif(b[0]==(t->tm_year+1900)&&b[1]<(t->tm_mon+1))judge=1;else if(b[1]==(t->tm_mon+1)&&b[2]<t->tm_mday)judge=1;else{for(k;k<j-1;k++)B[k]=B[k+1];j--;ofstream R_output("Record.txt");if (R_output.is_open())                     //将书本归还后的新的借阅记录写入;{for(int n=0;n<j;n++)R_output<<B[n]<<endl;R_output.close();}A[stu_num.search(stu,0)].stock+=1;ofstream B_output("Book.txt");if (B_output.is_open())                     //将书本归还后的新的书库信息入库;{for(int n=0;n<i;n++)B_output<<A[n]<<endl;B_output.close();}cout<<"\n书本归还成功"<<endl;judge=0;}while(judge){char over;cout<<"\n归还超期,是否已经缴费(Y/N):";cin>>over;if(over=='Y'||over=='y'){for(k;k<j-1;k++)B[k]=B[k+1];j--;        ofstream R_output("Record.txt");        if (R_output.is_open())              //将书本归还后的新的借阅记录写入;        {for(int n=0;n<j;n++)R_output<<B[n]<<endl;R_output.close();        }A[stu_num.search(stu,0)].stock+=1;ofstream B_output("Book.txt");if (B_output.is_open())                         //将书本归还后的新的书库信息入库;{for(int n=0;n<i;n++)B_output<<A[n]<<endl;B_output.close();}cout<<"\n书本归还成功"<<endl;judge=0;}else{cout<<"\n注销失败,请缴费后再删除记录;"<<endl;break;}}}elsecout<<"\n该归还者未借阅此书"<<endl;}elsecout<<"\n输入有误或不存在此归还者的借书记录"<<endl;system("pause");    return 0;}int record(){    cout<<setw(15)<<"书证号"<<setw(20)<<"归还日期"<<setw(15)<<"书号"<<setw(25)<<"书名"<<endl;cout<<endl;char buffer[256];    ifstream R_read("Record.txt");    if (! R_read.is_open())    {        cout<<"Error opening B_file";return 0;    }    while (!R_read.eof())    {        R_read.getline(buffer,100);        cout<<buffer<< endl;    }system("pause");    return 0;}