#include<iostream.h> #include<fstream.h> #include<string.h> class BC //图书卡类 { public: void print() { cout<<"BookCardN"<<no<<endl <<"Name:"<<name<<endl <<"sex:"<<(sex=='1'?"man":"women")<<endl; } char *GetNo() { return no; } void Register() { cout<<"BookCardN"; cin>>no; cout<<"user name:"; cin>>name; cout<<"sex(input 0 or 1,1:man,0:women):"; cin>>sex; } friend ostream &operator <<(ostream &stream,BC &card) { stream.write(card.no,sizeof(card.no)); stream.write(card.name,sizeof(card.name)); stream.put(card.sex); return stream; } friend istream &operator >>(istream &stream,BC &card) { stream.read(card.no,sizeof(card.no)); stream.read(card.name,sizeof(card.name)); stream.get(card.sex); return stream; } private: char no[12]; char name[15]; char sex; }; class Book//借书证类 { public: void print() { cout<<"Book Number:"<<no<<endl <<"Book Name:"<<name<<endl <<"Borrowed:"<<(borrowed=='1'?"yes":"no")<<endl; } char *GetName() { return name; } char *GetNo() { return no; } void Register() { cout<<"BookN"; cin>>no; cout<<"BookName:"; cin>>name; borrowed='0'; } char GetBorrowed() { return borrowed; } void SetBorrowed(char b) { borrowed=b; } friend ostream &operator <<(ostream &stream,Book &book) { stream.write(book.no,sizeof(book.no)); stream.write(book.name,sizeof(book.name)); stream.put(book.borrowed); return stream; } friend istream &operator >>(istream &stream,Book &book) { stream.read(book.no,sizeof(book.no)); stream.read(book.name,sizeof(book.name)); stream.get(book.borrowed); return stream; } private: char no[12]; char name[15]; char borrowed; }; class Borrowhofel:public Book,public BC//借书记录类 { public: char *GetCardNo() { return BookCardNo; } char *GetBookNo() { return BookNo; } Borrowhofel(){} Borrowhofel(char bc[12],char b[15]) { strcpy(BookCardNo,bc); strcpy(BookNo,b); } friend ofstream &operator <<(ofstream &stream,Borrowhofel &hofel) { stream.write(hofel.BookCardNo,sizeof(hofel.BookCardNo)); stream.write(hofel.BookNo,sizeof(hofel.BookNo)); return stream; } friend ifstream &operator >>(ifstream &stream,Borrowhofel &hofel) { stream.read(hofel.BookCardNo,sizeof(hofel.BookCardNo)); stream.read(hofel.BookNo,sizeof(hofel.BookNo)); return stream; } private: char BookCardNo[12]; char BookNo[15]; }; void enter_BC() { BC card; ofstream outfile; outfile.open("BC.txt",ios::app); char we='y'; while(we=='y') { card.Register(); outfile<<card; cout<<"Are you continue?(y\\n)"; cin>>we; } outfile.close(); } void enter_book() { Book book; ofstream outfile; outfile.open("book.txt",ios::app); char we='y'; while(we=='y') { book.Register(); outfile<<book; cout<<"Are you continue?(y\\n)"; cin>>we; } outfile.close(); } void borrow_book() { Borrowhofel hofel; BC card; Book book; char cardno[12]; cout<<"Input your BookCardN"; cin>>cardno; ifstream infile; infile.open("BC.txt",ios::in); int flag=0; infile>>card; while(!infile.eof()) { if(strcmp(card.GetNo(),cardno)==0) { flag=1; break; } infile>>card; } infile.close(); if(flag==0) { cout<<"The BookCardNo not exist!\n"; return; } char we='y'; char bookno[10]; while(we=='y') { cout<<"Input the bookno you want borrow:"; cin>>bookno; infile.open("book.txt",ios::in); flag=0; infile>>book; while(!infile.eof()) { if(strcmp(book.GetNo(),bookno)==0) { if(book.GetBorrowed()=='0') { flag=1; break; } } infile>>book; } int pos=infile.tellg(); infile.close(); if(flag==0) { cout<<"The book has borrowed or not exist!\n"; continue; } book.SetBorrowed('1'); ofstream outfile; outfile.open("book.txt",ios::ate); outfile.seekp(pos-21); outfile<<book; outfile.close(); outfile.open("borrow_hofel.txt",ios::app); hofel=Borrowhofel(cardno,bookno); outfile<<hofel; outfile.close(); cout<<"Do you continue?(y\\n)"; cin>>we; } } void return_book() { Borrowhofel hofel; BC card; Book book; char cardno[12]; cout<<"Input your BookCardN"; cin>>cardno; ifstream infile; infile.open("BC.txt",ios::in); int flag=0; infile>>card; while(!infile.eof()) { if(strcmp(card.GetNo(),cardno)==0) { flag=1; break; } infile>>card; } infile.close(); if(flag==0) { cout<<"The BookCardNo not exist!\n"; return; } char we='y'; char bookno[15]; while(we=='y') { cout<<"Input the bookno you want return:"; cin>>bookno; infile.open("book.txt",ios::in); flag=0; infile>>book; while(!infile.eof()) { if(strcmp(book.GetNo(),bookno)==0 &&book.GetBorrowed()=='1') { flag=1; break; } infile>>book; } int pos=infile.tellg(); infile.close(); if(flag==0) { cout<<"The book has not borrowed or not exist!\n"; continue; } book.SetBorrowed('0'); ofstream outfile; outfile.open("book.txt",ios::ate); outfile.seekp(pos-21); outfile<<book; outfile.close();flag=0; infile.open("borrow_hofel.txt",ios::in); outfile.open("tempt.txt",ios::out); infile>>hofel; while(!infile.eof()) { if(strcmp(hofel.GetCardNo(),cardno)|| strcmp(hofel.GetBookNo(),bookno)) outfile<<hofel; infile>>hofel; } infile.close(); outfile.close(); infile.open("tempt.txt",ios::in); outfile.open("borrow_hofel.txt",ios::out); infile>>hofel; while(!infile.eof()) { outfile<<hofel; infile>>hofel; } infile.close(); outfile.close(); cout<<"Do you continue?(y\\n)"; cin>>we; } } void inquire_book() { Book book; char we='y'; ifstream infile; char bookno[10]; while(we=='y') { infile.open("book.txt",ios::in); cout<<"input BookN"; cin>>bookno; infile>>book; while(strcmp(book.GetNo(),bookno)&&!infile.eof()) { infile>>book; } if(infile.eof()) cout<<"the book not exist!\n"; else book.print(); infile.close(); cout<<"Are you continue?(y\\n)"; cin>>we; } } void inquire_BC() { BC card; char we='y'; ifstream infile; char cardno[10]; while(we=='y') { infile.open("BC.txt",ios::in); cout<<"input BookCardN"; cin>>cardno; infile>>card; while(strcmp(card.GetNo(),cardno)&&!infile.eof()) { infile>>card; } if(infile.eof()) cout<<"the BookCard not exist!\n"; else card.print(); infile.close(); cout<<"Are you continue?(y\\n)"; cin>>we; } }
void main() { char a; cout<<"************************************\n" <<"* 1.to inquire card. *\n" <<"* 2.to borrow book. *\n" <<"* 3.to return book. *\n" <<"* 4.to inquire book. *\n" <<"* 5.to enter new book. *\n" <<"* 6.to enter new card. *\n" <<"* 7.to quit the system. *\n" <<"************************************\n"; cout<<"Select the NO!"<<endl; cin>>a; int flag=1; while(flag) { switch(a) { case '1':inquire_BC();break; case '2':borrow_book();break; case '3':return_book();break; case '4':inquire_book();break; case '5':enter_book();break; case '6':enter_BC();break; case '7':flag=0;continue; } cout<<"Select the NO!\n"; cin>>a; } }