Current location - Training Enrollment Network - Books and materials - Library system code
Library system code
# include & ltiostream & gt

# include & ltiomanip & gt

# include & lt string & gt

//input/output file stream class

# include & ltfstream & gt

Use namespace std

//The most readers

const int Maxr = 100;

//The most books

const int Maxb = 100;

//Each reader can borrow up to five books.

const int max bor = 5;

//reader class to realize the description of reader information.

Class reader

{

Private:

//Delete tag 1: Deleted 0: Not deleted.

Int label;

//Reader number

Int number;

//Reader's name

Character name [10];

//Borrow books

int bor book[max bor];

Public:

Reader ()

{

;

}

//Get the name

char *getname()

{

Returns the name;

}

//Get the delete flag

int gettag()

{

Return label;

}

//Get the reader number

int getno()

{

Return no;

}

//Set the name

Void collection name (char na[])

{

Strcpy (name, not applicable);

}

//Set the delete flag 1: Deleted 0: Not deleted.

void delbook()

{

tag = 1;

}

//Increase readers

void addreader(int n,char *na)

{

tag = 0;

no = n;

Strcpy (name, not applicable);

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

bor book[I]= 0;

}

//Book borrowing operation

Void book borrowing (int bookid)

{

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

{

if (borbook[i]==0)

{

bor book[I]= bookid;

Return;

}

}

}

//Book return operation

Int return book (int bookmark)

{

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

{

if(borbook[i]==bookid)

{

bor book[I]= 0;

Returns1;

}

}

Returns 0;

}

//Read the reader information

Invalid display ()

{

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:

//The reader records the pointer

int top

//Reader record

Reader read [maxr];

Public:

//constructor, read reader.txt into read[]

Database ()

{

Reader s;

top =- 1;

//Open an input file

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

while ( 1)

{

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

If (! File) interrupt;

top++;

read[top]= s;

}

//close reader.txt

file . close();

}

//Delete all reader information

Empty Clear ()

{

top =- 1;

}

//Look for the existence before adding a reader.

int addreader(int n,char *na)

{

reader * p = query(n);

if (p==NULL)

{

top++;

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

Returns1;

}

Returns 0;

}

//Search by number

Reader * query (int readerid)

{

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

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

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

{

Change & to [i];

}

Returns NULL

}

//Output all reader information.

Invalid display ()

{

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

Read [me]. disp();

}

//Reader Library Maintenance

void reader data();

//destructor, write read[] into the reader.txt file.

~ r database ()

{

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;

}

}

}

//Books, realizing the description, book number, title and borrowing of books.

Private:

//Delete tag 1: Deleted 0: Not deleted.

Int label;

//Book number

Int number;

//Book title

char name[20];

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

int onshelf

Public:

Book(){}

//Get the name

char * getname(){ return name; }

//get the book number

int getno(){ return no; }

//Get the delete flag

int gettag(){ return tag; }

//Set the title of the book

Void collection name (char na[])

{

Strcpy (name, not applicable);

}

//Delete books

void del book(){ tag = 1; }

//Add books

void addbook(int n,char *na)

{

tag = 0;

no = n;

Strcpy (name, not applicable);

on shelf = 1;

}

//Book borrowing operation

int borrowbook()

{

if(on self = = 1)

{

on shelf = 0;

Returns1;

}

Returns 0;

}

//Book return operation

void retbook()

{

on shelf = 1;

}

//output books

Invalid display ()

{

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:

//Book record pointer

int top

//Book records

book book[Maxb];

Public:

//constructor, read book.txt into book[]

B database ()

{

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();

}

//Delete All

Empty Clear ()

{

top =- 1;

}

//Add books

int addbook(int n,char *na)

{

book * p = query(n);

if (NULL==p)

{

top++;

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

Returns1;

}

Returns 0;

}

//Find books

Book * query (int bookmark)

{

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

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

{

Return to & book [i];

}

Returns NULL

}

//Library maintenance

void book data();

Invalid display ()

{

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

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

Book [me]. disp();

}

//destructor, write book[] into the book.txt file.

~ b database ()

{

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; {

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

//Search by number

r = readerdb . query(readerid);

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:";

}

}

}