📄 book_library.cpp
字号:
/* PROJECT BOOK LIBRARY */
// -: INCLUDED HEADER FILES
#include "fstream.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "ctype.h"
#include "conio.h"
#include "dos.h"
// THIS CLASS CONTROL ALL THE FUNCTIONS IN THE
// -:MENU
class MENU
{
public :
void ma_menu(void) ;
private :
void ed_menu(void) ;
void ed_book(void) ;
void ed_member(void) ;
};
// THIS CLASS CONTAINS FUNCTIONS RELATED TO
// -:BOOKS
class BOOK
{
public :
void table(void) ;
char *b_name(int) ;
protected :
void add_book(int, char tname[33], char tauthor[26], float, int, int) ;
void update_copies(int, int, int) ;
void change(int, char[], char[], float) ;
void deletion(void) ;
int b_found(int) ;
int b_name_found(char []) ;
int recordno(int) ;
int available(int) ;
char *a_name(int) ;
float b_price(int) ;
int no_of_copies(int) ;
int b_codeof(char[]) ;
void display(int) ;
int reccount(void) ;
void delete_rec(int) ;
private :
int bookcode, copies ;
char name[33], author[26] ;
float price ;
int avail ;
};
// THIS CLASS CONTAINS FUNCTIONS RELATED TO
// -:MEMBERS
class MEMBER
{
public :
void jot(void) ;
protected :
void add_mem(int,int,char[],char[],char[],int,int,int) ;
void mod(int,char[],char[],char[]) ;
void deletion(void) ;
int m_found(int) ;
void update_b(int,int,int,int,int) ;
char *m_name(int) ;
char *m_phone(int) ;
char *m_address(int) ;
int recordno(int) ;
int ltcode(void) ;
int issued(int) ;
int fine(int) ;
void display(int) ;
void del_rec(int) ;
private :
int memcode,bookcode ;
char name[26],phone[10],address[33] ;
int dd,mm,yy ; // DATE OF RETURNING THE BOOK //
};
// THIS IS DERIVED FROM THE BASE CLASSES
// -:BOOK & MEMBER
// AND CONTAINS FUNCTIONS FOR WORKING (i.e. ISSUE,RETURN,ETC).
class WORKING : public BOOK, public MEMBER //INHERITING PUBLIC MEMBERS
{
public :
void issue_b(void);
void return_b(void);
void add_b(void);
void add_m(void);
void modify_b(void);
void modify_m(void);
void delete_b(void);
void delete_m(void);
};
// THIS CLASS CONTAINS FUNCTIONS RELATED TO
// -:DATE & FINE (FOR LATE RETURNING OF THE BOOK)
class DATE
{
public :
void ex_date(int,int,int,int);
int diff(int,int,int,int,int,int);
int day, mon, year ;
};
// FUNCTION TO EXTEND GIVEN DATE BY 15 DAYS
void DATE :: ex_date(int d1,int m1,int y1,int days)
{
static int month[]={31,29,31,30,31,30,31,31,30,31,30,31};
for (int i=1;i<=days;i++)
{
d1++;
if((d1>month[m1-1]) || (y1%4!=0 && m1==2 && d1>28))
{
d1=1;
m1++;
}
if (m1>12)
{
m1=1;
y1++;
}
}
day = d1;
mon = m1;
year= y1;
}
// THIS FUNCTION RETURNS THE DIFFERENCE BETWEEN TWO GIVEN DATES
int DATE :: diff(int d1,int m1,int y1,int d2,int m2,int y2)
{
int days=0;
if((y2<y1) || (y2==y1 && m2<m1) || (y2==y1 && m2==m1 && d2<d1))
return days;
static int month[]={ 31,29,31,30,31,30,31,31,30,31,30,31 };
while (d1!=d2 || m1!=m2 || y1!=y2)
{
days++;
d1++;
if((d1>month[m1-1]) || (y1%4!=0 && m1==2 && d1>28))
{
d1=1;
m1++;
}
if(m1>12)
{
m1=1;
y1++;
}
}
return days ;
}
// FUNCTION TO DISPLAY
// -:MAIN MENU
// &
// TO CONTROL ALL THE FUNCTIONS
// IN THE MAIN MENU.
void MENU :: ma_menu(void)
{
char ch;
while (1)
{
clrscr();
gotoxy(29,6);
cout<<"B O O K L I B R A R Y";
gotoxy(29,7);
cout<<"~~~~~~~~~~~~~~~~~~~~~~";
gotoxy(30,10);
cout<<" 1. INTRODUCTION";
gotoxy(30,11);
cout<<" 2. ADD NEW BOOK";
gotoxy(30,12);
cout<<" 3. ADD NEW MEMBER";
gotoxy(30,13);
cout<<" 4. ISSUE BOOK";
gotoxy(30,14);
cout<<" 5. RETURN BOOK";
gotoxy(30,15);
cout<<" 6. LIST OF BOOKS";
gotoxy(30,16);
cout<<" 7. LIST OF MEMBERS";
gotoxy(30,17);
cout<<" 8. EDIT";
gotoxy(30,18);
cout<<" ESC. QUIT";
gotoxy(30,20);
cout<<"ENTER YOUR CHOICE : ";
ch=getche();
if(ch==27)
break;
else
if(ch=='1')
exit;
else
if(ch=='2')
{
WORKING W;
W.add_b();
}
else
if(ch=='3')
{
WORKING W;
W.add_m();
}
else
if(ch=='4')
{
WORKING W;
W.issue_b();
}
else
if(ch=='5')
{
WORKING W;
W.return_b();
}
else
if(ch=='6')
{
BOOK B;
B.table();
}
else
if(ch=='7')
{
MEMBER M;
M.jot();
}
else
if(ch=='8')
ed_menu();
else
if(ch==27)
break;
}
}
// FUNCTION TO DISPLAY EDIT MENU
void MENU :: ed_menu(void)
{
char ch ;
while (1)
{
clrscr() ;
gotoxy(32,9) ;
cout <<"E D I T M E N U";
gotoxy(32,10) ;
cout <<"~~~~~~~~~~~~~~~~" ;
gotoxy(34,13) ;
cout <<"1. BOOKS" ;
gotoxy(34,14) ;
cout <<"2. MEMBERS" ;
gotoxy(34,15) ;
cout <<"0. EXIT" ;
gotoxy(31,17) ;
cout <<"ENTER YOUR CHOICE : " ;
ch = getche() ;
if (ch == 27)
break ;
else
if (ch =='1')
ed_book() ;
else
if (ch == '2')
ed_member() ;
else
if (ch == '0')
break ;
}
}
// FUNCTION TO DISPLAY EDIT MENU FOR BOOK & CONTROL
// ALL THE FUNCTION IN THE EDIT MENU.
void MENU :: ed_book(void)
{
char ch ;
while (1)
{
clrscr() ;
gotoxy(31,9) ;
cout <<"E D I T B O O K S";
gotoxy(31,10) ;
cout <<"~~~~~~~~~~~~~~~~~~" ;
gotoxy(34,13) ;
cout <<"1. MODIFY" ;
gotoxy(34,14) ;
cout <<"2. DELETE" ;
gotoxy(34,15) ;
cout <<"0. EXIT" ;
gotoxy(31,17) ;
cout <<"ENTER YOUR CHOICE : " ;
ch = getche() ;
if (ch == 27)
break ;
else
if (ch == '1')
{
WORKING W ;
W.modify_b() ;
}
else
if (ch == '2')
{
WORKING W ;
W.delete_b() ;
}
else
if (ch == '0')
break ;
}
}
// FUNCTION TO DISPLAY EDIT MENU FOR MEMBERS & CONTROL
// ALL THE FUNCTION IN THE EDIT MENU.
void MENU :: ed_member(void)
{
char ch ;
while (1)
{
clrscr() ;
gotoxy(29,9) ;
cout <<"E D I T M E M B E R S";
gotoxy(29,10) ;
cout <<"~~~~~~~~~~~~~~~~~~~~~~" ;
gotoxy(34,13) ;
cout <<"1. MODIFY" ;
gotoxy(34,14) ;
cout <<"2. DELETE" ;
gotoxy(34,15) ;
cout <<"0. EXIT" ;
gotoxy(29,17) ;
cout <<"ENTER YOUR CHOICE : " ;
ch = getche() ;
if (ch == 27)
break ;
else
if (ch == '1')
{
WORKING W ;
W.modify_m() ;
}
else
if (ch == '2')
{
WORKING W ;
W.delete_m() ;
}
else
if (ch == '0')
break ;
}
}
// THIS FUNCTION RETURN 0 IF GIVEN BOOK CODE NOT FOUND
int BOOK :: b_found(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
int found=0 ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (bookcode == tcode)
{
found = 1 ;
break ;
}
}
file.close() ;
return found ;
}
// THIS FUNCTION RETURN 0 IF GIVEN BOOK NAME NOT FOUND
int BOOK :: b_name_found(char t1code[33])
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
int found=0 ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (!strcmpi(name,t1code))
{
found = 1 ;
break ;
}
}
file.close() ;
return found ;
}
// THIS FUNCTION RETURN RECORD NO. FOR THE BOOK CODE
int BOOK :: recordno(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
int count=0 ;
while (file.read((char *) this, sizeof(BOOK)))
{
count++ ;
if (bookcode == tcode)
break ;
}
file.close() ;
return count ;
}
// THIS FUNCTION RETURNS THE AVAILABLE COPIES FOR THE GIVEN
// BOOK CODE.
int BOOK :: available(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
int tavail=0 ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (bookcode == tcode)
{
tavail = avail ;
break ;
}
}
file.close() ;
return tavail ;
}
// THIS FUNCTION RETURNS THE NO. OF COPIES FOR THE GIVEN
// BOOK CODE.
int BOOK :: no_of_copies(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
int tcopies=0 ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (bookcode == tcode)
{
tcopies = copies ;
break ;
}
}
file.close() ;
return tcopies ;
}
// THIS FUNCTION RETURNS THE BOOK NAME OF THE GIVEN BOOK
// CODE.
char *BOOK :: b_name(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
char tname[33] ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (bookcode == tcode)
{
strcpy(tname,name) ;
break ;
}
}
file.close() ;
return tname ;
}
// THIS FUNCTION RETURNS THE AUTHOR NAME OF THE GIVEN BOOK
// CODE.
char *BOOK :: a_name(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
char tauthor[26] ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (bookcode == tcode)
{
strcpy(tauthor,author) ;
break ;
}
}
file.close() ;
return tauthor ;
}
// THIS FUNCTION RETURNS THE BOOK PRICE OF THE GIVEN BOOK
// CODE.
float BOOK :: b_price(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
float tprice=0.0 ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (bookcode == tcode)
{
tprice = price ;
break ;
}
}
file.close() ;
return tprice ;
}
// THIS FUNCTION RETURNS THE BOOK CODE OF THE GIVEN BOOK
// NAME.
int BOOK :: b_codeof(char t1code[33])
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
int tcode=0 ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (!strcmpi(name,t1code))
{
tcode = bookcode ;
break ;
}
}
file.close() ;
return tcode ;
}
// THIS FUNCTION RETURNS THE NO. OF THE RECORDS IN THE BOOK
// FILE.
int BOOK :: reccount(void)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
int count=0 ;
while (file.read((char *) this, sizeof(BOOK)))
count++ ;
file.close() ;
return count ;
}
// THIS FUNCTION DELETES THE RECORD OF THE GIVEN BOOK CODE.
void BOOK :: delete_rec(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
fstream temp ;
temp.open("temp.dat", ios::out) ;
file.seekg(0,ios::beg) ;
while ( !file.eof() )
{
file.read((char *) this, sizeof(BOOK)) ;
if ( file.eof() )
break ;
if ( bookcode != tcode )
temp.write((char *) this, sizeof(BOOK)) ;
}
file.close() ;
temp.close() ;
file.open("BOOK.DAT", ios::out) ;
temp.open("temp.dat", ios::in) ;
temp.seekg(0,ios::beg) ;
while(!temp.eof())
{
temp.read((char *) this, sizeof(BOOK)) ;
if ( temp.eof() )
break ;
file.write((char *) this, sizeof(BOOK)) ;
}
file.close() ;
temp.close() ;
}
// THIS FUNCTION ADD THE RECORD IN THE BOOK FILE
void BOOK :: add_book(int tcode,char tname[33], char tauthor[26], float tprice, int tcopies, int tavail)
{
fstream file ;
file.open("BOOK.DAT", ios::app) ;
bookcode = tcode ;
strcpy(name,tname) ;
strcpy(author,tauthor) ;
price = tprice ;
copies = tcopies ;
avail = tavail ;
file.write((char *) this, sizeof(BOOK)) ;
file.close() ;
}
// THIS FUNCTION UPDATE THE RECORD IN THE BOOK FILE FOR THE
// GIVEN BOOK CODE
void BOOK :: update_copies(int tcode, int tcopies, int tavail)
{
int recno ;
recno = recordno(tcode) ;
fstream file ;
file.open("BOOK.DAT", ios::out | ios::ate) ;
copies = tcopies ;
avail = tavail ;
int location ;
location = (recno-1) * sizeof(BOOK) ;
file.seekp(location) ;
file.write((char *) this, sizeof(BOOK)) ;
file.close() ;
}
// THIS FUNCTION MODIFY THE RECORD IN THE BOOK FILE FOR THE
// GIVEN BOOK CODE
void BOOK :: change(int tcode, char tname[33], char tauthor[26], float tprice)
{
int recno ;
recno = recordno(tcode) ;
fstream file ;
file.open("BOOK.DAT", ios::out | ios::ate) ;
strcpy(name,tname) ;
strcpy(author,tauthor) ;
price = tprice ;
int location ;
location = (recno-1) * sizeof(BOOK) ;
file.seekp(location) ;
file.write((char *) this, sizeof(BOOK)) ;
file.close() ;
}
// THIS FUNCTION DISPLAY THE RECORD FROM THE BOOK FILE
// FOR THE GIVEN BOOK CODE
void BOOK :: display(int tcode)
{
fstream file ;
file.open("BOOK.DAT", ios::in) ;
file.seekg(0,ios::beg) ;
while (file.read((char *) this, sizeof(BOOK)))
{
if (bookcode == tcode)
{
gotoxy(5,5) ;
cout <<"BOOK CODE : " <<bookcode ;
gotoxy(5,7) ;
cout <<"BOOK NAME : " <<name ;
gotoxy(5,8) ;
cout <<"AUTHOR NAME : " <<author ;
gotoxy(5,9) ;
cout <<"PRICE : Rs." <<price ;
gotoxy(5,10) ;
cout <<"COPIES : " <<price ;
gotoxy(5,11) ;
cout <<"AVAILABLE : " <<avail ;
break ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -