Current location - Training Enrollment Network - Books and materials - Realize the book query function
Realize the book query function
# include & ltiostream & gt

# include & ltiomanip & gt

# include & lt string & gt

# include & ltfstream & gt// input/output file stream class

Use namespace std

const int Maxr = 100; //The most readers

const int Maxb = 100; //The most books

const int max bor = 5; //Each reader can borrow up to five books.

//reader class, which describes the information of readers.

Class reader

{

Private:

Int label; //Delete tag 1: Deleted 0: Not deleted.

Int number; //Reader number

Character name [10]; //Reader's name

int bor book[max bor]; //Borrow books

Public:

Reader() {}

char * getname(){ return name; }//Get the name

int gettag(){ return tag; }//Get delete tag

int getno(){ return no; }//Get the reader number

Void setname(char na[]) // Set the name.

{

Strcpy (name, not applicable);

}

void del book(){ tag = 1; }//Set the delete flag 1: Deleted 0: Not deleted.

Void addreader(int n, char *na)// Add reader.

{

tag = 0;

no = n;

Strcpy (name, not applicable);

for(int I = 0; I & ltMaxbori++)

bor book[I]= 0;

}

Void borrowbook(int bookid)// borrowing operation

{

for(int I = 0; I & ltMaxbori++)

{

if (borbook[i]==0)

{

bor book[I]= bookid;

Return;

}

}

}

Int retbook(int bookid)// book return operation

{

for(int I = 0; I & ltMaxbori++)

{

if(borbook[i]==bookid)

{

bor book[I]= 0;

Returns1;

}

}

Returns 0;

}

Void disp()// Read the reader information.

{

cout & lt& ltsetw(5)& lt; & lt No< setw (10) < & lt name & lt& lt "Borrowing number: [";

for(int I = 0; I & ltMaxbori++)

if(borbook[i]! =0)

cout & lt& ltbor book[I]& lt; & lt"|";

cout & lt& lt"]" & lt; & ltendl

}

};

//reader class library, to realize the establishment of readers' personal data.

Category database

{

Private:

Int top// reader record pointer

Reader read [maxr]; //Reader record

Public:

The RDatabase() // constructor reads reader.txt into read[].

{

Reader s;

top =- 1;

The fstream file ("reader.txt", IOs:: in); //Open an input file

while ( 1)

{

file . read((char *)& amp; s,sizeof(s));

If (! File) interrupt;

top++;

read[top]= s;

}

file . close(); //close reader.txt

}

Void clear()// Delete all reader information.

{

top =- 1;

}

Int addreader(int n, char *na)// When adding readers, check whether they exist.

{

reader * p = query(n);

if (p==NULL)

{

top++;

Read [top]. addreader(n,na);

Returns1;

}

Returns 0;

}

Reader *query(int readerid)// Search by number.

{

for(int I = 0; I<= topi++)

If (read [me]. getno()= = readerid & amp; & amp

Read [me]. gettag()==0)

{

Change & to [i];

}

Returns NULL

}

Void disp() // Output all reader information.

{

for(int I = 0; I<= topi++)

Read [me]. disp();

}

void reader data(); //Reader Library Maintenance

~RDatabase() // destructor, write read[] to the reader.txt file.

{

The fstream file ("reader.txt", IOs:: out);

for(int I = 0; I<= topi++)

If (read [me]. gettag()==0)

file . write((char *)& amp; read[i],sizeof(read[I]);

file . close();

}

};

void RDatabase::readerdata()

{

Char selection;

char rname[20];

int readerid

reader * r;

And (choose! ='0')

{

Cout & lt& lt" \ n \ n \ t \ tReader maintenance \ n \ n \ t1is newly added \ n \ n \ t \ t 2 has been changed \ n \ n \ t \ t 3 has been deleted \ n

CIN & gt; & gt selection;

Switch (selection)

{

Case "1":

Cout & lt& lt "Enter the card reader number:";

CIN & gt; & gtreaderid

Cout & lt& lt "Enter the reader name:";

CIN & gt; & gtrname

addreader (readerid,rname);

Break;

Case "2":

Cout & lt& lt "Enter the card reader number:";

CIN & gt; & gtreaderid

r = query(readerid);

if (r==NULL)

{

Cout & lt& lt "Readers don't exist"

Break;

}

Cout < < "Enter a new name:";

CIN & gt; & gtrname

r-& gt; set name(rname);

Break;

Case "3":

Cout & lt& lt "Enter the card reader number:";

CIN & gt; & gtreaderid

r = query(readerid);

if (r==NULL)

{

Cout & lt& lt "Readers don't exist"

Break;

}

r-& gt; del book();

Break;

Case "4":

Cout & lt& lt "Read in the reader number:";

CIN & gt; & gtreaderid

r = query(readerid);

if (r==NULL)

{

Cout & lt& lt "Readers don't exist"

Break;

}

r-& gt; disp();

Break;

Case "5":

disp();

Break;

Case "6":

clear();

Break;

Default value: cout & lt& lt "entered incorrectly, please re-enter:"; Break;

}

}

}

//Book category, which realizes the description, book number, title, lending and return of books.

Classroom books

{

Private:

Int label; //Delete tag 1: Deleted 0: Not deleted.

Int number; //Book number

char name[20]; //Book title

Int onshelf// Do you want to borrow 1: Borrow 2: Borrow?

Public:

Book(){}

char * getname(){ return name; }//Get the name

int getno(){ return no; }//Get the book number

int gettag(){ return tag; }//Get delete tag

Void setname(char na[])// Set the title of the book.

{

Strcpy (name, not applicable);

}

void del book(){ tag = 1; }//Delete books

Voiddaddbook (int n, char *na)// Add books.

{

tag = 0;

no = n;

Strcpy (name, not applicable);

on shelf = 1;

}

Int borrowbook()// borrowing operation

{

if(on self = = 1)

{

on shelf = 0;

Returns1;

}

Returns 0;

}

Void retbook()// book return operation

{

on shelf = 1;

}

Void disp()// output books

{

cout & lt& ltsetw(6)& lt; & lt No< setw (18) < & lt name & lt& ltsetw( 10)

& lt& lt(onshelf== 1? On the shelf: "borrowed") < & ltendl

}

};

//Library class, which can maintain, search and delete books.

Class b database

{

Private:

Int top// book record pointer

book book[Maxb]; //Book records

Public:

The BDatabase()// constructor reads book.txt into book[].

{

Book b;

top =- 1;

The fstream file ("book.txt", IOs:: in);

while ( 1)

{

file . read((char *)& amp; b,sizeof(b));

If (! File) interrupt;

top++;

book[top]= b;

}

file . close();

}

Void clear()// Delete All

{

top =- 1;

}

Int addbook(int n, char *na)// Add books.

{

book * p = query(n);

if (NULL==p)

{

top++;

Book [top]. addbook(n,na);

Returns1;

}

Returns 0;

}

Book *query(int bookid)// find books.

{

for(int I = 0; I<= topi++)

If (book [me]. getno()= = bookid & amp; & Book [me]. gettag()==0)

{

Return to & book [i];

}

Returns NULL

}

void book data(); //Library maintenance

Invalid display ()

{

for(int I = 0; I<= topi++)

If (book [me]. gettag()==0)

Book [me]. disp();

}

~BDatabase()// destructor, and write book[] into the book.txt file.

{

The fstream file ("book.txt", IOs:: out);

for(int I = 0; I<= topi++)

If (book [me]. gettag()==0)

file . write((char *)& amp; book[i],sizeof(book[I]);

file . close();

}

};

void BDatabase::bookdata()

{

Char selection;

char bname[40];

int bookid

Book * b;;

And (choose! ='0')

{

Cout & lt& lt" \ n \ n \ n \ t \ t \ Library maintenance "< & ltendl & lt& ltendl

Cout & lt& lt" \t\t 1 was newly added \n \t\t2 was changed \n\t\t3 was deleted \n\t\t4 was searched \n\t\t5 was displayed \n\t\t6 was completely deleted \

CIN & gt; & gt selection;

Switch (selection)

{

Case "1":

Cout & lt& lt "Enter the book number:"

CIN & gt; & gtbookid

Cout & lt& lt "Enter a title:"

CIN & gt; & gtbname

addbook(bookid,bname);

Break;

Case "2":

Cout & lt& lt "Enter the book number:"

CIN & gt; & gtbookid

B = BookID;

if (b==NULL)

{

Cout & lt& lt "This book doesn't exist"

Break;

}

Cout & lt& lt "Enter a new title:"

CIN & gt; & gtbname

b-& gt; set name(bname);

Break;

Case "3":

Cout & lt& lt "Read the book number:"

CIN & gt; & gtbookid

B = BookID;

if (b==NULL)

{

Cout & lt& lt "This book doesn't exist"

Break;

}

b-& gt; del book();

Break;

Case "4":

Cout & lt& lt "Read the book number:"

CIN & gt; & gtbookid

B = BookID;

if (b==NULL)

{

Cout & lt& lt "This book doesn't exist"

Break;

}

b-& gt; disp();

Break;

Case "5":

disp();

Break;

Case "6":

clear();

Break;

Default value: cout & lt& lt "entered incorrectly, please re-enter:";

}

}

}

//The realization of the main () function and the guidance of the main interface of the program.

void main()

{

Char selection;

int bookid,readerid

RDatabase ReaderDB

reader * r;

B database BookDB

Book * b;;

And (choose! ='0')

{

Cout & lt< endl & lt<< "\ t \ t \ library management system \ n \ n \ n”;

Cout & lt< "\ t \ t1borrowing books \ n \ n \ t \ T2 Returning books \ n \ n \ t \ t3Library maintenance \ n \ n \ t \ t4Reader maintenance \ n \ n \ n \.

CIN & gt; & gt selection;

Switch (selection)

{

Case "1":

Cout & lt& lt borrows the reader number:;

CIN & gt; & gtreaderid

Cout & lt& lt "ISBN:";

CIN & gt; & gtbookid

r = readerdb . query(readerid); //Search by number

if (NULL==r)

{

Cout & lt& lt "Readers don't exist, books can't be borrowed" < & ltendl

Break;

}

b = bookdb . query(bookid);

if (b==NULL)

{

Cout & lt& lt "This book doesn't exist, so you can't borrow it" < & ltendl.

Break;

}

if(b-& gt; borrowbook()==0)

{

Cout & lt& lt "This book has been lent out, so you can't borrow it any more" < & ltendl.

Break;

}

r-& gt; Borrowing books (b-> getno());

Break;

Case "2":

Cout & lt& lt "returns the book \ nReader number:";

CIN & gt; & gtreaderid

Cout & lt& lt "ISBN:";

CIN & gt; & gtbookid

r = readerdb . query(readerid);

if (r==NULL)

{

Cout & lt& lt "The reader does not exist, so the book cannot be returned" < & ltendl.

Break;

}

b = bookdb . query(bookid);

if (b==NULL)

{

Cout & lt& lt "The book does not exist and cannot be returned" < & ltendl

Break;

}

b-& gt; ret book();

r-& gt; retbook(b-& gt; getno());

Break;

Case "3":

bookdb . book data();

Break;

Case "4":

reader db . reader data();

Break;

Default value: cout & lt& lt "entered incorrectly, please re-enter:";

}

}

}

Try running by yourself ~