您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何使用C++編寫實現圖書管理系統,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內容如下
為圖書管理人員編寫一個圖書管理系統,圖書管理系統的設計主要是實現對圖書的管理和相關操作,包括3個表:
圖書信息表——存儲圖書的基本信息,包括書號、書名、作者、出版社、出版日期、存館數量、定價等。
讀者信息表——存儲讀者的基本信息,包括學號、姓名、學院、專業班級等。
圖書借閱表——存儲讀者借書的相關信息,包括學號、姓名、書號、書名、借閱日期、應還日期、歸還日期等。
用菜單選擇方式完成了以下功能:
1、圖書信息添加功能:包括書號、書名、作者、出版社、出版日期、存館數量、定價等。
2、圖書信息查詢:分別按書名, 按作者名,
按出版單位等進行查詢。
3、圖書信息排序:按書號、書名等按升序進行排序。
4、圖書信息的修改、刪除:按書號或書名進行圖書的修改和刪除。
5、讀者信息添加功能:包括學號、姓名、學院、專業、班級等。
6、讀者信息查詢:分別按學號、姓名、班級等進行查詢。
7、讀者信息排序:按學號、學院等按升序進行排序。
8、讀者信息的修改、刪除:按學號+姓名進行讀者信息的修改和刪除
9、圖書借閱:輸入學號+書號,如果該書圖書信息表中的存館數量大于0,則可以借出,借出相應數量后修改圖書信息表中的存館數量,在圖書借閱表添加該同學的借閱。
10、圖書歸還:輸入學號+書號,修改圖書信息表中的存館數量,在圖書借閱表中記錄該同學的歸還時間。
11、圖書借閱查詢:分別按學號、書名、學院等進行查詢。
#include <iostream> #include <string> #include <cstring> #include <fstream> #include <cstdlib> #include <windows.h> #include <winbase.h> const int INC=20; using namespace std; class Date{ //Date類 :日期類 public: int day; //日 int month; //月 int year; //年 Date(int d=1,int m=1,int y=2012){ //構造函數 默認初始日期2012.1.1 //判斷日期的合法性 if(d>0&&d<32)day=d;else day=1; if(m>0&&m<13)month=m;else m=1; if(y>0)year=y;else year=2012; } void setDay(int d){day=d;} //設置日 void setMonth(int m){month=m;} //設置月 void setYear(int y){year=y;} //年 int getDay(){return day;} //獲取年月日 int getMonth(){return month;} int getYear(){return year;} void disp(){cout<<year<<"/"<<month<<"/"<<day;} //顯示日期 Date operator+(int dtmp); bool isLeapYear(int yy); //判斷是否為閏年 }; bool Date::isLeapYear(int yy){ //判斷是否為閏年 if(yy%400==0) return true; else if(yy%100!=0&&yy%4==0) return true; else return false; } Date Date::operator+(int dtmp){ //重載+運算符 //bool Date::isLeapYear(int yy); int yy=year,mm=month,dd=day,mtmp,m2; //年份yy 月份mm 日dd 借閱天數 dtmp bool flag; while(dtmp>=0) { if(isLeapYear(yy)) { dtmp-=366; flag=true; } else { dtmp-=365; flag=false; } yy++; } if(flag) dtmp+=366; else dtmp+=365; yy--; if(isLeapYear(yy)) { m2=29; } else { m2=28; } mtmp=dtmp+dd; int m_day[]={0,31,m2,31,30,31,30,31,31,30,31,30,31}; //每月天數 for(;mtmp>m_day[mm];) { mtmp-=m_day[mm]; if(mm==12) { mm=0; yy++; } mm++; } dd=mtmp; Date dt(yy,mm,dd); return dt; //返回一個新日期 } class Book{ //書號、書名、作者、出版社、出版日期、出版定價、存館數量 private: string num; //s書號 string title; //標題 string author; //作者 string publisher; //出版社 Date publishdate; //Date類:出版日期 float price; //價格 int howmany; //存館數量 public: Book(string num0="24416-5",string title0="數據結構",string author0="王紅梅,胡明,王濤", string pub0="清華大學出版社",int bd=1,int bm=1,int by=2012,float pr=29.0,int ct=10):publishdate((bd,bm,by)){ num=num0;title=title0;author=author0;publisher=pub0;price=pr;howmany=ct;} void setNum(string num0){num=num0;} //設置書號 void setTitle(string title0){title=title0;} //設置標題 void setAuthor(string author0){author=author0;} //設置作者 void setPublisher(string publisher0){publisher=publisher0;} //設置出版社 void setPublishdate(int bd,int bm,int by){Date d(bd,bm,by);publishdate=d;} //設置出版日期,先構造在復制 void setHowmany(int ct){howmany=ct;} //設置庫存 void setPrice(float pr){price=pr;} //設置價格 string getNum(){return num;} // 獲取 書號 標題 作者 等信息 string getTitle(){return title;} string getAuthor(){return author;} string getPublisher(){return publisher;} Date getPublishdate(){return publishdate;} int getHowmany(){return howmany;} float getPrice(){return price;} void dispABook(){ cout <<num<< '\t'<<title<< '\t'<<author<< '\t'<<publisher<< '\t'; //獲取Book信息 :書號標題 作者 出版社 日期 getPublishdate().disp(); //獲取日期信息 cout<< '\t'<<price<< '\t'<<howmany << '\n' ; } friend class InBook; //友元類 friend class ReaderBorrowBook; }; class Reader{ //學號、姓名、學院、專業班級 讀者類 private: string num; //學號 string name; //姓名 string college; //專業 string classno; //班級 public: Reader(string num0="20150000",string name0="NoName",string college0="信息學院",string clsn="網絡工程15-1"){ num=num0;name=name0;college=college0; classno=clsn;} //構造函數初始化信息 void setNum(string num0){num=num0;} //功能函數 設置信息 void setName(string name0){name=name0;} void setCollege(string college0){college=college0;} void setClassno(string clsn){classno=clsn;} string getNum(){return num;} //功能函數 獲取信息 string getName(){return name;} string getCollege(){return college;} string getClassno(){return classno;} void dispAReader(){ //顯示信息 cout <<num<< "\t"<<name<< "\t"<<college<< "\t"<<classno<< "\n"; } friend class Inreader; friend class ReaderBorrowBook; }; class BorrowBook { //借閱的書類 private: string num; //學號、姓名、書號、書名、借閱日期、應還日期、歸還日期 string name; string booknum; string title; Date borrowdate; Date toreturndate; Date returndate; BorrowBook(string num0="20150000",string name0="NoName",string booknum0="24416-5", string title0="數據結構",int bd=1,int bm=1,int by=2012,int td=1,int tm=5,int ty=2012,int rd=1,int rm=3,int ry=2012) :borrowdate(bd,bm,by),toreturndate(td,tm,ty),returndate(rd,rm,ry) { num=num0;name=name0; booknum=booknum0;title=title0; //初始化 } void setNum(string num0){num=num0;} //設置學號 void setName(string name0){name=name0;}//設置名字 void setBookNum(string num0){booknum=num0;}//設置書號 void setTitle(string title0){title=title0;}//書名 void setBorrowdate(int bd,int bm,int by){Date d(bd,bm,by);borrowdate=d;}//借閱日期 void setToreturndate(int td,int tm,int ty){Date d(td,tm,ty);toreturndate=d;}// void setReturndate(int rd,int rm,int ry){Date d(rd,rm,ry);returndate=d;}//歸還日期 string getNum(){return num;} //獲取學號 string getName(){return name;}//獲取名字 string getBookNum(){return booknum;}//書號 string getTitle(){return title;}//書名 Date getBorrowdate(){return borrowdate;}//得到日期 Date getReturndate(){return returndate;} Date getToreturndate(){return toreturndate;} void dispBorrowBook() //顯示值 { cout <<num<< "\t"<<name<< "\t"<<booknum<< "\t"<<title<< "\t"; borrowdate.disp();cout<<"\t"; toreturndate.disp();cout<<"\t"; returndate.disp();cout<< "\n"; } friend class InBorrowBook; friend class ReaderBorrowBook; }; void string2date(string pdates,int d[]) { int k=0,by,bm,bd; while((pdates[k]<'0'||pdates[k]>'9')&&k<pdates.length())k++; by=0; for(;pdates[k]!='/'&&k<pdates.length();k++) by=by*10+pdates[k]-'0'; k++;bm=0; for(;pdates[k]!='/'&&k<pdates.length();k++) bm=bm*10+pdates[k]-'0'; if(pdates[k]=='/'){ k++;bd=0; for(;k<pdates.length();k++) bd=bd*10+pdates[k]-'0'; } else bd=1; d[0]=by;d[1]=bm;d[2]=bd; } class InBook { public: Book *book; //book[n],很多本書 int bookcount; //記數 void write2Bookfile() { string num,title,author,publisher,pdates; Date pdate; int i,bd,bm,by,ct; //多余變量吧 float price; ofstream outf( "d:\\book.txt", ios::out ) ; //寫入的文件名應該與讀出的文件名相同;調試時可以取不同的文件名,以免覆蓋掉原文件 if ( !outf ) { cerr << "File could not be open." << endl ; } outf<<"圖書信息\n"; for(int i=0;i<bookcount;i++) //寫入圖書信息 { Date pd=book[i].getPublishdate(); outf<<book[i].getNum()<< '\t'<<book[i].getTitle()<< '\t'; outf<<book[i].getAuthor()<< '\t'<<book[i].getPublisher()<< '\t'; outf<<pd.getYear()<<"/"<<pd.getMonth()<<"/"<<pd.getDay(); outf<< '\t'<<book[i].getPrice()<< '\t'<<book[i].getHowmany()<< '\n' ; } outf.close() ; //關閉文件 } int inbookFile( char * fileName, int delLine) //獲取圖書信息 {cout<<1<<endl; string num,title,author,publisher,pdates; Date pdate; int i,bd,bm,by,ct; float price; ifstream inf( fileName, ios::in ) ; if ( !inf ) { cerr << "File could not be open." << endl ; return 1; } char s[80]; for ( i=1; i <= delLine; i++ ) inf.getline( s, 80 ) ; i=0; while( !inf.eof() )// { inf.getline( s, 80 ) ; cout << s << endl ; } { inf.getline( s, 80,'\t' ) ;//num=s; inf.getline( s, 80,'\t' ) ;title=s; inf.getline( s, 80,'\t' ) ;//author=s; inf.getline( s, 80,'\t' ) ;//publisher=s; inf.getline( s, 80,'\t' ) ;//pdates=s; inf>>price>>ct; if(title.length()>0) i++; } bookcount=i; cout<<bookcount<<endl; inf.clear(); inf.seekg(0,ios::beg); for ( i=1; i <= delLine; i++ ) inf.getline( s, 80 ) ; book=new Book[bookcount+INC]; for ( i=0; i <bookcount; i++ ) { inf.getline( s, 80,'\t' ) ;num=s; inf.getline( s, 80,'\t' ) ;title=s; inf.getline( s, 80,'\t' ) ;author=s; inf.getline( s, 80,'\t' ) ;publisher=s; inf.getline( s, 80,'\t' ) ;pdates=s; inf>>price>>ct;//inf,getchar(); if(title.length()>0){ int d[3]; if(num[0]==10)num=num.substr(1); string2date(pdates,d); by=d[0];bm=d[1];bd=d[2]; book[i]=Book(); book[i].setNum(num); book[i].setTitle(title); book[i].setAuthor(author); book[i].setPublisher(publisher); book[i].setPrice(price); book[i].setHowmany(ct); book[i].setPublishdate(bd,bm,by); } } inf.close() ; return bookcount; } friend class ReaderBorrowBook; int addbook(string num,string title,string author,string publisher,int bd,int bm,int by,float price,int ct)//添加圖書信息 { int i=bookcount; book[i]=Book(); book[i].setNum(num); book[i].setTitle(title); book[i].setAuthor(author); book[i].setPublisher(publisher); book[i].setPrice(price); book[i].setHowmany(ct); book[i].setPublishdate(bd,bm,by); ++bookcount; return bookcount; } void searchbookbytitle(string title) //按照書名查找 { bool found=false; for(int i=0;i<bookcount;i++) if((book[i].getTitle()).find(title)!= string::npos) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); found=true; //return i; } if(!found)cout<<"book title:"<<title<<" not found."<<endl; //return -1; } int searchbookbytitlep(string title){ //精確查找 for(int i=0;i<bookcount;i++) if(book[i].getTitle()==title) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); return i; } cout<<"book title:"<<title<<" not found."<<endl; return -1; } void searchbookbyauthor(string author) //按照作者查找 { bool found=false; for(int i=0;i<bookcount;i++) if((book[i].getAuthor()).find(author)!= string::npos) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); found=true; //return i; } if(!found)cout<<"book author:"<<author<<" not found."<<endl; //return -1; } int searchbookbyauthorp(string author) //精確查找 { for(int i=0;i<bookcount;i++) if(book[i].getAuthor()==author) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); return i; } cout<<"book author:"<<author<<" not found."<<endl; return -1; } void searchbookbypub(string pub) //按照出版社查找 { bool found=false; for(int i=0;i<bookcount;i++) if((book[i].getPublisher()).find(pub)!= string::npos) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); found=true; //return i; } if(!found)cout<<"book publisher:"<<pub<<" not found."<<endl; //return -1; } int searchbookbypubp(string pub) { for(int i=0;i<bookcount;i++) if(book[i].getPublisher()==pub) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); return i; } return -1; } void searchbookbynum(string num) {//模糊查找包含num的串 bool found=false; for(int i=0;i<bookcount;i++) if((book[i].getNum()).find(num)!= string::npos) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); found=true; // return i; } if(!found)cout<<"book num:"<<num<<" not found."<<endl; // return -1; } int searchbookbynump(string num) {//精確查找等于num的串 for(int i=0;i<bookcount;i++) if((book[i].getNum())==num) { //cout<<book[i].getTitle()<< book[i].getTitle().find(title)<<endl; cout<<i<<"\t"; book[i].dispABook(); return i; } return -1; } void sortbookbynum() //按照書號排序 { for(int i=0;i<bookcount;i++){ int k=i; for(int j=i+1;j<bookcount;j++) if(book[j].getNum()<book[k].getNum()) k=j; //cout<<k<<" k&i "<<i<<" :"<<book[i].getNum()<<"---"<<book[k].getNum()<<endl; if(k!=i){ Book t=book[i];book[i]=book[k];book[k]=t; } } } void sortbookbytitle() //按照標題排序 { for(int i=0;i<bookcount;i++) { int k=i; for(int j=i+1;j<bookcount;j++) if(book[j].getTitle()<book[k].getTitle()) k=j; if(k!=i){ Book t=book[i];book[i]=book[k];book[k]=t; } } } void changebookbynum(string oldnum,string newnum) //按書號改信息 { int k=searchbookbynump(oldnum); if(k>=0) { book[k].setNum(newnum); cout<<"changebookbynum successed:"<<endl; book[k].dispABook(); } else cout<<"changebook failed. No book of num="<<oldnum<<endl; } void deletebookbynum(string num) //按書號刪除書目 { int k=searchbookbynump(num); if(k>=0) { cout<<"Book to be deleted :"<<endl; book[k].dispABook(); book[k]=book[bookcount-1]; cout<<"deletebookbynum successed:"<<endl; bookcount--; //return bookcount; } else cout<<"deletebook failed. No book of num="<<num<<endl; } void changebookbytitle(string oldtitle,string newtitle) { //按標題修改書目 int k=searchbookbytitlep(oldtitle); if(k>=0) { book[k].setTitle(newtitle); cout<<"changebookbytitle successed:"<<endl; book[k].dispABook(); } else cout<<"changebook failed. No book of title="<<oldtitle<<endl; } void deletebookbytitle(string title){ //按書名刪除書 int k=searchbookbytitlep(title); if(k>=0) { cout<<"Book to be deleted :"<<endl; book[k].dispABook(); book[k]=book[bookcount-1]; cout<<"deletebookbytitle successed:"<<endl; bookcount--; //return bookcount; } else cout<<"deletebook failed. No book of title="<<title<<endl; } }; class InReader{ public: Reader *reader;int readercount; //reader[n],讀者總數 void write2Readerfile() //寫入讀者信息 { string a1,b1,c1,d1; ofstream fout( "d:\\reader.txt", ios::out ); if ( !fout ) { cerr << "File could not be open." << endl ; } fout<<"讀者信息\n"; for(int i=0;i<readercount;i++) fout<<reader[i].getNum()<<'\t'<<reader[i].getName()<<'\t'<<reader[i].getCollege()<<'\t'<<reader[i].getClassno()<<endl; fout.close(); } int inreaderFile( char * fileName, int delLine) //獲取讀者信息 {cout<<1<<endl; string num,name,college,classno; int i; ifstream inf( fileName, ios::in ) ; if ( !inf ) { cerr << "File could not be open." << endl ; return 1; } char s[80]; for ( i=1; i <= delLine; i++ ) inf.getline( s, 80 ) ; i=0; while( !inf.eof() )// { inf.getline( s, 80 ) ; cout << s << endl ; } { inf.getline( s, 80,'\t' ) ;//num=s; inf.getline( s, 80,'\t' ) ;name=s; inf.getline( s, 80,'\t' ) ;//college=s; inf.getline( s, 80 ) ;//classno=s; if(name.length()>0) i++; } readercount=i; cout<<readercount<<endl; inf.clear(); inf.seekg(0,ios::beg); for ( i=1; i <= delLine; i++ ) inf.getline( s, 80 ) ; reader=new Reader[readercount+INC]; for ( i=0; i <readercount; i++ ) { inf.getline( s, 80,'\t' ) ;num=s; inf.getline( s, 80,'\t' ) ;name=s; inf.getline( s, 80,'\t' ) ;college=s; inf.getline( s, 80 ) ;classno=s; if(name.length()>0){ if(num[0]==10)num=num.substr(1); reader[i]=Reader(); reader[i].setNum(num); reader[i].setName(name); reader[i].setCollege(college); reader[i].setClassno(classno); } } inf.close() ; return readercount; } int addreader(string num,string name,string college,string classno) //添加讀者 { int i=readercount; reader[i]=Reader(); reader[i].setNum(num); reader[i].setName(name); reader[i].setCollege(college); reader[i].setClassno(classno); ++readercount; return readercount; } void searchreaderbynum(string num){ //按學號查詢讀者 bool found=false; for(int i=0;i<readercount;i++) if((reader[i].getNum()).find(num)!= string::npos) { cout<<i<<"\t"; reader[i].dispAReader(); found=true; //return i; } if(!found)cout<<"reader of num: "<<num<<" not found"<<endl; //return -1; } int searchreaderbynump(string num){ //按學號精確查詢讀者 for(int i=0;i<readercount;i++) if(reader[i].getNum()==num) { cout<<i<<"\t"; reader[i].dispAReader(); return i; } return -1; } void searchreaderbyname(string nnname) //按名字查詢讀者 { for(int i=0;i<readercount;i++) if(reader[i].getName()==nnname) { cout<<i<<"\t"; reader[i].dispAReader(); // return i; } // return -1; } int searchreaderbynamep(string nnname){//模糊查找 for(int i=0;i<readercount;i++) if(reader[i].getName()==nnname) { cout<<i<<"\t"; reader[i].dispAReader(); return i; } return -1; } void searchreaderbyzybj(string zzy) //按學院查找 { for(int i=0;i<readercount;i++) if(reader[i].getCollege()==zzy) { cout<<i<<"\t"; reader[i].dispAReader(); // return i; } // return -1; } int searchreaderbyzybjp(string zzy){ //精確查找 for(int i=0;i<readercount;i++) if(reader[i].getCollege()==zzy) { cout<<i<<"\t"; reader[i].dispAReader(); return i; } return -1; } int searchreaderbyzybjjp(string bbj){ //按專業班級查找 for(int i=0;i<readercount;i++) if(reader[i].getClassno()==bbj) { cout<<i<<"\t"; reader[i].dispAReader(); return i; } return -1; } void searchreaderbyzybjj(string bbj) //模糊查找 { for(int i=0;i<readercount;i++) if(reader[i].getClassno()==bbj) { cout<<i<<"\t"; reader[i].dispAReader(); // return i; } // return -1; } void deletereaderbyxuehao(string xxhh){ //按學號刪除讀者 int k=searchreaderbynump(xxhh); if(k>=0) { cout<<"reader to be deleted :"<<endl; reader[k].dispAReader(); reader[k]=reader[readercount-1]; cout<<"deletereaderbyxuehao successed:"<<endl; readercount--; //return bookcount; } else cout<<"deletereader failed. No reader of xuehao="<<xxhh<<endl; } void deletereaderbyxingming(string xxh2h){ //按姓名刪除讀者 int k=searchreaderbynamep(xxh2h); if(k>=0) { cout<<"reader to be deleted :"<<endl; reader[k].dispAReader(); reader[k]=reader[readercount-1]; cout<<"deletereaderbyxingming successed:"<<endl; readercount--; //return bookcount; } else cout<<"deletereader failed. No reader of xingming="<<xxh2h<<endl; } void sortreaderbyxuahao() //按學號排序 { for(int i=0;i<readercount;i++) { int k=i; for(int j=i+1;j<readercount;j++) if(reader[j].getNum()<reader[k].getNum()) k=j; if(k!=i){ Reader t=reader[i];reader[i]=reader[k];reader[k]=t; } } } void sortreaderbyxueyuan() //按學院排序 { for(int i=0;i<readercount;i++) { int k=i; for(int j=i+1;j<readercount;j++) if(reader[j].getCollege()<reader[k].getCollege()) k=j; if(k!=i){ Reader t=reader[i];reader[i]=reader[k];reader[k]=t; } } } void changereaderbyxuehao(string old,string news) //修改學號 { int k=searchreaderbynump(old); if(k>=0) { reader[k].setNum(news); cout<<"changereaderbyxuehao successed:"<<endl; reader[k].dispAReader(); } else cout<<"changereader failed. No reader of num="<<old<<endl; } void changereaderbyxingming(string old,string news) //修改姓名 { int k=searchreaderbynamep(old); if(k>=0) { reader[k].setName(news); cout<<"changereaderbyxuehao successed:"<<endl; reader[k].dispAReader(); } else cout<<"changereader failed. No reader of num="<<old<<endl; } void changereaderbyxueyuan(string old,string news) //修改學院 { int k=searchreaderbyzybjp(old); if(k>=0) { reader[k].setCollege(news); cout<<"changereaderbyxuehao successed:"<<endl; reader[k].dispAReader(); } else cout<<"changereader failed. No reader of num="<<old<<endl; } void changereaderbyzybj(string old,string news) //修改專業班級 { int k=searchreaderbyzybjjp(old); if(k>=0) { reader[k].setClassno(news); cout<<"changereaderbyxuehao successed:"<<endl; reader[k].dispAReader(); } else cout<<"changereader failed. No reader of num="<<old<<endl; } friend class ReaderBorrowBook; }; class InBorrowBook{ public: friend class ReaderBorrowBook; BorrowBook *borrowbook;int borrowbookcount; //borrowbook[n],借書總數 void searchborrowbookbyxuehao(string b) { for(int i=0;i<borrowbookcount;i++) if(borrowbook[i].getNum()==b) { cout<<i<<"\t"; borrowbook[i].dispBorrowBook(); // return i; } // return -1; } void searchborrowbookbysm(string b) //按書名查找借閱書 { for(int i=0;i<borrowbookcount;i++) if(borrowbook[i].getTitle()==b) { cout<<i<<"\t"; borrowbook[i].dispBorrowBook(); // return i; } // return -1; } void write2BorrowBookfile() //寫入借書文件 { ofstream ffout( "d:\\borrowbook.txt", ios::out ); if ( !ffout ) { cerr << "File could not be open." << endl ; } ffout<<"借閱信息\n"; for(int i=0;i<borrowbookcount;i++) { Date bbb=borrowbook[i].getBorrowdate(); Date ddd=borrowbook[i].getToreturndate(); Date ccc=borrowbook[i].getReturndate(); ffout<<borrowbook[i].getNum()<<'\t'<<borrowbook[i].getName()<<'\t'<<borrowbook[i].getBookNum()<<'\t'<<borrowbook[i].getTitle()<<'\t'<<bbb.getYear()<<"/"<<bbb.getMonth()<<"/" <<bbb.getDay()<<'\t'<<ddd.getYear()<<"/"<<ddd.getMonth()<<"/" <<ddd.getDay()<<'\t'<<ccc.getYear()<<"/"<<ccc.getMonth()<<"/" <<ccc.getDay()<<endl; } } int inborrowbookFile( char * fileName, int delLine) //獲取借閱圖書信息 {cout<<1<<endl; string num,name,booknum,title; string borrowdates; string toreturndates; string returndates; int i; ifstream inf( fileName, ios::in ) ; if ( !inf ) { cerr << "File could not be open." << endl ; return 1; } char s[80]; for ( i=1; i <= delLine; i++ ) inf.getline( s, 80 ) ; i=0; while( !inf.eof() ) { inf.getline( s, 80,'\t') ; inf.getline( s, 80,'\t' ) ; inf.getline( s, 80,'\t' ) ; inf.getline( s, 80 ) ; if(strlen(s)>1) i++; } borrowbookcount=i; cout<<borrowbookcount<<endl; inf.clear(); inf.seekg(0,ios::beg); for ( i=1; i <= delLine; i++ ) inf.getline( s, 80 ) ; borrowbook=new BorrowBook[borrowbookcount+INC]; for ( i=0; i <borrowbookcount; i++ ) { inf.getline( s, 80,'\t' ) ;num=s; inf.getline( s, 80,'\t' ) ;name=s; inf.getline( s, 80,'\t' ) ;booknum=s; inf.getline( s, 80,'\t' ) ;title=s; inf.getline( s, 80,'\t' ) ;borrowdates=s; inf.getline( s, 80,'\t' ) ;toreturndates=s; inf.getline( s, 80,'\n' ) ;returndates=s; if(name.length()>0){ if(num[0]==10)num=num.substr(1); borrowbook[i]=BorrowBook(); int d[3]; string2date(borrowdates,d); int by=d[0],bm=d[1],bd=d[2]; borrowbook[i].setBorrowdate(bd,bm,by); string2date(toreturndates,d); by=d[0];bm=d[1];bd=d[2]; borrowbook[i].setToreturndate(bd,bm,by); string2date(returndates,d); by=d[0];bm=d[1];bd=d[2]; borrowbook[i].setReturndate(bd,bm,by); borrowbook[i].setNum(num); borrowbook[i].setName(name); borrowbook[i].setBookNum(booknum); borrowbook[i].setTitle(title); } } inf.close() ; return borrowbookcount; } friend class ReaderBorrowBook; int searchbyreaderbook(string readernum,string booknum) //按學號查詢 { for(int i=0;i<borrowbookcount;i++) if((borrowbook[i].getNum())==readernum&&(borrowbook[i].getBookNum())==booknum) { cout<<i<<"\t"; borrowbook[i].dispBorrowBook(); return i; } return -1; } }; class ReaderBorrowBook{ InBook bk; //聲明三個類 InReader rd; InBorrowBook bb; public: void write2file(){ bk.write2Bookfile(); //寫入文件保存信息 rd.write2Readerfile(); bb.write2BorrowBookfile(); } void init(){ //打開三個文件 bk.inbookFile( "d:\\book.txt", 1); rd.inreaderFile( "d:\\reader.txt", 1); bb.inborrowbookFile( "d:\\borrowbook.txt", 1); } void dispBook(){ //顯示書信息 for ( int i=0; i <bk.bookcount; i++ ) {cout <<i<<":"; bk.book[i].dispABook();} } void dispReader(){ //顯示讀者信息 for ( int i=0; i <rd.readercount; i++ ) {cout <<i<<":";rd.reader[i].dispAReader();} } void dispBorrowbook(){ //借書信息查詢 for ( int i=0; i <bb.borrowbookcount; i++ ) {cout <<i<<":";bb.borrowbook[i].dispBorrowBook();} } void dispCount(){ //總數顯示 cout<<bk.bookcount<<endl;cout<<rd.readercount<<endl;cout<<bb.borrowbookcount<<endl; } void addbook() //添加書 { string num,title,author,publisher; char num0[80],tit[80],auth[80],pub[80]; int pd,pm,py,howmany; float price; Date publishdate; cout<<"書號、書名、作者、出版社、出版日期(yyyy mm dd)、定價、存館數量"<<endl; string num1="24416-5",title0="數據結構",author0="王紅梅,胡明,王濤",pub0="清華大學出版社"; int pd0=1,pm0=1,py0=2012; float price0=29.0; int howmany0=10; //bookcount= cin>>num1>>title0>>author0>>pub0>>pd0>>pm0>>py0>>price0>>howmany0; bk.addbook(num1,title0,author0,pub0,pd0,pm0,py0,price0,howmany0); bk.write2Bookfile(); int i=bk.bookcount-1; cout <<i<<":"; bk.book[i].dispABook(); } void addreader(){ //添加讀者 string num,name,college,classno; char num0[80],nam[80],coll[80],clas[80]; cout<<"學號\t姓名\t學院\t專業班級:"<<endl; string num1="20151000",name0="王濤",college0="computer",class0="network class 1"; cin>>num1>>name0>>college0>>class0; rd.addreader(num1,name0,college0,class0); rd.write2Readerfile(); int i=rd.readercount-1; cout <<i<<":"; rd.reader[i].dispAReader(); } void searchBook(){ //查書 cout<<"2.圖書信息查詢:分別按書名, 按作者名, 按出版社進行查詢。"<<endl; char tit[80]; string title="數據結構",author="胡明",pub="機械工業"; int i,j,k,select; while(1){ cout<<"圖書信息查詢:"<<endl; cout<<"1.按書名查詢"<<endl; cout<<"2.按作者名查詢"<<endl; cout<<"3.按出版社查詢"<<endl; cout<<"0. return"<<endl; cin>>select; cin.get(); switch(select){ case 1: cout<<"書名:"; getline(cin,title,'\n'); bk.searchbookbytitle(title); break; case 2: cout<<"作者名:"; getline(cin,author,'\n'); bk.searchbookbyauthor(author); break; case 3: cout<<"出版社:"; getline(cin,pub,'\n'); bk.searchbookbypub(pub);break; case 0:return; } } } void sortbook(){ //排序 int select; cout<<"3.圖書信息排序:按書號、書名等按升序進行排序。"<<endl; cout<<"圖書信息排序:"<<endl; cout<<"1.按書號排序"<<endl; cout<<"2.按書名排序"<<endl; cout<<"0. return"<<endl; cin>>select; switch(select){ case 1: cout<<"書號:"; bk.sortbookbynum(); dispBook(); break; case 2: cout<<"書名:"; bk.sortbookbytitle(); dispBook(); break; case 0:return; } } void editbook(){ //編輯書 string oldtitle="VisualBasic 程序設計教程",newtitle="VisualBasic 程序設計教程-C",oldnum="40667",newnum="40667-C"; int select; cout<<"4.圖書信息的修改、刪除:按書號或書名進行圖書的修改和刪除。"<<endl; while(1){ cout<<"圖書信息的修改、刪除:"<<endl; cout<<"1.按書號修改"<<endl; cout<<"2.按書號刪除"<<endl; cout<<"3.按書名修改"<<endl; cout<<"4.按書名刪除"<<endl; cout<<"0. return"<<endl; cin>>select; cin.get(); switch(select){ case 1: cout<<"old書號:"; getline(cin,oldnum,'\n'); cout<<"new書號:"; getline(cin,newnum,'\n'); cout<<"changebookbynum: "<<oldnum<<" to "<<newnum<<endl; bk.changebookbynum(oldnum,newnum); //dispBook(); break; case 2: cout<<"delete書號:"; getline(cin,oldnum,'\n'); cout<<"delete書號:"<<oldnum<<endl; bk.deletebookbynum(oldnum); //dispBook(); break; case 3: cout<<"old書名:"; getline(cin,oldtitle,'\n'); cout<<"new書名:"; getline(cin,newtitle,'\n'); cout<<"changebookbytitle: "<<oldtitle<<" to "<<newtitle<<endl; bk.changebookbytitle(oldtitle,newtitle); //dispBook(); break; case 4: cout<<"delete書名:"; getline(cin,oldtitle,'\n'); cout<<"deletebookbytitle: "<<oldtitle<<endl; bk.deletebookbytitle(oldtitle); //dispBook(); break; case 0: bk.write2Bookfile(); cout<<"修改成功"<<endl; return; } } } void readerborrowabook(){ //借閱 cout<<"9.圖書借閱:輸入學號+書號,如果該書圖書信息表中的存館數量大于0,則可以借出,"<<endl; string rdnum="20061709",booknum="33071",name,title; int select,rdpos,bookpos,y,m,d; while(1){ cout<<"圖書借閱:"<<endl; cout<<"1.借書"<<endl; cout<<"0.return"<<endl; cin>>select; cin.get(); if(select==0)return; cout<<"borrow學號:"; getline(cin,rdnum,'\n'); cout<<"borrow書號:"; getline(cin,booknum,'\n'); bookpos=bk.searchbookbynump(booknum); rdpos=rd.searchreaderbynump(rdnum); if(bookpos>0&&rdpos>0){ int hm=bk.book[bookpos].getHowmany(); if(hm>0){ name=rd.reader[rdpos].getName(); title=bk.book[bookpos].getTitle(); bk.book[bookpos].setHowmany(hm-1); //修改圖書信息表中的存館數量 bb.borrowbook[bb.borrowbookcount].setNum(rdnum); bb.borrowbook[bb.borrowbookcount].setBookNum(booknum); bb.borrowbook[bb.borrowbookcount].setName(name); bb.borrowbook[bb.borrowbookcount].setTitle(title); SYSTEMTIME ct; GetLocalTime(&ct);//取系統時間,如果用GetSystemTime(&ct);那么獲取的是格林尼治標準時間 y=ct.wYear; m=ct.wMonth; d=ct.wDay; Date toret=Date(d,m,y)+60; bb.borrowbook[bb.borrowbookcount].setBorrowdate(d,m,y); bb.borrowbook[bb.borrowbookcount].setToreturndate(toret.getDay(),toret.getMonth(),toret.getYear()); bb.borrowbook[bb.borrowbookcount].setReturndate(1,1,1); bb.borrowbookcount++; } else cout<<booknum<<" 存館數量=0"<<". can't be borrowed. "<<endl; } else{ if(bookpos<0)cout<<"No book "<<booknum<<". can't borrow!"<<endl; if(rdpos<0)cout<<"No reader "<<rdnum<<". can't borrow!"<<endl; } } } void readerreturnabook(){ //歸還 cout<<"10.圖書歸還:輸入學號+書名,修改圖書信息表中的存館數量,圖書借閱表中記錄該同學的歸還日期。"<<endl; char tit[80]; string rdnum="20061709",booknum="33071",name,title; int selectttt,rdpos,bookpos,y,m,d; while(1){ cout<<"圖書歸還:"<<endl; cout<<"1.還書"<<endl; cout<<"0.return"<<endl; cin>>selectttt; cin.get(); if(selectttt==0) {return;} cout<<"return學號:"<<endl; getline(cin,rdnum,'\n'); cout<<"return書號:"; getline(cin,booknum,'\n'); bookpos=bk.searchbookbynump(booknum); rdpos=rd.searchreaderbynump(rdnum); int k; k=bb.searchbyreaderbook(rdnum,booknum); if(k>=0){ if(bookpos>0&&rdpos>0){ bk.book[bookpos].setHowmany(bk.book[bookpos].getHowmany()+1); SYSTEMTIME ct; GetLocalTime(&ct);//取系統時間 y=ct.wYear; m=ct.wMonth; d=ct.wDay; bb.borrowbook[k].setReturndate(d,m,y); } else{ if(bookpos<0)cout<<"No book "<<booknum<<". can't return!"<<endl; if(rdpos<0)cout<<"No reader "<<rdnum<<". can't return!"<<endl; }} else cout<<"No borrowbook "<<endl; } } void searchreader() { string xuehao ,xingming,zzy,bbj; cout<<"6.讀者信息查詢:分別按學號、姓名、班級等進行查詢。"<<endl; int selectt; while(1){ cout<<"讀者信息查詢:"<<endl; cout<<"1.按學號查詢"<<endl; cout<<"2.按姓名查詢"<<endl; cout<<"3. 按學院查詢"<<endl; cout<<"4. 按專業班級查詢"<<endl; cout<<"0. return"<<endl; cin>>selectt; cin.get(); switch(selectt) { case 1: cout<<"學號:"<<endl; getline(cin,xuehao,'\n'); cout<<1<<endl; rd.searchreaderbynum(xuehao); break; case 2:cout<<"姓名:"<<endl; getline(cin,xingming,'\n'); rd.searchreaderbyname(xingming); break; case 3:cout<<"學院: "<<endl; getline(cin,zzy,'\n'); rd.searchreaderbyzybj(zzy); break; case 4:cout<<"專業班級: "<<endl; getline(cin,bbj,'\n'); rd.searchreaderbyzybjj(bbj); break; case 0: return; } } } void sortreader() { int selectt1; cout<<"7:讀者信息排序:按學號、學院等按升序進行排序。"<<endl; cout<<"讀者信息排序:"<<endl; cout<<"1.按學號排序"<<endl; cout<<"2.按學院排序"<<endl; cout<<"0. return"<<endl; cin>>selectt1; switch(selectt1){ case 1: cout<<"學號:"; rd.sortreaderbyxuahao(); dispReader(); break; case 2: cout<<"學院:"; rd.sortreaderbyxueyuan(); dispReader(); break; case 0:return; } } void editreader() { string xh,xm,xy,bj,newxh,newxm,newxy,newbj; int select2; cout<<"8.學生信息的修改、刪除:。"<<endl; while(1){ cout<<"學生信息的修改、刪除:"<<endl; cout<<"1.按學號修改"<<endl; cout<<"2.按學號刪除"<<endl; cout<<"3.按姓名修改"<<endl; cout<<"4.按姓名刪除"<<endl; cout<<"0. return"<<endl; cin>>select2; cin.get(); switch(select2) { case 1:cout<<"要修改學生的學號為:"<<endl; cin>>xh; cout<<"請輸入新學號"<<endl; cin>>newxh; rd.changereaderbyxuehao(xh,newxh); break; case 2:cout<<"要刪除的學生學號為:"<<endl; cin>>xh; rd.deletereaderbyxuehao(xh); break; case 3:cout<<"修改的學生姓名為:"<<endl; cin>>xm; cout<<"請輸入修改后的姓名:"<<endl; cin>>newxm; rd.changereaderbyxingming(xm,newxm); break; case 4:cout<<"要刪除的學生姓名為:"<<endl; cin>>xm; rd.deletereaderbyxingming(xm); break; case 0: rd.write2Readerfile(); cout<<"修改成功"; return; } } } void searchreaderborrowbook() { int bz; string xxh,ssm; while(1) {cout<<"11.圖書借閱查詢:分別按學號、書名、學院等進行查詢。"<<endl; cout<<"1.按學號查詢"<<endl; cout<<"2.按書名查詢"<<endl; cout<<"0.return"<<endl; cin>>bz; switch(bz) { case 1:cout<<"請輸入學號"<<endl; cin>>xxh; bb.searchborrowbookbyxuehao(xxh);break; case 2:cout<<"請輸入書名"<<endl; cin>>ssm;bb.searchborrowbookbysm(ssm);break; case 0: return; } } } }; int main(){ ReaderBorrowBook rbb; rbb.init(); rbb.dispBook(); rbb.dispReader(); rbb.dispBorrowbook(); rbb.dispCount(); int select; while(1){ cout<<"菜單選擇功能:"<<endl; cout<<"1.圖書信息添加功能:包括書號、書名、作者、出版社名稱、存館數量、定價等"<<endl; cout<<"2.圖書信息查詢:分別按書名, 按作者名, 按出版單位等進行查詢。"<<endl; cout<<"3.圖書信息排序:按書號、書名等按升序進行排序。"<<endl; cout<<"4.圖書信息的修改、刪除:按書號或書名進行圖書的修改和刪除。"<<endl; cout<<"5.讀者信息添加功能:包括學號、姓名、學院、專業班級等。"<<endl; cout<<"6.讀者信息查詢:分別按學號、姓名、班級等進行查詢。"<<endl; cout<<"7.讀者信息排序:按學號、學院等按升序進行排序。"<<endl; cout<<"8.讀者信息的修改、刪除:按學號+姓名進行讀者信息的修改和刪除。"<<endl; cout<<"9.圖書借閱:輸入學號+書號,如果該書圖書信息表中的存館數量大于0,則可以借出,"<<endl; cout<<"借出相應數量后修改圖書信息表中的存館數量,在圖書借閱表添加該同學的借閱。"<<endl; cout<<"10.圖書歸還:輸入學號+書名,修改圖書信息表中的存館數量,在圖書借閱表中記錄該同學的歸還時間。"<<endl; cout<<"11.圖書借閱查詢:分別按學號、書名、學院等進行查詢。"<<endl; cout<<"0. exit"<<endl; cin>>select; switch(select){ case 1:rbb.addbook(); rbb.dispCount();break; case 2:rbb.searchBook();break; case 3:rbb.sortbook();break; case 4:rbb.editbook();break; case 5:rbb.addreader(); rbb.dispCount();break; case 6:rbb.searchreader();break; case 7:rbb.sortreader();break; case 8:rbb.editreader();break; case 9:rbb.readerborrowabook();break; case 10:rbb.readerreturnabook();break; case 11:rbb.searchreaderborrowbook();break; case 0:rbb.write2file();exit(0); } } return 0; }
部分截圖
關于“如何使用C++編寫實現圖書管理系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。