图书管理系统----202005
写个笔记记录一下。系统比较简陋,也许还会有一些我没测出来的bug。
#题目:
#系统使用须知:
- 必须满足以下要求:
(1)在C盘新建“图书管理系统”文件夹
(2)在“图书管理系统”文件夹内新建以下文件:
administrator_list.txt
book_list.txt
reader_list.txt
User_list.txt
(3)在“图书管理系统”文件夹内新建以下文件夹:
日志文件log
(4)在“日志文件log”文件夹内新建以下文件:
图书借还日志.txt
(5)使用系统前可以预设书库和用户,也可以在使用系统时注册用户、管理员,注册图书管理员后可以向书库添加图书;如果需要预设书库和用户,则应在txt文件administrator_list、book_list、reader_list、User_list里严格按以下格式输入预设信息:
【User_list】格式:
10001-------------------【1用户码,系统生成】
Reader1----------------【2用户名,不超过10个字符】
1--------------------------【3用户类型1=reader-student,2=reader-teacher,11=book_administrator,12=system_administrator】
1234567890-----------【4密码,十个字符,由数字、字母组成,区分大小写】
*--------------------------【5用户信息结尾】
【reader_list】格式:
10001------------------【1用户码,系统生成】
Reader1---------------【2用户名,不超过10个字符】
1-------------------------【3用户类型,1=reader-student,2=reader-teacher】
1234567890----------【4密码,十个字符,由数字、字母组成,区分大小写】
2-------------------------【5借阅总数】
List----------------------【(可没有)*6借阅清单】
0-------------------------【(可没有)*7借阅编号】
English book----------【*8书名】
1116---------------------【(可没有)*9借出时间,月份+日期,*默认2月28天】
1--------------------------【(可没有)*10借阅编号】
Chinese book---------【(可没有)*11书名】
1220---------------------【(可没有)*12借出时间,月份+日期,*默认2月28天】
*---------------------------【13用户信息结尾】
【administrator_list】格式:
10004-------------------【1用户码,系统生成】
Maria--------------------【2用户名,不超过10个字符】
11------------------------【3用户类型11=book_administrator,12=system_administrator】
1234567891-----------【4密码,十个字符,由数字、字母组成,区分大小写】
*--------------------------【5用户信息结尾】
【book_list】格式:
Chinese book---------【1书名】
出版社1-----------------【2作者名】
0--------------------------【3借阅情况,0=借出(不可借),1=在馆(可借)】
*--------------------------【4书籍信息结尾】
- 关于系统内用户的分类
- 关于用户的权限
读者——借书(根据细分类别有不同的借书期限和借书上限)
管理员—— - 由于水平有限,目前存在一个bug:如果是一位有借书记录的读者修改了用户类型(e.g.学生读者→教师读者/系统管理员/图书管理员),那么其借书记录将丢失。
- 系统运行时,某些时候错误地输入字母而不是数字会导致程序异常,请注意。
- 实现的一些附加功能:
(1)用户登录时输错三次密码后,将需要重新选择用户登录;
(2)用户注册时需二次输入密码确认;
(3)系统运行结束后会保存运行时修改的所有信息至对应的txt文件中,如文件未被覆盖,则下次运行系统时,可以继续使用上次修改后的信息(即用户注册等信息将保留,下次可以进行使用已注册的用户登录等操作)。
#原代码
/*模拟一个图书管理系统应用程序,支持系统用户的图书借阅、图书管理、用户管理等功能。
图书借阅管理主要包括 1)图书借阅、2)图书归还、3)借阅信息查看等功能。
图书管理主要包括1)图书的增加、删除、修改、查看、统计等功能。
用户管理主要包括1)用户注册、2)登录、3)修改密码、4)修改个人信息、5)设置用户类型等功能。
具体要求:
1、定义图书管理系统中的Book书目类(还可以定义User用户类、Log图书借还日志类等)。
2、合理应用类的继承性进行馆藏资源的继承性定义,可分为书、碟片、电子资源等(或将系统用户分为读者、图书管理员、系统管理员)。
3、根据不同类型用户,登录系统显示不同的用户功能菜单,实现不同的操作处理,如学生与教师所借阅书的数量和天数均不相同(或考虑图书馆中多种馆藏资源支持的用户操作有所区别)。
4、增加异常处理,在借阅图书时,已到所能借阅图书数量的最大数量给予提示;归还图书时,图书已超期需要缴纳罚款。
5、通过重载运算符"<<"和">>"方便图书信息、读者信息录入等操作,并实现将馆藏图书信息、借阅记录等保存到磁盘,形成图书借还日志文件。
6、扩展实验:可尝试使用MFC可视化界面提高用户操作方便性和友好性。
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
// 全局变量初始化
int booksum;
int user_codetail;
// 类定义
/*---------------------Book------------------------------------*/
class Book
{
public:
string Book_name;
string Book_writer;
int Book_condition;
Book(){}
friend ostream& operator<<(ostream &out, Book& a)
{
out<<a.Book_name<<"----"<<a.Book_writer<<endl;
return out;
}
friend istream& operator>>(istream &in, Book& a)//add new book
{
cout<<"新增书籍名称: ";
getchar();
getline(cin,a.Book_name);
cout<<"作者: ";
getline(cin,a.Book_writer);
a.Book_condition = 1;
return in;
}
};
/*---------------------User------------------------------------*/
class User
{
public:
int User_code;
string User_name;
string User_key;
int User_type;
/* 1=reader-student
2=reader-teacher
11=book_administrator
12=system_administrator */
User(){}
void adduser(int codetail, string name, int type, string key)
{
ofstream outfile;
User_code = codetail;
User_name = name;
User_type = type;
User_key = key;
}
/*friend istream& operator>>(istream &in, User& b)
//*使用于向日志文件写入记录
{
cin>>b.User_code;
cin>>b.User_name;
cin>>b.User_key;
return in;
}*/
};
class reader: public User
{
public:
int reader_keepdays_limit;
int reader_booksum_limit;
int book_keeping;
string booklist[6];
int borrowdate[6];
reader(){book_keeping = 0;}
void limit_set(int d, int b)
{
reader_keepdays_limit = d;
reader_booksum_limit = b;
}
friend void book_borrow(int type, int code);
friend void book_return(int type, int code);
};
class student: public reader
{
public:
student()
{
limit_set(30, 3);
}
};
class teacher: public reader
{
public:
teacher()
{
limit_set(60, 6);
}
};
class book_administrator: public User
{
public:
book_administrator(){}
};
class system_administrator: public User
{
public:
system_administrator(){}
};
/*-----------------------------------------------------------*/
//-------------------------------------------------------------------MAIN PART.
// 函数载入
/*User*/int loaduser();
/*Book*/void loadbook();
/*Book*/void book_borrow(int type, int code);
/*Book*/void book_return(int type, int code);
/*User*/int Register(int codetail);
/*User*/int Sign_in();
/*User*/void modify_user(int step);
int menu(int type, int code);
/*Book*/int keepingtime(int bdate, int today);
/*Book*/int searchbook(string name);
/*User*/void writelist(int User_code, string User_name, int User_type, string User_key);
/*Book*/void addbook();
/*Book*/void deletebook();
/*Book*/void modifybook();
/*Book*/void book_list_record();
/*User*/void rewrite_User_list();
void log(int event, int book_code, int date, int reader_code);
//---------
// 全局变量数组载入
User user[110];
student stu[25];
teacher tea[25];
book_administrator badmin[25];
system_administrator sadmin[25];
Book book[100];
//-----------
// main函数
int main()
{
cout<<"加载中......"<<endl;
int type,n,codetail,i,step=1;
int online=0;
codetail = loaduser() + 1 ;
user_codetail = codetail;
//cout<<"user_codetail = "<<user_codetail<<endl;//text
loadbook();
cout<<"--------------图书管理模拟系统加载完毕--------------"<<endl<<endl;
for(;online==0;)
{ cout<<"--------菜单--------"<<endl;
cout<<" 1 = 登录"<<endl;
cout<<" 2 = 注册"<<endl;
cout<<" -1 = 退出系统"<<endl;
cin>>i;
switch(i)//online=User_code
{
case 1:
{ online = Sign_in(); break;}
case 2:
{ online = Register(codetail);
codetail++;
user_codetail = codetail;
break;}
case (-1):
{ cout<<"退出系统中..."<<endl;
online = -1;
break;}
default: { cout<<"错误输入!"<<endl; break; }
}
type = user[online-10000].User_type;
for(; online>0 ;)
{ step = menu(type,online);
if(step < 0) online = 0;//cin>>step;
}
//cout<<"(in main) user_codetail = "<<user_codetail<<endl;//text
}
rewrite_User_list();
book_list_record();
system("pause");
return 0;
}
// 自定义函数
/*User*/int loaduser()//读取文件里的用户记录
{
int codetail;
ifstream infile;
int i=1;
string step;
//Part 1
infile.open("C:\\图书管理系统\\User_list.txt",ios::in);
for(; !infile.eof() ; i++)
{
infile>>user[i].User_code;
infile.get();
infile>>user[i].User_name;
infile>>user[i].User_type;
infile.get();
infile>>user[i].User_key;
infile.get();
infile>>step;
switch(user[i].User_type)
{
case 1:{
stu[i].User_code = user[i].User_code;
stu[i].User_name = user[i].User_name;
stu[i].User_type = user[i].User_type;
stu[i].User_key = user[i].User_key;
break;
}
case 2:{
tea[i].User_code = user[i].User_code;
tea[i].User_name = user[i].User_name;
tea[i].User_type = user[i].User_type;
tea[i].User_key = user[i].User_key;
break;
}
case 11:{
badmin[i].User_code = user[i].User_code;
badmin[i].User_name = user[i].User_name;
badmin[i].User_type = user[i].User_type;
badmin[i].User_key = user[i].User_key;
break;
}
case 12:{
sadmin[i].User_code = user[i].User_code;
sadmin[i].User_name = user[i].User_name;
sadmin[i].User_type = user[i].User_type;
sadmin[i].User_key = user[i].User_key;
break;
}
}
codetail = user[i].User_code;
}
infile.close();
//Part 2
infile.open("C:\\图书管理系统\\reader_list.txt",ios::in);
int code;
string name;
int type;
string key;
int bookkeeping;
int n=-1;
for(; !infile.eof() ; )
{
infile>>code; infile>>name; infile>>type; infile>>key;
infile>>bookkeeping;
if(type==1) stu[code-10000].book_keeping = bookkeeping;
else if(type==2) tea[code-10000].book_keeping = bookkeeping;
infile>>step;
int logic = step.compare("List");
if(logic == 0 )//t = key.compare(check);
{
char i;
infile>>i;
for(; i != '*' ; )
{
n = i - '0';
infile.get();
if(type==1)//学生读者
{
getline(infile,stu[code-10000].booklist[n]);
infile>>stu[code-10000].borrowdate[n];
}
else if(type==2)//教师读者
{
getline(infile,tea[code-10000].booklist[n]);
infile>>tea[code-10000].borrowdate[n];
}
infile>>i;
}
}
}
cout<<"成功加载用户目录!"<<endl;
//text
/*{ cout<<"tea[3].User_code"<<tea[3].User_code<<endl;
cout<<"tea[3].User_name"<<tea[3].User_name<<endl;
cout<<"tea[3].User_type"<<tea[3].User_type<<endl;}*/
return codetail;
}
/*Book*/void loadbook()//读取书目
{
ifstream infile;
int i;
char star;
infile.open("C:\\图书管理系统\\book_list.txt",ios::in);
for( i = 0 ; ( !infile.eof() )&& i<100 ; i++)
{ getline(infile,book[i].Book_name);
getline(infile,book[i].Book_writer);
infile>>book[i].Book_condition;
infile>>star; infile.get();
//cout<<endl<<"i = "<<i<<endl;//text
}
booksum = i;
infile.close();
cout<<"成功加载书库目录!"<<endl;
//cout<<"booksum = "<<booksum<<endl<<endl;//text
}
/***************************************************************/
int menu(int type, int code)//菜单显示
{//menu函数返回值为-1时用户退出
int step;
if(type<10)//读者菜单
{ cout<<"# 读者";
cout<<"菜单:"<<endl<<"1 借书"<<endl<<"2 还书"<<endl<<"3 查询用户信息"<<endl;
cout<<"0 登出"<<endl;
cin>>step;
if(step==1) book_borrow(type, code);
else if(step==2) book_return(type, code);
else if(step==3)
{ if (type==1)
{ cout<<stu[code-10000].User_code<<" "<<stu[code-10000].User_name<<endl;
//输出当前借阅书单
cout<<"借阅书单:"<<endl;
for(int p=0 ; p<stu[code-10000].book_keeping ; p++)
{ cout<<p<<"-----"<<stu[code-10000].booklist[p];
cout<<"-----"<<stu[code-10000].borrowdate[p]<<endl;
}
}
else if(type==2)
{ cout<<tea[code-10000].User_code<<" "<<tea[code-10000].User_name<<endl;
//输出当前借阅书单及借书日期
cout<<"借阅书单:"<<endl;
for(int p=0 ; p<tea[code-10000].book_keeping ; p++)
{ cout<<p<<"-----"<<tea[code-10000].booklist[p];
cout<<"-----"<<tea[code-10000].borrowdate[p]<<endl;
}
}
}
else if(step==0){ return -1;}
else { cout<<"错误输入!"<<endl;}
}
else if(type==11)//图书管理员菜单
{ cout<<"# 图书管理员";
cout<<"菜单:"<<endl<<"1 增加书籍"<<endl<<"2 删除书籍"<<endl;
cout<<"3 修改书籍信息"<<endl<<"4 查询书籍信息"<<endl;
cout<<"0 登出"<<endl;
cin>>step;
if(step==1) { addbook(); } //增加图书
else if(step==2) { deletebook();} //删除图书(将书的状态设置为不可借阅)
else if(step==3) { modifybook();} //修改书籍信息
else if(step==4) //打印所有图书信息
{ for(int i=0; i<booksum;i++)
{ cout<<" ["<<book[i].Book_name<<"]----------"<<book[i].Book_writer;
cout<<" 状态:";
if(book[i].Book_condition==0) cout<<"借出"<<endl;
else if(book[i].Book_condition==-1) cout<<"删除"<<endl;
else if(book[i].Book_condition==1) cout<<"在馆"<<endl;
}
cout<<" 书籍总数: "<<booksum<<"。"<<endl;
}
else if(step==0){ return -1;}
else { cout<<"错误输入!"<<endl;}
}
else if(type==12)//系统管理员菜单
{ cout<<"# 系统管理员";
cout<<"菜单:"<<endl<<"1 修改用户密码"<<endl<<"2 修改用户名称"<<endl<<"3 修改用户类型"<<endl;
cout<<"0 登出"<<endl;
cin>>step;
if(step == 0) { return -1;}
else if(step>=1 && step<=3) { modify_user(step); }
else { cout<<"错误输入!"<<endl;}
}
cout<<"任意数字键返回菜单"<<endl;
cin>>step;
return 1;
}
/*User*/int Register(int codetail)//用户注册
{
string name;
string key;
int t, type;
for(t=0;t==0;)//设置名字
{ cout<<"设置名字(请不要超过十个字符 不要包含空格 *一个汉字将占用两个字符):"<<endl;
cin>>name;
t = name.length();
if(t<=10) break;
else
{ t=0;
cout<<"格式不符,请重新";
}
}
for(t=-1;t<0;)//设置用户类型
{ cout<<"设置用户类型:"<<endl;
cout<<"1=读者 0=管理员"<<endl;
cin>>t;
if(t==1)
{ cout<<"1=学生 2=教师"<<endl;
cin>>t;
if(t==1) {type = 1;} //用户类型设置为学生读者
else if(t==2) {type = 2;} //用户类型设置为教师读者
else { t=-1;}
}
else if(t==0)
{ cout<<"1=图书管理员 2=系统管理员"<<endl;
cin>>t;
if(t==1) {type = 11;} //用户类型设置为图书管理员
else if(t==2) {type = 12;} //用户类型设置为系统管理员
else { t=-1;}
}
else t=-1;
if(t<0)
cout<<"错误输入!"<<endl;
}
for(t=0;t==0;)//设置密码
{ cout<<"设置密码(十位,可由数字、字母组成,注意区分大小写):"<<endl;
cin>>key;
t = key.length();
if(t==10) break;
else
{ t=0;
cout<<"请重新";
}
}
string check;
cout<<"再次";
for(t=0; ; )//确认密码
{ cout<<"输入密码确认"<<endl;
cin>>check;
t = key.compare(check);
if(t==0) break;
else cout<<"请重新";
}
cout<<"请等待......"<<endl;
switch(type)//建立用户
{
case 1:
{ stu[codetail-10000].adduser(codetail, name, type, key);
break;
}
case 2:
{ tea[codetail-10000].adduser(codetail, name, type, key);
break;
}
case 11:
{ badmin[codetail-10000].adduser(codetail, name, type, key);
break;
}
case 12:
{ sadmin[codetail-10000].adduser(codetail, name, type, key);
break;
}
}
user[codetail-10000].adduser(codetail, name, type, key);
writelist(codetail, name, type, key);
cout<<endl<<" 已注册,您的用户码为:"<<user[codetail-10000].User_code<<endl;
cout<<endl<<"【 用户["<<user[codetail-10000].User_name<<"]登录 】"<<endl;
return user[codetail-10000].User_code;
}
/*User*/void writelist(int User_code, string User_name, int User_type, string User_key)//写入新注册用户
{
// 向文件User_list写入新用户数据
ofstream outfile;
outfile.open("C:\\图书管理系统\\User_list.txt",ios::app);
cout << "写入用户目录" << endl;
outfile <<endl;
outfile << User_code <<endl;
outfile << User_name <<endl;
outfile << User_type <<endl;
outfile << User_key <<endl;
outfile << "*" ;
// 关闭打开的文件
outfile.close();
if(User_type<10)
{// 向文件reader_list写入新用户数据
outfile.open("C:\\图书管理系统\\reader_list.txt",ios::app);
cout << "写入读者目录" << endl;
outfile <<endl;
outfile << User_code <<endl;
outfile << User_name <<endl;
outfile << User_type <<endl;
outfile << User_key <<endl;
outfile << "0" <<endl;
outfile << "*" ;
// 关闭打开的文件
outfile.close();
}
else if(User_type>10)
{// 向文件administrator_list写入新用户数据
outfile.open("C:\\图书管理系统\\administrator_list.txt",ios::app);
cout << "写入管理员目录" << endl;
outfile <<endl;
outfile << User_code <<endl;
outfile << User_name <<endl;
outfile << User_type <<endl;
outfile << User_key <<endl;
outfile << "*" ;
// 关闭打开的文件
outfile.close();
}
}
/*User*/int Sign_in()//用户登录
{
string name;
string key;
int code,i,errortimes=0;
int t=0;
int quit=0;
cout<<"输入用户码:"<<endl;
cin>>code;
if(code<=10000 || code >= user_codetail)
{ cout<<"用户码错误"<<endl;
return 0; }
string k;
for(i=0;t==0&&i<100;i++)
{
if(user[i].User_code == code)
{ k = user[i].User_key;
t=1; //cout<<"Find!"<<endl;//text
}
else { }
}
i--;
for(;quit==0 && errortimes<3;)
{ cout<<"输入十位密码(注意区分大小写):"<<endl;
//cout<<"[退出=0]";
cin>>key;
t = k.compare(key);
if(t==0) break;
else if(errortimes<3) cout<<"请重新";
errortimes++;
}
if(t==0)
{ cout<<endl<<"【 用户["<<user[i].User_name<<"]登录 】"<<endl;
if(user[i].User_type == 1) cout<<"进入学生读者操作界面"<<endl;
else if(user[i].User_type == 2) cout<<"进入教师读者操作界面"<<endl;
else if(user[i].User_type == 11) cout<<"进入图书管理员操作界面"<<endl;
else if(user[i].User_type == 12) cout<<"进入系统管理员操作界面"<<endl;
cout<<endl;
}
else if(errortimes==3)
{ cout<<"已输错3次密码,请重新选择用户登录"<<endl;
return 0; }
return user[i].User_code;
}
/*User*/void modify_user(int step)//修改用户信息
{
int modify_user_code;
int modify_user;
cout<<"输入需要修改信息的用户的用户码:";
cin>>modify_user_code;
for( ; modify_user_code<=10000 || modify_user_code >= user_codetail ; )
{ cout<<"用户码错误"<<endl;
cout<<"重新输入需要修改信息的用户的用户码:";
cin>>modify_user_code;
}
int i,find=0;
int t;
for( i=0 ; find==0 && i<=user_codetail ; i++ )
{ if(user[i].User_code == modify_user_code)
{ modify_user = i;
find=1; //cout<<"Find!"<<endl;//text
}
}
cout<<"用户 "<<user[modify_user].User_code<<" ["<<user[modify_user].User_name<<"] 类型:"<<user[modify_user].User_type;
cout<<" 密码:"<<user[modify_user].User_key<<endl;
//------1 Modify user's password------2 Modify user name------3 Modify user's type
switch(step)
{
case 1:
{ string key;
for(t=0;t==0;)//设置密码
{ cout<<"设置新密码(十位,可由数字、字母组成,注意区分大小写):"<<endl;
cin>>key;
t = key.length();
if(t==10) break;
else
{ t=0;
cout<<"请重新";
}
}
string check;
cout<<"再次";
for(t=0; ; )//确认密码
{ cout<<"输入密码以确认"<<endl;
cin>>check;
t = key.compare(check);
if(t==0) break;
else cout<<"请重新";
}
cout<<"Loading......"<<endl;
user[modify_user].User_key = key;
break;
}
case 2:
{ string name;
for(t=0;t==0;)//设置名字
{ cout<<"修改名字为(请不要超过十个字符 不要包含空格 *一个汉字将占用两个字符):"<<endl;
cin>>name;
t = name.length();
if(t<=10) break;
else { t=0;
cout<<"格式不符,请重新操作"<<endl;}
}
cout<<"加载中......"<<endl;
user[modify_user].User_name = name;
break;
}
case 3:
{ int type;
cout<<"用户类型:"<<endl<<" 1=学生读者"<<endl;
cout<<" 2=教师读者"<<endl;
cout<<" 11=图书管理员"<<endl;
cout<<" 12=系统管理员"<<endl;
for( type=-1 ; type<0 ;)//设置用户类型
{ cout<<"设置用户类型为:"<<endl;
cin>>type;
if(type==1 || type==2 || type==11 || type==12 ){}
else
{ type=-1;
cout<<"错误输入!"<<endl;
cout<<"重新";
}
}
cout<<"加载中......"<<endl;
user[modify_user].User_type = type;
break;
}
}
cout<<"------用户信息更新------ "<<endl<<user[modify_user].User_code<<endl<<" ["<<user[modify_user].User_name<<"] "<<endl;
cout<<"用户类型:"<<user[modify_user].User_type<<endl;
cout<<"密码:"<<user[modify_user].User_key<<endl;
}
/*Book*/void book_borrow(int type, int code)//借书
{
int warning=1;
string bookname;
int today;
int booknum = -1;
cout<<"今日日期(月份、日期):(e.g. 0920)"<<endl;
for(int warning=1;warning==1;)
{ cin>>today;
if(today>1231||today<101 || (today>131&&today<201) || (today>228&&today<301) || (today>331&&today<401) || (today>430&&today<501) || (today>531&&today<601) || (today>630&&today<701) || (today>731&&today<801) || (today>831&&today<901) || (today>930&&today<1001) || (today>1031&&today<1101) || (today>1130&&today<1201))
{ warning = 1;
cout<<"错误日期!"<<endl;
cout<<"请重新输入今日日期(月份、日期):(e.g. 0920)"<<endl;
}
else warning = 0;
}
int date;
for(; warning==1 ;)
{
cout<<"请输入书名:";
cin>>bookname;
booknum = searchbook(bookname);
if(booknum>=0 && book[booknum].Book_condition>0)
{ try
{ switch(type)
{
case 1:
{ stu[code-10000].book_keeping+=1;
if( stu[code-10000].book_keeping > stu[code-10000].reader_booksum_limit )
{ stu[code-10000].book_keeping-=1;
throw "已到所能借阅图书数量的最大数量!";
}
else
{
stu[code-10000].booklist[stu[code-10000].book_keeping-1] = bookname;
stu[code-10000].borrowdate[stu[code-10000].book_keeping-1] = today;
}
break;
}
case 2:
{ tea[code-10000].book_keeping+=1;
if( tea[code-10000].book_keeping > tea[code-10000].reader_booksum_limit )
{ tea[code-10000].book_keeping-=1;
throw "已到所能借阅图书数量的最大数量!";
}
else
{ tea[code-10000].booklist[tea[code-10000].book_keeping-1] = bookname;
tea[code-10000].borrowdate[tea[code-10000].book_keeping-1] = today;
}
break;
}
}
cout<<"已借阅["<<book[booknum].Book_name<<"]"<<endl;
/*书籍借出*/log(1, booknum, today, code-10000);
}catch (const char* msg)
{ cerr << msg << endl;
warning = 0;
}
book[booknum].Book_condition = 0;
}
else if(booknum>=0 && book[booknum].Book_condition==0)
cout<<"["<<book[booknum].Book_name <<"] 此书无库存"<<endl;
else cout<<"查无此书,无法借阅。"<<endl;
cout<<"1=继续借书 任意键=退出"<<endl;
cin>>warning;
}
}
/*Book*/void book_return(int type, int code)//还书
{
int i;
int r;
int today;
string bname;
cout<<"今日日期(月份、日期):(e.g. 0920)"<<endl;
for(int warning=1;warning==1;)
{ cin>>today;
if(today>1231||today<101 || (today>131&&today<201) || (today>228&&today<301) || (today>331&&today<401) || (today>430&&today<501) || (today>531&&today<601) || (today>630&&today<701) || (today>731&&today<801) || (today>831&&today<901) || (today>930&&today<1001) || (today>1031&&today<1101) || (today>1130&&today<1201))
{ warning = 1;
cout<<"错误日期!"<<endl;
cout<<"请重新输入今日日期(月份、日期):(e.g. 0920)"<<endl;
}
else warning = 0;
}
int step=1;
if(type==1 &&stu[code-10000].book_keeping==0)
{ step = 0; }
else if(type==2 &&tea[code-10000].book_keeping==0)
{ step = 0; }
if(step==0) cout<<"未借阅书籍"<<endl;
else
{ for( ; step==1; )
{ switch(type)
{
case 1:
{
for(i=0;i<stu[code-10000].book_keeping;i++)
//输出当前借阅书单
{ cout<<i<<"-----"<<stu[code-10000].booklist[i]<<endl;}
cout<<"输入书前序号选择归还"<<endl;
cin>>r;
bname = stu[code-10000].booklist[r];
//检查书籍是否超期
try
{ int time;
time = keepingtime(stu[code-10000].borrowdate[r], today);
if(time>stu[code-10000].reader_keepdays_limit)
{
cout<<"允许借阅天数:"<<stu[code-10000].reader_keepdays_limit<<endl;
cout<<"超期!超出天数:"<<time - stu[code-10000].reader_keepdays_limit;
throw (time - stu[code-10000].reader_keepdays_limit);
}
else cout<<"已归还 ["<< stu[code-10000].booklist[r] <<"]"<<endl;
}catch(int fineday){
double fine;
fine = fineday*0.5;
cout<<" 需缴纳 "<<fine<<" 元罚款"<<endl;
}
for(i=r; i<stu[code-10000].book_keeping ; i++ )
//还书后重新排列借阅书单
{
stu[code-10000].booklist[i] = stu[code-10000].booklist[i+1];
stu[code-10000].borrowdate[i] = stu[code-10000].borrowdate[i+1];
}
stu[code-10000].book_keeping--;
break;
}
case 2:
{
for(i=0;i<tea[code-10000].book_keeping;i++)
//输出当前借阅书单
{
cout<<i<<"-----"<<tea[code-10000].booklist[i]<<endl;
}
cout<<"输入书前序号选择归还"<<endl;
cin>>r;
bname = tea[code-10000].booklist[r];
//检查书籍是否超期
try
{ int time;
time = keepingtime(tea[code-10000].borrowdate[r], today);
if(time>tea[code-10000].reader_keepdays_limit)
{
cout<<"允许借阅天数:"<<tea[code-10000].reader_keepdays_limit<<endl;
cout<<"超期!超出天数:"<<time - tea[code-10000].reader_keepdays_limit;
throw (time - tea[code-10000].reader_keepdays_limit);
}
else cout<<"已归还 ["<< tea[code-10000].booklist[r] <<"]"<<endl;
}catch(int fineday){
double fine;
fine = fineday*0.5;
cout<<" 需缴纳 "<<fine<<" 元罚款"<<endl;
}
for(i=r; i<tea[code-10000].book_keeping ; i++ )
//还书后重新排列借阅书单
{ tea[code-10000].booklist[i] = tea[code-10000].booklist[i+1];
tea[code-10000].borrowdate[i] = tea[code-10000].borrowdate[i+1];
}
tea[code-10000].book_keeping--;
break;
}
}
book[ searchbook(bname) ].Book_condition = 1;
/*书籍归还*/log(2, searchbook(bname), today, code-10000);
cout<<"1=继续还书 任意键=退出"<<endl;
cin>>step;
}
}
}
/*Book*/int keepingtime(int bdate, int today)//计算书籍借阅时长
{
int day0,day1,month0,month1;
int sum=0;
int days;
day0 = bdate%100;//借出时间
month0 = bdate/100;
day1 = today%100;//归还时间
month1 = today/100;
if(month0 != month1)
{
sum = day1;
month1 -= 1;
}
for( ; month1>=month0 ; month1-=1 )
{
if(month1 == 2) days = 28;
else if(month1 == 1||month1 == 3||month1 == 5||month1 == 7||month1 == 8||month1 == 10||month1 == 12)
days=31;
else days = 30;
if (month1==month0)
{
if(sum!=0)
sum += days - day0;
else sum += day1-day0;
}
else sum += days;
}
return sum;
}
/*Book*/int searchbook(string name)//书籍查找//condition:传入1 = 借书搜索 传入0:还书
{
int i;
int find=0;
//cout<<"Searching..."<<endl;
for(i=0; find==0 && i<100; i++)
{ if(name.compare(book[i].Book_name) == 0) {
find = 1;
return i;}
}
return -1;
}
/*Book*/void addbook()//增加书籍
{/*
Chinese book------------【1书名】
出版社1------------------【2作者名】
0-------------------------【3借阅情况,0=借出(不可借),1=在馆(可借)】
*-------------------------【4书籍信息结尾】
*/
cin>>book[booksum];
ofstream outfile;
outfile.open("C:\\图书管理系统\\book_list.txt",ios::app);
cout << "写入书库目录" << endl;
outfile <<endl;
outfile << book[booksum].Book_name <<endl;
outfile << book[booksum].Book_writer <<endl;
outfile << book[booksum].Book_condition <<endl;
outfile << "*" ;
// 关闭打开的文件
outfile.close();
/*新增书籍*/
booksum++;
cout<<endl<<"书籍总数 = "<<booksum<<endl<<endl;//text
}
/*Book*/void deletebook()//删除书籍
{
string delete_bookname;
cout<<"输入需要删除的书籍名称:"<<endl;
cin.get();
getline(cin,delete_bookname);
cout<<"查找 ["<<delete_bookname<<"]中...... ";
int find=0,decide,i;
for(i=0; find==0&&i<booksum ;i++)
{ if( !(delete_bookname.compare(book[i].Book_name)) )
find=1;
}
cout<<"找到目标书籍? "<<find<<endl;
if(find==0) cout<<" 未找到! "<<endl;
if(find==1) {
cout<<"确定删除此书,不允许借阅?"<<endl<<book[i-1].Book_name<<" "<<book[i-1].Book_writer<<endl;
cout<<"1 = 确定 其他数字键 = 退出"<<endl;
cin>>decide;
if(decide==1)
{ book[i-1].Book_condition = -1;
cout<<"已删除 ["<< book[i-1].Book_name <<"]"<<endl;
/*书籍删除*/
booksum-=1;
}
}
}
/*Book*/void modifybook()//修改书籍
{
string modify;
int step;
cout<<"输入需要修改的书籍名称:"<<endl;
cin.get();
getline(cin,modify);
cout<<"查找 ["<<modify<<"]中...... ";
int find=0,modify_c,i;
for(i=0; find<=0&&i<booksum ;i++)
{ if( !(modify.compare(book[i].Book_name)) )
{ find=1;
cout<<"找到目标书籍? "<<find<<endl;
if(book[i].Book_condition == 0)
{ cout<<"["<<book[i].Book_name<<"] 处于借出状态,暂时无法修改信息"<<endl;
find = -1;
}
}
}
if(find==0) cout<<" 未找到! "<<endl;
if(find==1)
{ cout<<book[i-1].Book_name<<" "<<book[i-1].Book_writer<<" "<<book[i-1].Book_condition<<endl;
cout<<"修改: 1 = 书名 2 = 作者 3 = 书籍状态 其它数字键 = 退出"<<endl;
cin>>step;
if(step==1)
{ cout<<"输入此书籍新名称:"<<endl;
cin.get();
getline(cin,modify);
cout<<"加载中...... "<<endl;
book[i-1].Book_name = modify;
}
else if(step==2)
{ cout<<"输入此书籍新作者:"<<endl;
cin.get();
getline(cin,modify);
cout<<"加载中...... "<<endl;
book[i-1].Book_writer = modify;
}
else if(step==3)
{ cout<<"更新此书籍状态为:(1=在馆 0=借出 -1=删除/无法借阅)"<<endl;
cin>>modify_c;
cout<<"加载中...... "<<endl;
book[i-1].Book_condition = modify_c;
}
else { i = -1; }
if(i > 0)
{ cout<<book[i-1].Book_name<<" "<<book[i-1].Book_writer<<" "<<book[i-1].Book_condition<<endl;
/*书籍修改*/
cout<<" 修改完成 "<<endl;
}
}
}
/*Book*/void book_list_record()//更新书库文件book_list.txt
{ ofstream outfile;
outfile.open("C:\\图书管理系统\\book_list.txt",ios::trunc);
cout << "更新书库目录..." << endl;
for(int i = 0 ; i < booksum ; i++)
{ if(i!=0) outfile <<endl;
outfile << book[i].Book_name <<endl;
outfile << book[i].Book_writer <<endl;
outfile << book[i].Book_condition <<endl;
outfile << "*" ;
}
//cout<<"booksum = "<<booksum<<endl<<endl;//text
// 关闭打开的文件
outfile.close();
cout << "更新书库目录完成!" << endl;
}
/*User*/void rewrite_User_list()//更新用户文件
{ ofstream outfile;
int i;
//向文件User_list更新用户数据
cout<<"更新用户目录..."<<endl;
outfile.open( "C:\\图书管理系统\\User_list.txt", ios::trunc);
for(i=1 ; i<user_codetail-10000 ; i++)
{ if(i>1) outfile <<endl;
outfile << user[i].User_code <<endl;
outfile << user[i].User_name <<endl;
outfile << user[i].User_type <<endl;
outfile << user[i].User_key <<endl;
outfile << "*" ;
}
// 关闭打开的文件
outfile.close();
cout<<"更新用户目录完成!"<<endl;
int reader=0, admin=0;
cout<<"更新读者目录..."<<endl;
cout<<"更新管理员目录..."<<endl;
for(i=1 ; i<user_codetail-10000 ; i++)
{
if(user[i].User_type<10)// 向文件reader_list更新用户数据
{ if(reader==0)
{ outfile.open("C:\\图书管理系统\\reader_list.txt",ios::trunc);
reader++;
}
else
{ outfile.open("C:\\图书管理系统\\reader_list.txt",ios::app);
outfile <<endl;
}
outfile << user[i].User_code <<endl;
outfile << user[i].User_name <<endl;
outfile << user[i].User_type <<endl;
outfile << user[i].User_key <<endl;
if(user[i].User_type==1)
{ outfile << stu[i].book_keeping <<endl;
if(stu[i].book_keeping==0){}
else
{ int booknum;
outfile << "List" <<endl;
for(booknum=0 ; booknum <stu[i].book_keeping ; booknum++)
{ outfile << booknum <<endl;
outfile << stu[i].booklist[booknum] <<endl;
outfile << stu[i].borrowdate[booknum] <<endl;
}
}
outfile << "*" ;
}
else if(user[i].User_type==2)
{ outfile << tea[i].book_keeping <<endl;
if(tea[i].book_keeping==0){}
else
{ int booknum;
outfile << "List" <<endl;
for(booknum=0 ; booknum <tea[i].book_keeping ; booknum++)
{ outfile << booknum <<endl;
outfile << tea[i].booklist[booknum] <<endl;
outfile << tea[i].borrowdate[booknum] <<endl;
}
}
outfile << "*" ;
}
// 关闭打开的文件
outfile.close();
}
else// 向文件administrator_list更新用户数据
{ if(admin==0)
{ outfile.open("C:\\图书管理系统\\administrator_list.txt",ios::trunc);
admin++;
}
else
{ outfile.open("C:\\图书管理系统\\administrator_list.txt",ios::app);
outfile <<endl;
}
outfile << user[i].User_code <<endl;
outfile << user[i].User_name <<endl;
outfile << user[i].User_type <<endl;
outfile << user[i].User_key <<endl;
outfile << "*" ;
// 关闭打开的文件
outfile.close();
}
}
cout<<"更新读者目录完成!"<<endl;
cout<<"更新管理员目录完成!"<<endl;
}
/*Book*/void log(int event, int book_code, int date, int reader_code)//图书借还日志文件
{/* 1 = 书籍借出 2 = 书籍归还*/
ofstream outfile;
outfile.open("C:\\图书管理系统\\日志文件log\\图书借还日志.txt", ios::app );
//cout << "Updating the log of book..." << endl;//text
switch(event)
{
case 1:
{ outfile<<"["<< book[book_code].Book_name <<"] "<<book[book_code].Book_writer<<"---";
outfile<<date<<"--借出"<<endl;
outfile<<" 读者"<<user[reader_code].User_code<<" ["<<user[reader_code].User_name<<"]"<<endl;
outfile << "*" <<endl;
// 关闭打开的文件
outfile.close();
break;
}
case 2:
{ outfile<<"["<< book[book_code].Book_name <<"] "<<book[book_code].Book_writer<<"---";
outfile<<date<<"--归还"<<endl;
outfile<<" 读者"<<user[reader_code].User_code<<" ["<<user[reader_code].User_name<<"]"<<endl;
outfile << "*" <<endl;
// 关闭打开的文件
outfile.close();
break;
}
}
}
/*-----------------------------------------------------------------------------*/
(疯狂注释+换行导致代码有1000+行,又臭又长)
#截图
运行正常开始。
End.
欢迎评论。
20200709更新:处理了原代码中中英混用的情况,现应为全中文。