Today to introduce another common management system-book management system, I hope we can hands-on practice, the following to share with you the fruits of my labor.
Book information includes: Login number, title, author name, category number, publishing unit, publication time, Price and so on. Try to design a book information management system, so that it can provide the following functions:
(1) Book information input function (book Information file preservation)
(2) Library information browsing function
(3) Query and sorting function: (At least one query method)
. Search by Title
. Query by author name
(4) The deletion and modification of book information
Share the code as follows
#include <iostream.h> #include <conio.h>//console data input and output function #include <fstream.h>//file stream #include < iomanip.h>//Control Data output format #include <string.h> const int maxr=100;//MAX number of readers const int maxb=100; Max number of Books const int maxbor=5; Each reader borrows most books class book {//books category, realizes to the book description, the book number, the book title, borrows, also enters and so on function private:int tag;//delete Mark 1: deleted 0: not deleted int no;//book Number char name[ 20]; Title Char author[20];//author Char fenlei[20];//classification number char cbs[20]; Publishing house int cbtime;//Publishing Time double bookprice;//book price int onshelf;
Whether to rack 1: Again 2: Lent Public:book () {;}
Char *getname () {//Get title return name;
Char *getauthorname () {//Get author name return author;}
Char *getfenlei () {//Get category number return Fenlei} char *getcbsname () {//Get Publisher name return CBS;
int Getcbtime () {//Get publication Time return cbtime} double Getbookprice () {//Get book price return bookprice; int Getno () {//Get book Number
return no;
int Gettag () {//Get delete tag return tag;
} void SetName (char na[]) {//Set title strcpy (Name,na); } void Setauthorname (char aa[]) {//Set author name strcpy(AUTHOR,AA);
} void Setfenlei (char fe[]) {//Set category number strcpy (FENLEI,FE);} void Setcbs (char ca[]) {//Set publishing house strcpy (CBS,CA);}
void Setcbtime (int time) {//set cbtime=time;}
void Setbookprice (double price) {//Set book prices Bookprice=price} void setonshelf (int oa) {Onshelf=oa;}
void Delbook () {//delete book char i; cout<< "OK delete?" y/n? "
<<endl;
cin>>i; if (i== ' y ') | |
i== ' Y ') tag=1;
} void Addbook (int n,char *na,char *aa,char *fe,char *ca,int-time,double OA) {//add book Price,int;
No=n;
strcpy (Name,na);
strcpy (AUTHOR,AA);
strcpy (CBS,CA);
strcpy (FENLEI,FE);
Cbtime=time;
Bookprice=price;
Onshelf=oa;
int Borrowbook () {//Library operation if (onshelf>0) {onshelf--;
return 1;
return 0;
} void Retbook () {//book return operation onshelf++; } void Disp () {//Output book COUT<<SETW (3) <<NO<<SETW (a) <<NAME<<SETW <<author<& LT;SETW (a) <<FENLEI<<SETW <<cbs<<setw (a) <<CBTIME<<SETW (10) <<BOOKPRICE<<SETW <<onshelf<<endl;
}
}; Class Bdatabase {//library category, to achieve the maintenance of books, find, delete and other private:int top;//books Record pointer book BOOK[MAXB];//Library Records Public:bdatabase () {//Construction letter
Number, read Book.txt to book[] in book B;
Top=-1;
FStream file ("Book.txt", ios::in);
while (1) {File.read ((char *) &b,sizeof (b));
if (!file) break;
top++;
Book[top]=b;
} file.close ();
void Clear () {//delete all char i; cout<< "OK to delete all?" y/n? "
<<endl;
cin>>i; if (i== ' y ') | |
i== ' Y ') top=-1;
int addbook (int n,char *na,char *aa,char *fe,char *ca,int time, double Price,int oa) {//Add book *p=query1 (n);
if (null==p) {top++;
Book[top].addbook (N,NA,AA,FE,CA,TIME,PRICE,OA);
return 1;
return 0; Book *query1 (int bookid) {//Lookup books by number for (int i=0;i<=top;i++) if (Book[i].getno () ==bookid &&book[i].gett
AG () ==0) {return &book[i];
return NULL;
Book *query2 (char a[]) {//Find books by title *e; int r=0; for (int i=0;i<=top;i++) if (strcmp (Book[i].getname (), a) ==0 &&book[i].gettag () ==0) {if (r==0) cout< ; <SETW (3) << "numbered" <<SETW (a) << "title" <<SETW (a) << "author" <<setw (a) << "classification number" <<SETW << "Publishing house" <<SETW (a) << "publication Time" <<SETW (a) << "book Price" <<SETW (a) <
< "stock" <<endl;
e=&book[i];
E->disp ();
r++; if (r==0) cout<< "Can't find the book!"
<<endl;
return NULL;
Book *query3 (char a[]) {//Find books by author *e;
int r=0;
for (int i=0;i<=top;i++) if (strcmp (Book[i].getauthorname (), a) ==0 &&book[i].gettag () ==0) {if (r==0) COUT<<SETW (3) << "numbered" <<SETW (a) << "title" <<SETW (a) << "author" <<setw (a) < < "<<SETW" << "Publishing house" <<SETW (a) << "publication Time" <<SETW (a) << "book Price" <<SETW
(a) << "stock" <<endl;
e=&book[i];
E->disp ();
r++; if (r==0) cout<< "Can't find the book!" <<endl;
return NULL;
Book *query4 (char a[]) {//Find books by publisher *e;
int r=0; for (int i=0;i<=top;i++) if (strcmp (Book[i].getcbsname (), a) ==0 &&book[i].gettag () ==0) {if (r==0) COUT&L T;<SETW (3) << "numbered" <<SETW (a) << "title" <<SETW (a) << "author" <<setw (a) << "classification number" <<SETW << "Publishing house" <<SETW (a) << "publication Time" <<SETW (a) << "book Price" <<SETW (a) <
< "stock" <<endl;
e=&book[i];
E->disp ();
r++; if (r==0) cout<< "Can't find the book!"
<<endl;
return NULL; } void BookData ();
Library information Void disp () {for (int i=0;i<=top;i++) if (Book[i].gettag () ==0) Book[i].disp ();
~bdatabase () {//destructor, writes book[] to the Book.txt file FStream files ("Book.txt", ios::out);
for (int i=0;i<=top;i++) if (Book[i].gettag () ==0) file.write ((char *) &book[i],sizeof (book[i));
File.close ();
}
};
void Bdatabase::bookdata () {char choice3;
Char bname[40];//title char auname[40];//author nameChar bfenlei[40];//classification number char cname[40];//publishing house int time;//Publishing time double price;//price char ch;
int Choice4;
int BookID;
int Bookod;
Book *b;
while (choice3!= ' 0 ') {cout<< "\n\t\t\t** book information **\n" <<endl;
cout<< "\t\t\t** 1 Add * *" <<endl;
cout<< "\t\t\t** 2. Change * * <<endl;
cout<< "\t\t\t** 3. Delete * * <<endl;
cout<< "\t\t\t** 4. Find * * <<endl;
cout<< "\t\t\t** 5. Show * * <<endl;
cout<< "\t\t\t** 6" All deleted * * * <<endl;
cout<< "\t\t\t** 0. Return * * <<endl;
cout<< "\t\t\t Please choose:";
cin>>choice3;
Switch (choice3) {case ' 1 ': cout<< "Please enter the new book Number:";
cin>>bookid;
B=query1 (BookID); if (b!=null) {cout<< "the number already exists and cannot be added!"
<<endl;
Break
cout<< "Please enter the title of the new book:";
cin>>bname;
cout<< "Please enter the name of the new book Author:";
cin>>auname;
cout<< "Please enter the new book category number:";
cin>>bfenlei;
cout<< "Please enter new book publishing house:";
cin>>cname; cout<< "Please enter new book publication Time:";
cin>>time;
cout<< "Please enter the book Price:";
cin>>price;
cout<< "Please enter the number of new books:";
cin>>bookod;
Addbook (Bookid,bname,auname,bfenlei,cname,time,price,bookod);
Char Choice2;
{cout<< "\n\t\t\t** save y/n? **\n" <<endl; cout<< "\t\t\t** 1.
Y Save * * <<endl; cout<< "\t\t\t** 0.
N Do not save * * <<endl;
cout<< "\t\t\t Please choose:";
cin>>choice2;
Switch (CHOICE2) {case ' 1 ': cout<< "add book success!";
Break
Case ' 0 ': break;
} getch ();
Break
Case ' 2 ': cout<< "Please enter the book Number:";
cin>>bookid;
B=query1 (BookID); if (b==null) {cout<< "The book does not exist!"
"<<endl;
Break
cout<< "The information for this book is:" <<endl; COUT<<SETW (3) << "numbered" <<SETW (a) << "title" <<SETW (a) << "author" <<setw (a) < < "<<SETW" << "Publishing house" <<SETW (a) << "publication Time" <<SETW (a) << "book Price" <<SETW
(a) << "stock" <<endl; B->disp (); cout<< "Is it modified?"
(y/n): ";
cin>>ch; if (ch== ' y ') | |
ch== ' Y ') {int A;
cout<< "\n\t\t\t** Book Modification **\n" <<endl;
cout<< "\t\t\t** 1. Change the title * * <<endl;
cout<< "\t\t\t** 2. Modify Author * * <<endl;
cout<< "\t\t\t** 3. Modify the category number * * * <<endl;
cout<< "\t\t\t** 4. Modify Publishing House * * <<endl;
cout<< "\t\t\t** 5. Revise publication time * * <<endl;
cout<< "\t\t\t** 6. Revise Book price * * * <<endl;
cout<< "\t\t\t** 7. Change the number of books * * * <<endl;
cout<< "\t\t\t** 0. Return * * <<endl;
cout<< "\t\t\t Please choose:";
cin>>a;
Switch (a) {case 1:cout<< "Please enter a new title:"; cin>>bname; B->setname (bname);
Break
Case 2:cout<< "Please enter a new author:"; cin>>auname; B->setauthorname (Auname);
Break
Case 3:cout<< "Please enter a new category number:"; cin>>bfenlei; B->setfenlei (Bfenlei);
Break
Case 4:cout<< "Please enter new book publishing house:"; cin>>cname; B->setcbs (CNAME);
Break
Case 5:cout<< "Please enter new book publication Time:"; Cin>>time;b->setcbtime (time);
Break
Case 6:cout<< "Please enter the book Price:"; cin>>price; B->setbookprice (price);
Break
Case 7:cout<< "Please enter a new stock:"; cin>>bookod; B->setonshelf (Bookod);
Break
Case 0:break;
} cout<< "Modify book success!";
Getch ();
Break
Case ' 3 ': cout<< "Please enter the book Number:";
cin>>bookid;
B=query1 (BookID); if (b==null) {cout<< "The book does not exist, cannot be deleted!"
<<endl;
Break
} b->delbook ();
cout<< "Delete success!";
Getch ();
Break
Case ' 4 ': cout<< ' \n\t\t\t** 1. Find by Book Number * * <<endl;
cout<< "\t\t\t** 2. Search by Book title * * <<endl;
cout<< "\t\t\t** 3. Search by Book author * * <<endl;
cout<< "\t\t\t** 4. Search by Book publishing house * * <<endl; cout<< "\t\t\t** 0.
Back to * * <<endl;
cout<< "\t\t\t Please choose:";
cin>>choice4;
Switch (choice4) {case 1:cout<< "Please enter the book Number:";
cin>>bookid;
B=query1 (BookID);
if (b==null) {cout<< "The book does not exist!";
Break COUT<<SETW (3) << "numbered" <<SETW (a) << "title" <<SETW (a) << "author" <<setw (a) < < "<<SETW" << "Publishing house" <<SETW (a) << "publication Time" <<SETW (a) << "book Price" <<SETW
(a) << "stock" <<endl;
B->disp ();
Break
Case 2:cout<< "Please enter book title:";
cin>>bname;
B=query2 (bname);
Break
Case 3:cout<< "Please input book author:";
cin>>auname;
B=query3 (Auname);
Break
Case 4:cout<< "Please input book publishing house:";
cin>>cname;
B=query4 (CNAME);
Break
Case 0:break;
} break; Case ' 5 ': COUT<<SETW (3) << "numbered" <<SETW (Ten) << "title" <<SETW (a) << "author" <<SETW (10 << "<<SETW" << "Publishing house" <<SETW (a) << "publication Time" <<SETW (a) << "book Price" <
<SETW << "Stock" <<endl;
Disp ();
Getch ();
Break
Case ' 6 ': Clear ();
Break
Default:break; Class Reader {//reader's Information description private: int tag; Delete Mark 1 means that 0 has been deleted to indicate that int no is not deleted; Reader number char name[20]; Reader name int Borbook[maxbor];
Borrowed book Public:reader ()//constructor {;}
Friend Ostream &operator<< (ostream &output,reader &rd) {output<<rd.no; output<< "";
output<<endl;
return output;}
Char *getname () {//Get name return name;
int Gettag () {//Get delete tag return tag;
int Getno () {//Get reader number return no;
} void SetName (char na[]) {//Set name strcpy (Name,na);
void Delbook () {//Set deletion Mark 1: deleted 0: not deleted char i; cout<< "OK delete?" y/n? "
<<endl;
cin>>i; if (i== ' y ') | |
i== ' Y ') tag=1;
} void Addreader (int n,char *na) {//Add reader tag=0;
No=n;
strcpy (Name,na);
for (int i=0;i<maxbor;i++) borbook[i]=0; } void Borrowbook (int bookid) {//library operation for (int i=0;i<maxbor;i++) {if (borbook[i]==0) {Borbook[i]=book
Id
return;
{Retbook int bookid) {//book return operation for (int i=0;i<maxbor;i++) {if (Borbook[i]==bookid) { Borbook[i]=0; cout<< "book Success!"
<<endl;
return 1; } cout<< "did not borrow the book, The return failed!"
<<endl;
return 0;
} void Disp () {//Read reader information int have=0;
int bz=0;
COUT<<SETW (5) <<NO<<SETW <<NAME<<SETW (15);
for (int i=0;i<maxbor;i++) if (borbook[i]!=0) {if (bz==0) {have=1;
cout<< "[" <<borbook[i]<< "]\t\t" <<endl;
bz++;
else {cout<< "\r\t\t\t\t\t" "[" <<borbook[i]<< "]\t\t" <<SETW () <<endl;
} if (have==0) cout<< "\ t has not yet borrowed a book" <<endl;
}
}; Class Rdatabase {//Reader class library, to achieve the establishment of the reader's personal data private:int top;//reader record pointer reader READ[MAXR];//reader Record public:rdatabase () {//construct
Create a function to read Reader.txt to read[] in reader s;
Top=-1; FStream file ("Reader.txt", ios::in);
Opens an input file while (1) {File.read ((char *) &s,sizeof (s));
if (!file) break;
top++;
Read[top]=s; } file.close (); Turn off Reader.txt file} void Clear () {//deleteAll reader information char i; cout<< "OK to delete all?" y/n? "
<<endl;
cin>>i; if (i== ' y ') | |
i== ' Y ') top=-1;
int addreader (int n,char *na) {/////Add reader first to find reader *p=queryid (n);
if (p==null) {top++;
Read[top].addreader (N,na);
return 1;
else cout<< "The number already exists!";
return 0; Reader *queryid (int readerid) {//find for by Reader ID for (int i=0;i<=top;i++) if (Read[i].getno () ==readerid&&read
[I].gettag () ==0) {return &read[i];
return NULL; Reader *queryname (char readername[10]) {//lookup by Reader name for (int i=0;i<=top;i++) if (strcmp Read[i].getnam
E (), readername) ==0 && Read[i].gettag () ==0) {return &read[i];
return NULL; void disp () {//output all reader information for (int i=0;i<=top;i++) if (Read[i].gettag () ==0) read[i
].disp (); } void Readerdata ();
Reader Library Information ~rdatabase () {//destructor, writes read[] to the Reader.txt file FStream files ("Reader.txt", ios::out); for (int i=0;i<=top;i++) IF (Read[i].gettag () ==0) file.write ((char *) &read[i],sizeof (read[i));
File.close ();
}
};
void Rdatabase::readerdata () {char choice1;
Char rname[20];
int Readerid;
Char readername[10];
int choice2;
Reader *r;
while (choice1!= ' 0 ') {cout<< "\n\t\t\t** reader information **\n" <<endl; cout<< "\t\t\t** 1.
Add * * "<<endl; cout<< "\t\t\t** 2.
Change * * <<endl; cout<< "\t\t\t** 3.
Delete * * "<<endl; cout<< "\t\t\t** 4.
Find * * <<endl; cout<< "\t\t\t** 5.
Show * * <<endl; cout<< "\t\t\t** 6.
Delete all * * * <<endl; cout<< "\t\t\t** 0.
Back to * * <<endl;
cout<< "\t\t\t Please choose:";
cin>>choice1;
Switch (CHOICE1) {case ' 1 ': cout<< "Please enter reader number:";
cin>>readerid;
cout<< "Please enter the reader's name:";
cin>>rname;
Addreader (Readerid,rname); cout<< "Add reader success!"
<<endl;
Getch ();
Break
Case ' 2 ': cout<< "Please input reader number:";
cin>>readerid; R=Queryid (Readerid); if (r==null) {cout<< "The reader does not exist!"
"<<endl;
Break
cout<< "Please enter a new name:";
cin>>rname;
R->setname (Rname); cout<< "Modify reader success!"
<<endl;
Getch ();
Break
Case ' 3 ': cout<< "Please input reader number:";
cin>>readerid;
R=queryid (Readerid);
if (r==null) {cout<< "The reader does not exist!") << Endl;
Break
} r->delbook (); cout<< "Delete Successful!"
<<endl;
Getch ();
Break
Case ' 4 ': cout<< ' \n\t\t\t** 1. Find by Reader number * * <<endl;
cout<< "\t\t\t** 2. Search by Reader name * * <<endl;
cout<< "\t\t\t** 0. Return * * <<endl;
cout<< "\t\t\t Please choose:";
cin>>choice2;
Switch (CHOICE2) {case 1:cout<< "Please enter reader number:";
cin>>readerid;
R=queryid (Readerid); if (r==null) {cout<< "The reader does not exist!")
<< Endl;
Break
COUT<<SETW << "Reader id" <<setw << "reader's name" <<SETW (a) << "library number" <<endl; r-≫disp ();
Break
Case 2:cout<< "Please enter the reader's name:";
cin>>readername;
R=queryname (Readername); if (r==null) {cout<< "The reader does not exist!")
<<endl;
Break
COUT<<SETW << "Reader id" <<setw << "reader's name" <<SETW (a) << "library number" <<endl;
R->disp ();
Break
Case 0:break;
} break; Case ' 5 ': COUT<<SETW << "Reader ID" <<SETW (s) << "reader's name" <<SETW () << "Library number" <
<endl;
Disp ();
Getch ();
Break
Case ' 6 ': Clear ();
Break
Default:break;
}} class Maindesk//Implementing the main interface of the program {char choice5;
Char Choice2;
Double xh,mm;
int Bookid,readerid;
Rdatabase Readerdb;
Reader *r;
Bdatabase bookdb;
Book *b;
Public:maindesk () {;} int Denglu () {int k=0; cout<< "\n\t\t\t Welcome to the Library Management system!"
<<endl;
cout<< "\t\t\t you a total of 3 landing opportunities" <<endl; while (choice2!= ' 0 ') {++k; cout<< "\t\t\t * * * 1. Login * *" <<eNdl cout<< "\t\t\t * * 0.
Exit * * <<endl;
cout<< "\t\t\t Please choose:";
cin>>choice2;
Switch (CHOICE2) {case ' 1 ': cout<< "Please enter school number and password" <<endl;
cin>>xh>>mm;
if ((xh>=138325039) && (xh<=138325039) && (xh==mm)) {cout<< Login succeeded <<endl;
Enterdesk (); } if ((xh<138325039) | | (xh>138325039) | |
(XH!=MM))
{cout<< "Login failed: You also have" <<3-k<< "Landing Opportunities" <<endl; if (k>=3) {cout<< "You have exceeded the maximum number of logins, the system automatically exits!"
<<endl;
return 1;
} continue;
} break;
Case ' 0 ': break;}
return 1;
return 0;
} void Enterdesk () {while (choice5!= ' 0 ') {cout<< "\n\t\t\t**** Book Management system ****\n\n";
cout<< "\t\t\t** 1. Book Info * * <<endl;
cout<< "\t\t\t** 2. Reader Information * * <<endl;
cout<< "\t\t\t** 3. Borrow Books * *" <<endl;
cout<< "\t\t\t** 4. Return of Books * *" <<endl; cout<< "\t\t\t** 0.
Exit * * <<endl; cout<< "\t\t\t Please choose: ";
cin>>choice5;
Switch (choice5) {case ' 1 ': Bookdb.bookdata ();
Break
Case ' 2 ': Readerdb.readerdata ();
Break
Case ' 3 ': cout<< "\t\t\t\t Library operation" <<endl;
cout<< "Please enter the Library reader number:";
cin>>readerid;
R=readerdb.queryid (Readerid); if (null==r) {//by number find out if there is a reader cout<< "there is no such reader, you cannot borrow books!"
<<endl;
Break
cout<< "Please enter the book number to borrow:";
cin>>bookid;
B=bookdb.query1 (BookID); if (b==null) {//by number find out whether there is the book cout<< "does not exist in the book, can not borrow books!"
<<endl;
Break if (B->borrowbook () ==0) {cout<< "The book has been borrowed and cannot borrow books!"
<<endl;
Break cout<< "Readers to borrow books successfully!"
<<endl;
R->borrowbook (B->getno ());
Break
Case ' 4 ': cout<< "\t\t\t\t book operation" <<endl;
cout<< "Please enter the book Reader number:";
cin>>readerid;
R=readerdb.queryid (Readerid);
if (r==null) {cout<< "does not exist the reader, cannot return the book" <<endl;Break
cout<< "Please enter the book number to return:";
cin>>bookid;
B=bookdb.query1 (BookID);
if (b==null) {cout<< "does not exist this book, cannot return books" <<endl;
Break
} b->retbook ();
R->retbook (B->getno ());
Break
Case ' 0 ': break;
Default:break;
}
}
}
};
void main ()//main function {Maindesk yourdesk; if (Yourdesk.denglu ()) cout<< "\t\t Thank you for using, Goodbye!"
<<endl;
}
Run Effect chart:
The above is the entire content of this article, I hope you can like it, grasp the realization of it