📄 libary.cpp
字号:
#include <iostream.h>
#include <fstream.h>
#include <string>
#include <stdlib.h>
// The base class for books in the library.
class book_cl
{
protected:
int book_cd;
char book_nm[30];
char publisher[30];
public:
/* Function to accept the details common to both books and periodicals.
It takes the last book code as a parameter. */
void getdata(int lcd)
{
char check;
cout << "\n\n\tADD NEW BOOK/PERIODICAL DETAILS" << endl;
book_cd=lcd+1;
cout << "\n\nBook code: " << book_cd << endl;
// Verify that the book name is not left blank.
check='n';
while(check=='n')
{
cout << endl << "Enter Book/Periodical name: ";
cin.getline(book_nm, 30);
if((strlen(book_nm))==0)
cout << "\n\n\tBook/Periodical name must be entered.";
else
check='y';
}
// Verify that the publisher name is not left blank.
check='n';
while(check=='n')
{
cout << "\nEnter Publisher name: ";
cin.getline(publisher, 30);
if((strlen(publisher))==0)
cout << "\n\n\tPublisher name must be entered.";
else
check='y';
}
}
// Function to display the details common to books and periodicals.
void dispdata(void)
{
cout <<"\nCode: " << book_cd;
cout <<"\tName: " << book_nm;
}
};
// The derived class for books in the library.
class book:public book_cl
{
protected:
char author_nm[30];
char pubdate[8];
float price;
char status;
public:
/* Function to accept the book specific details. It takes the last book
code as a parameter. */
void getdata(int lcode)
{
char check;
// Invoke the function in the base class.
book_cl::getdata(lcode);
// Verify that the author name is not left blank.
check='n';
while(check=='n')
{
cout << "\nEnter Author Name: ";
cin.getline(author_nm, 30);
if((strlen(author_nm))==0)
cout << "\n\n\tAuthor name must be entered.";
else
check='y';
}
cout << "\nEnter Publishing date: ";
cin >> pubdate;
cin.ignore();
// Verify that the price is greater than 0.
check='n';
while(check=='n')
{
cout << "\nEnter price: ";
cin >> price;
cin.ignore();
if(price<=0)
cout << "\n\n\tBook price must be greater than 0.";
else
check='y';
}
// Set the new book's status as available.
status='A';
}
// Function to display the details specific to books.
void dispdata(void)
{
book_cl::dispdata();
cout <<"\tAuthor: " << author_nm;
}
// Function that returns the book code.
int getcode(void)
{
return book_cl::book_cd;
}
// Function to set the book status to the value received as a parameter.
void setstatus(char pstatus)
{
status=pstatus;
}
// Function that returns the book status.
char getstatus(void)
{
return status;
}
};
// The derived class for periodicals in the library.
class periodical:public book_cl
{
protected:
char periodicity;
public:
/* Function to accept the periodical specific details. It takes the last
periodical code as a parameter. */
void getdata(int lcode)
{
char check;
// Invoke the function in the base class.
book_cl::getdata(lcode);
// Verify that a valid periodicity is entered.
check='n';
while(check=='n')
{
cout << "\nEnter Periodicity (F/M/S/Y): ";
cin >> periodicity;
if(periodicity>='a' && periodicity<='z')
periodicity=periodicity-32;
if(periodicity!='F' && periodicity!='M' && periodicity!='S' && periodicity!='Y')
cout << "\n\n\tPeriodicity must be entered as F for Fortnightly, M for Monthly,S for Six-monthly, and Y for Yearly";
else
check='y';
}
}
// Function that returns the book code.
int getcode(void)
{ return book_cl::book_cd;
}
// Function that returns the book name.
char *getname(void)
{ return book_cl::book_nm;
}
};
// The class for periodical issues in the library.
class perissue
{
protected:
int percode;
int issno;
char issdate[8];
float price;
char status;
public:
/* Function to accept the details of an issue for a periodical.
It takes the periodical code and issue number as parameters. */
void getdata(int pcode, int issuenum)
{
char check;
percode=pcode;
issno=issuenum;
cout << "\nEnter Issue date: ";
cin >> issdate;
// Verify that the price is greater than 0.
check='n';
while(check=='n')
{
cout << "\nEnter price: ";
cin >> price;
if(price<=0)
cout << "\n\n\tIssue price must be greater than 0.";
else
check='y';
}
// Set the new issue's status as available.
status='A';
}
/* Function to display the details of the issue. It takes the periodical
name as a parameter. */
void dispdata(char *fbooknm)
{
cout << "\nCode: " << percode;
cout << "\tName: " << fbooknm;
cout << "\tIssue: " << issno;
cout <<"\tIssue date: " << issdate;
}
// Function that returns the issue number.
int getissno(void)
{ return issno;
}
// Function that returns the periodical code.
int getpercd(void)
{ return percode;
}
// Function that returns the availability status of the issue.
char getstatus(void)
{
return status;
}
// Function to set the issue status to the value received as a parameter.
void setstatus(char pstatus)
{
status=pstatus;
}
};
void main(void)
{
int choice=0;
// Function declarations.
void addbook(void);
void addper(void);
void issueret(void);
void delissue(void);
void viewavail(void);
// Loop to display the menu.
while(choice!=6)
{
system("cls");
cout << "\n\n\tLIBRARY MANAGEMENT SYSTEM";
cout << "\n\n1. Add new Book details";
cout << "\n2. Add new Periodical details";
cout << "\n3. Record Issue/Return details";
cout << "\n4. Delete old Periodical Issue details";
cout << "\n5. View Available Books/Periodicals";
cout << "\n6. Exit";
cout << "\n\n\tEnter choice: ";
// Accept user input menu choice.
cin >> choice;
cin.ignore();
// Check user input menu choice and invoke appropriate function.
if(choice==1)
addbook();
else if(choice==2)
addper();
else if(choice==3)
issueret();
else if(choice==4)
delissue();
else if(choice==5)
viewavail();
else if(choice==6)
return;
else
cout << "\n\n\tINVALID CHOICE! Valid choices are 1 to 6.";
}
}
// Function to accept and add new book details to the Book.dat file.
void addbook(void)
{
// Declare objects of the book class.
book book1, book2;
int lastcode;
system("cls");
// Open Book.dat file for reading.
fstream finout("Book.dat", ios::in);
if(!finout)
{ cout << "Could not open Book.dat file.";
return;
}
// Obtain the book code of the last book in the file.
finout.seekg(0);
while(finout.read((char *)&book1, sizeof(book1))){}
lastcode=book1.getcode();
/* If the details of the first book are being entered, set last
book code to 0.*/
if(lastcode<0)
lastcode=0;
// Close the file.
finout.close();
/* Invoke the function to accept book details and pass the last book
code as a parameter to it. */
book2.getdata(lastcode);
// Reopen Book.dat file for adding the new record.
finout.open("Book.dat", ios::app);
if(!finout)
{ cout << "Could not open Book.dat file.";
return;
}
// Write the new record to the file.
finout.write((char *)&book2, sizeof(book2));
// Close the file.
finout.close();
return;
}
/* Function to accept and add new periodical details to the Periodical.dat
file, or new issue details to the Issue.dat file. */
void addper(void)
{
char reply, check;
system("cls");
// Accept whether Periodical of Issue details are being input.
check='n';
while(check=='n')
{
cout << "\n\nAdd Periodical details or Issue details? (P/I): ";
cin >> reply;
cin.ignore();
if(reply!='P' && reply!='I' && reply!='p' && reply!='i')
cout << "\n\n\tPlease enter P for periodical entry or I for Issue entry.";
else
check='y';
}
if(reply=='P' || reply=='p')
{
// Declare objects of the class periodical.
periodical per1, per2;
int lastcode;
// Open Periodical.dat file for reading.
fstream finout("Periodical.dat", ios::in);
if(!finout)
{ cout << "Could not open Periodical.dat file.";
return;
}
// Obtain the periodical code of the last periodical in the file.
finout.seekg(0);
while(finout.read((char *)&per1, sizeof(per1))){}
lastcode=per1.getcode();
/* If the details of the first periodical are being entered, set last
book code to 0.*/
if(lastcode<0)
lastcode=0;
// Close the file.
finout.close();
/* Invoke the function to accept periodical details and pass the
last periodical code as a parameter to it. */
per2.getdata(lastcode);
// Reopen Periodical.dat file for adding the new record.
finout.open("Periodical.dat", ios::app);
if(!finout)
{ cout << "Could not open Periodical.dat file.";
return;
}
// Write the new record to the file.
finout.write((char *)&per2, sizeof(per2));
// Close the file.
finout.close();
return;
}
else
{
// Declare objects of the class perissue and periodical.
perissue issue1, issue2;
periodical per1;
int issnum1, issnum2, percd1, percd2;
char check='n';
fstream finout1, finout2;
// Open Periodical.dat file for reading.
finout1.open("Periodical.dat", ios::in);
if(!finout1)
{ cout << "Could not open Periodical.dat file.";
return;
}
/* Accept the periodical code for which the issue details are being
entered and ensure that it exists in Periodical.dat. */
while(check=='n')
{
cout << "\n\nEnter Periodical code: ";
cin >> percd1;
finout1.seekg(0);
int j=0;
while(finout1.read((char *)&per1, sizeof(per1)))
{
j++;
percd2=per1.getcode();
if(percd1==percd2)
{
check='y';
break;
}
}
if(j==0)
check='y';
if(check=='n')
cout << "\n\n\tINVALID PERIODICAL CODE! Please re-enter.";
}
// Close the Periodical.dat file.
finout1.close();
// Open Issue.dat file for reading.
finout2.open("Issue.dat", ios::in);
if(!finout2)
{ cout << "Could not open Issue.dat file.";
return;
}
/* Accept the issue number and ensure that the issue details for
the specified periodical do not exist in Issue.dat. */
check='n';
while(check=='n')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -