|
//头文件
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
//文件流,全局变量
fstream bookstream;
//类student
class student
{
private:
char name[20];
char student_number[20];
char sex[10];
char education_level[20];
public:
//构造函数
student() {};
student(char * a, char * b,char *c, char * d)
{
strcpy(name,a);
strcpy(student_number,b);
strcpy(sex,c);
strcpy(education_level,d);
}
//成员函数
char * getName()
{
return name;
}
//友元函数-流重载
friend ostream & operator<<(ostream&stream,student s);
friend istream & operator>>(istream&stream,student &s);
};
//重载<<
ostream & operator<<(ostream&stream,student s)
{
stream<<s.name<<" ";
stream<<s.student_number<<" ";
stream<<s.sex<<" ";
stream<<s.education_level<<" ";
return stream;
}
//重载>>
istream & operator>>(istream&stream,student &s)
{
cout<<"Enter name: ";
stream>>s.name;
cout<<"Enter your student_number: ";
stream>>s.student_number;
cout<<"Enter your sex: ";
stream>>s.sex;
cout<<"Enter your education_level: ";
stream>>s.education_level;
return stream;
}
//类book
class book
{
private:
char title[50];
char author[20];
char status[10];
student ownner;
public:
//构造函数
book() {};
book(char * a, char * b, char *c, student d)
{
strcpy(title,a);
strcpy(author,b);
strcpy(status,c);
ownner=d;
}
//成员函数
char * getTitle()
{
return title;
}
char * getStatus()
{
return status;
}
student getOwnner()
{
return ownner;
}
void setStatus(char * a)
{
strcpy(status,a);
}
void setOwnner(student a)
{
ownner=a;
}
//友元函数-流重载
friend ostream & operator<<(ostream&stream,book b);
friend istream & operator>>(istream&stream,book &b);
};
//重载<<
ostream & operator<<(ostream&stream,book b)
{
stream << "<" <<b.title << ">" <<" ";
stream<<b.author<<" ";
stream<<b.status<<" ";
stream<<b.ownner<<"\n";
return stream;
};
//重载>>
istream & operator>>(istream&stream,book &b)
{
cout<<"Enter book title: ";
stream>>b.title;
cout<<"Enter book author: ";
stream>>b.author;
cout<<"Enter book status: ";
stream>>b.status;
cout<<"Enter ownner: ";
stream>>b.ownner;
return stream;
};
//打开文件-尾部
int openfile_tail( )
{
bookstream.open("e:/liuyao/java/bookfile",ios::in|ios:ut|ios::app);
if(!bookstream)
{
cout<<"Cannot open phone book file\n";
return 1;
}
}
//打开文件-头部
int openfile_head( )
{
bookstream.open("e:/liuyao/java/bookfile",ios::in|ios:ut);
if(!bookstream)
{
cout<<"Cannot open phone book file\n";
return 1;
}
}
//关闭文件
void closefile()
{
bookstream.close();
}
//主函数
int main()
{
char c;
book a,b;
student s;
char booktitle[30];
char ownnername[20];
//欢迎界面
cout<<"Welcome to the rental shop!\n";
for( ; ; )
{
//操作选择
do{
cout<<"1.Add new books\n";
cout<<"2.Search books\n";
cout<<"3.Return books\n";
cout<<"4.Borrow books\n";
cout<<"5.Display all the books\n";
cout<<"6.Sum books\n";
cout<<"7.Quit\n";
cout<<"\n\nEnter a choice:";
cin>>c;
}while(c<'1'||c>'6');
bool find=0;
switch(c)
{
//有新书加入-从尾部追加
case '1':
cin>>b;
openfile_tail();
bookstream.write((unsigned char *)&b,sizeof b);
closefile();
break;
//搜索书-从头部搜索
case '2':
cout<<"lease input the book title you want to search: ";
cin>>booktitle;
openfile_head();
while(!bookstream.eof())
{
bookstream.read((unsigned char *)&a,sizeof a);
if( strcmp( booktitle, a.getTitle() )==0 )
{
cout<<a;
find=1;
}
}
if(!find)
cout<<"We have not the book you want to search!\n";
closefile();
break;
//还书
case '3':
cout<<"lease input the book title you want to return ";
cin>>booktitle;
cout<<"lease input your name: ";
cin>>ownnername;
openfile_head();
while((!bookstream.eof()) && (find==0))
{
bookstream.read((unsigned char *)&a,sizeof a);
if( (strcmp( booktitle, a.getTitle() )==0) && (strcmp( ownnername, a.getOwnner().getName() )==0 ))
{
a.setStatus("in");
student st("none","none","none","none");
a.setOwnner(st);
find=1;
cout<<"You have returned the book successfully!\n\n";
}
}
bookstream.seekp(- (sizeof a), ios::cur);
bookstream.write((unsigned char *)&a,sizeof a);
closefile();
break;
//借书
case '4':
cout<<"Enter the book title you want to borrow: ";
cin>>booktitle;
openfile_head();
while((!bookstream.eof())&&(find==0))
{
bookstream.read((unsigned char *)&a,sizeof a);
if( (strcmp( booktitle, a.getTitle() )==0) && (strcmp( "in", a.getStatus() )==0 ))
{
a.setStatus("out");
cin>>s;
a.setOwnner(s);
find=1;
cout<<"You have borrowed the book successfully!\n\n";
}
}
bookstream.seekp(- (sizeof a), ios::cur);
bookstream.write((unsigned char *)&a,sizeof a);
closefile();
break;
//现实书目
case '5':
openfile_head();
while(!bookstream.eof())
{
bookstream.read((unsigned char *)&a,sizeof a);
cout<<a;
}
cout<<"\n";
closefile();
break;
//统计
case '6':
int totalbook,bookout,bookin;
totalbook=0;
bookout=0;
bookin=0;
openfile_head();
while(!bookstream.eof())
{
totalbook++;
bookstream.read((unsigned char *)&a,sizeof a);
if( strcmp( "out", a.getStatus() )==0 )
bookout++;
else
bookin++;
}
cout<<"Our shop has "<<totalbook<<" books totally\n";
cout<<bookout<<" books are on borrowed\n";
cout<<bookin<< " book are in the shop\n\n";
closefile();
break;
//退出
case '7':
return 0;
}
}
} |
|