Program design specification
Design the borrowing management of special library.
The problem description library is suitable to be described by the object-oriented function of C++. The library management system is divided into four parts: borrowing and returning books, book management and reader service. Design a reader class to record the basic information of each reader; Reader library class Rdatabase, which records all reader information; Book, which records the basic information of each book; Library class Bdatabase, which records all book information.
basic requirement
In the 1 reader library class RDatabase, everything in the read.txt file is read into the reader record rede[] in its constructor. After processing, write all the undeleted records in read[] to read.txt in the destructor.
2. In the library class BDatabase, in its constructor, all books in the book.txt file are read into the book record book[]. After processing, write all the undeleted records in book[] to book.txt in the destructor.
3. Use the constructor to initialize the data of readers and books and complete the memory allocation. The destructor completes the requested heap memory space before the program ends.
4. Write the main function and test the matrix class comprehensively. The user interface is required to adopt menu mode. In the test, it is necessary to read the reader and book data from the disk file through I/O stream and display them on the screen at the same time. The obtained data is written into disk file storage through I/O stream and displayed on the screen at the same time.
There should be enough notes in the source program, and detailed flow charts and written materials should be included in the report.
Class design
The program contains the following four classes:
1. reader class, which has the basic management functions of readers and the following private data:
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
2. the 2.reader library class Rdatabase contains the following private data:
Int top// reader record pointer
Reader read [maxr]; //Reader record
3. Library books have the basic functions of books and the following private data:
Int label; //Delete tag 1: deleted; 0: Not deleted
Int number; //Book number
char name[20]; //Book title
Has Intonshelf/1been borrowed by shelf 0?
4. The library class BDatabase has the following private data:
Int top// book record pointer
book book[Maxb]; //Book records
Design description of special function
constructors
The constructor Reader () in 1. Reader class initialization function;
2. In the reader library class Rdatabase, its constructor RDatabase () reads all the contents in the read.txt file into the reader record rede[].
3. The constructor Book () in the Book class initializes the function;
4. in the library class BDatabase, in its constructor, read all the contents in the book.txt file into the book record book[].
Copy constructor
The copy constructor in the Reader class copies the return value of getname () to setname (), and the copy constructor in the Book class copies the return value of getname () to the book name setting function setname ().
destroyer
1. In the reader library class RDatabase, its destructor ~Rdatabase () writes all undeleted records in read[] to read.txt
2. in the library class BDatabase, its destructor ~Bdatabase () writes all the undeleted records in book[] into book.txt
Operator overloading
Overloaded "=", when comparing reader number and book number, overloaded; Overloaded bit operators ""and ""and so on.
Flow chart of main function algorithm
How to use this program
1. Enter the operation page and follow the prompts;
2. First add books and reader information, and then you can operate the existing information;
3. During the operation, you can add, change and delete the information of books or readers at any time;
4. When you choose to exit, clear the screen.
2. Computer program debugging report
Grammatical errors and their elimination
1. There are many spelling mistakes when typing the program. For example, Readdata () is wrongly typed as readdate () in many places. The ending semicolon is entered by different input methods. These small mistakes are hard to find at first, but with experience, they are easy.
2. An error occurred while creating a new constructor. Overloading the constructor should pay attention to the access rights of the function, so that the result will not be wrong.
Algorithm error and its elimination
1. In the book lending operation function of reader class, for(int I = 0;; I & ltMaxborI++) misspelled as for (int I =1; I & ltMaxborI++), causing a loop error.
2. The pointer is used incorrectly, and pointers B and R are confused, resulting in compilation errors and error reports of "Error C2660:' Retbook': function does not take 1 parameter".
Test results of three programs
Harvest and experience
In addition to the classroom, curriculum design is the most studied and tested. In the course of curriculum design, I always feel that I don't understand a lot and have too much to learn! After this course design, I have a deeper understanding of C++ language and improved my operational ability. The satisfaction of the typed code after running requires patience, carefulness, perseverance and abundant physical strength. Only after editing, compiling and running many times can you write a better program, and sometimes you need to modify it many times to achieve the ideal running effect. To learn programming, you need to ponder, practice, practice, and practice again, so that your programming skills can go up a storey still higher. In addition, you have to consult more experts!
source code
//********************
//Library lending management
//Category:
//Student ID:
//Name:
//********************
# include & ltiostream.h & gt
# include & ltiomanip.h & gt
# include & ltstring.h & gt
# include & ltfstream.h & gt
const int Maxr = 1000; //The most readers
const int Maxb = 10000; //The most books
const int max bor = 8; //Each reader can borrow up to 8 books.
Class reader//reader class
{
Int label; //Delete tag 1 has been deleted; 0 was not deleted.
Int number; //Reader number
Character name [10]; //Reader's name
int bor book[max bor]; //Borrow books
Public:
Reader(){}// constructor
char * getname(){ return name; }//Get the reader's name
int gettag(){ return tag; }//Get delete tag
int getno(){ return no; }//Get the reader number
Void setname(char na[])// Sets the name of the reader.
{
Strcpy (name, not applicable);
}
void del book(){ tag = 1; }//Set the delete flag
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;
}
}
}
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()// Output reader information.
{
cout & lt& ltsetw(5)& lt; & lt No< setw (10) < & lt name & lt& lt borrowing number: ";
for(int I = 0; I, Maxbori++)
if(borbook[i]! =0)
cout & lt& ltbor book[I]& lt; & ltendl
}
};
Class RDA base// reader library class
{
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);
while( 1)
{
file . read((char *)& amp; s,sizeof(s));
If (! File) interrupt;
top++;
read[top]= s;
}
file . close();
}
Void clear()// Delete all reader information.
{
top =- 1;
}
Int addreader(int n, char *na)// Add a reader and check whether it already exists.
{
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; & Read [me]. gettag==0)
Change & to [i];
Returns NULL
}
Void disp()// Delete all the information of the reader.
{
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()
{
int choice = 1;
char rname[20];
int readerid
reader * r;
And (choose! =0)
{
Cout & lt& lt "reader service 1 add 2, correct 3, delete 4, search 5, display 6, delete all 0s and exit" < & ltendl
CIN & gt; & gt selection;
Switch (selection)
{
Case 1:
Cout & lt& lt "Please enter the reader number:";
CIN & gt; & gtreaderid
Cout & lt& lt "read in the name of the reader:";
CIN & gt; & gtrname
addreader(readerid,rname);
Break;
Case 2:
Cout & lt& lt "Please enter the reader number:";
CIN & gt; & gtreaderid
r = query(readerid);
if(r==NULL)
{
Cout & lt& lt "Readers don't exist"
Break;
}
Cout & lt& lt Please enter a new reader name: "";
CIN & gt; & gtrname
r-& gt; set name(rname);
Break;
Case 3:
Cout & lt& lt "Please enter the 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 "Please enter the reader number:";
CIN & gt; & gtreaderid
r = query(readerid);
if(r==NULL)
{
Cout & lt& lt "Card reader number does not exist"
Break;
}
Case 5:
disp();
Break;
Case 6:
clear();
Break;
}
}
}
Classroom books//books
{
Int label; //Delete tag 1: deleted; 0: Not deleted
Int number; //Book number
char name[20]; //Book title
Has Intonshelf/1been borrowed by shelf 0?
Public:
Book(){}
char * getname(){ return name; }//Get the reader's 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 setw (10) < & lt(onshelf== 1? On the shelf: "borrowed") < & ltendl
}
};
Class b database//library class
{
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(p==NULL)
{
top++;
Book [top]. addbook(n,na);
Returns1;
}
Cout & lt& lt "added successfully!" & lt& ltendl
}
Book*query(int bookid)// find books.
{
for(int I = 0; I<= topi++)
If (book [me]. getno()= = bookid & amp; & Book [me]. gettag()==0)
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()
{
Int selection;
char bname[40];
int bookid
Book * b;;
And (choose! =0)
{
Cout & lt& lt "Book Management 1 Add 2, Change 3, Delete 4, Search 5, Display 6, Delete All 0s and Exit" < & ltendl
CIN & gt; & gt selection;
Switch (selection)
{
Case 1:
Cout < < "Enter the book number:";
CIN & gt; & gtbookid
Cout & lt& lt "Enter a title:";
CIN & gt; & gtbname
addbook(bookid,bname);
Break;
Case 2:
Cout < < "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 book name:";
CIN & gt; & gtbname
b-& gt; set name(bname);
Cout & lt& lt "Rename succeeded!" & lt& ltendl
Break;
Case 3:
Cout < < "Enter the book number:";
CIN & gt; & gtbookid
B = BookID;
if(b==NULL)
{
Cout & lt& lt "This book doesn't exist!" & lt& ltendl
Break;
}
b-& gt; del book();
Break;
Case 4:
Cout & lt& lt "Please enter the ISBN:";
CIN & gt; & gtbookid
B = BookID;
if(b==NULL)
{
Cout & lt& lt "This book doesn't exist!" & lt& ltendl
Break;
}
b-& gt; disp();
Break;
Case 5:
disp();
Break;
Case 6:
clear();
Break;
}
}
}
void main()
{
int choice= 1,bookid,readerid
RDatabase ReaderDB
reader * r;
B database BookDB
Book * b;;
And (choose! =0)
{
Cout & lt& lt "Borrow books from 1?" & lt& ltendl
Cout & lt& lt "Return the book?" & lt& ltendl
Cout & lt< "3 book maintenance?" & lt& ltendl
Cout & lt< "4 reader maintenance?" & lt& ltendl
Cout & lt< "0 left?" & lt& ltendl
CIN & gt; & gt selection;
Switch (selection)
{
Case 1:
Cout & lt& lt "To borrow books, please enter the reader number":
CIN & gt; & gtreaderid
Cout & lt& lt "ISBN:";
CIN & gt; & gtbookid
r = readerdb . query(readerid);
if(r==NULL)
{
Cout & lt& lt "Haha, borrowing books succeeded!" ;
Break;
}
b = bookdb . query(bookid);
if(b==NULL)
{
Cout & lt& lt "This book doesn't exist!" & lt& ltendl
Break;
}
if(b-& gt; borrowbook()==0)
{
Cout & lt& lt "The book has been borrowed"
Break;
}
r-& gt; Borrowing books (b-> getno());
Break;
Cout & lt& lt "Borrowing books succeeded!" & lt& ltendl
Case 2:
Cout & lt& lt To return the book, please enter the reader number:
CIN & gt; & gtreaderid
Cout & lt& lt "Please enter the book number;" ;
CIN & gt; & gtbookid
r = readerdb . query(readerid);
if(r==NULL)
{
Cout & lt& lt "Reader does not exist!" & lt& ltendl
Break;
}
b-& gt; ret book();
r-& gt; retbook(b-& gt; getno());
Break;
Cout & lt& lt "Return the book successfully!" & lt& ltendl
Case 3:
bookdb . book data();
Break;
Case 4:
reader db . reader data();
Break;
}
}
}
Output result
1 borrow books
Return the book
3 book management
4 reader service
0 exit
three
Book Management 1 Add 2, Change 3, Delete 4, Find 5, Display 6, Delete 0 all and exit.
1
Enter the ISBN:111.
Enter the title: Legend of Wulin.
Added successfully!
Book Management 1 Add 2, Change 3, Delete 4, Find 5, Display 6, Delete 0 all and exit.
1
Enter ISBN: 2222
Enter the title: Sleepless Tonight.
Added successfully!
Book Management 1 Add 2, Change 3, Delete 4, Find 5, Display 6, Delete 0 all and exit.
1 borrow books
Return the book
3 book management
4 reader service
0 exit
four
Reader service 1 add 2, correct 3, delete 4, search 5, display 6, delete all 0s and exit.
1
Please enter the reader number: 00 1.
Read the reader's name: Zhou Qiang
Reader service 1 add 2, correct 3, delete 4, search 5, display 6, delete all 0s and exit.
1 borrow books
Return the book
3 book management
4 reader service
0 exit
1
Please enter the reader number: 00 1 to borrow books.
Book number:111/
Haha, borrowing books succeeded! 1 borrow books
Return the book
3 book management
4 reader service
0 exit
2
Please enter the reader number: 00 1 to return the book.
Please enter the book number; 1 1 1 1
Return the book successfully!
1 borrow books
Return the book
3 book management
4 reader service
0 exit
three
Book Management 1 Add 2, Change 3, Delete 4, Find 5, Display 6, Delete 0 all and exit.
2
Enter ISBN: 2222
Enter the name of the new book: Complete Works of Jin Yong.
Rename succeeded!
Press any key to continue.
Is this ok?