图书馆管理系统代码.docx
《图书馆管理系统代码.docx》由会员分享,可在线阅读,更多相关《图书馆管理系统代码.docx(21页珍藏版)》请在冰点文库上搜索。
#include
#defineDEBUG0
//宏定义决定测试代码是否编译0为不编译非0则编译
constcharbook_path[20]="books.txt";
constcharreader_path[20]="readers.txt";
//文件读写声明
voidread_from_files_to_vector();
voidwrite_all_to_files();
#ifDEBUG
voidscan_readers(){
for(reader&s:
readers){
wcout<
}
}
#endif
intmain()
{
//完成操作后需要在控制台按5中退出才会保存到文件
wcin.imbue(china);
wcout.imbue(china);
read_from_files_to_vector();
#ifDEBUG
cout<<"sizeofreaders="< cout<<"sizeofbooks="< scan_book();
scan_readers();
#endif
stringpassword="123456"; //密码已改
cout<<"------------欢迎使用中南大学网络在线图书系统------------"< //这里可以添加你们小组的名单或一些装饰的字符
cout<<"--------------------请选择登录方式--------------------"< cout<<"1管理员登陆2读者登陆"< inta;
cin>>a;
cleanScreen();
if(a==1)
{
stringstr;
for(intj=0;j<=4;j++)
{
cout<<"请输入管理密码,你有"<<(4-j)<<"次机会"< cin>>str;
if(password==str)
{
break;
}
elsecout<<"密码错误!
请重新输入"< if(j==4)
return0;
}
cleanScreen();
cout<<"-------------欢迎来到图书管理系统-------------"< cout<<"成功登陆!
你有以下操作可供选择"< cout< //操作循环
cout<<"1浏览图书信息"< cout<<"2图书信息修改"< cout<<"3图书信息添加"< cout<<"4图书信息删除"< cout<<"5保存信息并退出系统"< cout<<"--------------请选择操作选项编号--------------"< intb;
cin>>b;
cleanScreen();
while(b!
=5){
switch(b)
{
case1:
scan_book();break;
case2:
modify_book();break;
case3:
add_book();break;
case4:
delete_book();break;
default:
cout<<"输入错误,请重新输入";break;
}
cout< cout<<"---------------请进行下一次操作---------------"< cout<<"1浏览图书信息"< cout<<"2图书信息修改"< cout<<"3图书信息添加"< cout<<"4图书信息删除"< cout<<"5退出系统"< cout<<"--------------请选择操作选项编号--------------"< cin>>b;
cleanScreen();
}
write_all_to_files();
return0;
}
elseif(a==2)
{
intreaderIndex=0; //记录读者下标
//输入学号登陆
cout<<"--------------请输入学号--------------"< wchar_tstudentnumber[20];
for(intm=0;m<=9;m++)
{
cout<<"请输入学号,你有"<<(9-m)<<"次机会"< wcin>>studentnumber;
intt;
for(t=0;t {
if(wcscmp(studentnumber,readers[t].cardnum)==0)
{
readerIndex=t;
break;
}
}
//判断跳出循环原因
if(t break;
else
{
cout<<"登录账号不存在,请重新输入学号"< continue;
}
if(m==9)
return0;
}
cleanScreen();
cout<<"------------欢迎来到读者自助系统------------"< wcout<你有以下操作可供选择"< cout< //操作循环
cout<<"1图书浏览"< cout<<"2图书查询"< cout<<"3图书借阅"< cout<<"4图书返还"< cout<<"5退出系统"< cout<<"---------------请选择操作选项--------------"< intc;
cin>>c;
cleanScreen();
while(c!
=5)
{
switch(c)
{
case1:
scan_book();break;
case2:
search_book();break;
case3:
borrow_book(readerIndex);break;
case4:
return_book(readerIndex);break;
default:
cout<<"输入错误!
请重新输入"< }
cout< cout<<"--------------请进行下一次操作--------------"< cout<<"1图书浏览"< cout<<"2图书查询"< cout<<"3图书借阅"< cout<<"4图书返还"< cout<<"5退出系统"< cout<<"---------------请选择操作选项---------------"< cin>>c;
cleanScreen();
}
write_all_to_files();
return0;
}
return0;
}
//main函数结束
//写入/读取文件
voidwrite_all_to_files()
{
wofstreamoutfile;
outfile.imbue(locale("chs"));
//写入书本文件
outfile.open(book_path);
outfile< for(inti=0;i {
outfile< }
outfile.close();
outfile.clear();
//写入读者文件,由于程序中没有针对读者的删改(备用,将0改为1后即可编译这段代码)
#if0
outfile.open(path:
:
reader_path);
outfile< for(inti=0;i {
outfile< }
outfile.close();
#endif
return;
}
voidread_from_files_to_vector()
{
wifstreaminfile;
infile.imbue(locale("chs"));
//用于设置读写中文
bookbb;
readerrr;
//在程序目录下没有相应文件时创建空文件(备用,将0改为1后即可编译这段代码)
#if0
wofstreamcreat_if_no;
creat_if_no.imbue(locale("chs"));
creat_if_no.open(book_path,ios:
:
_Noreplace);
creat_if_no.close();
creat_if_no.clear();
creat_if_no.open(reader_path,ios:
:
_Noreplace);
creat_if_no.close();
creat_if_no.clear();
#endif
//读取书本数据
infile.open(book_path);
intsize=0;
infile>>size;
while(!
infile.eof()&&size!
=0)
{
infile>>bb;
books.push_back(bb);
size--;
}
infile.close();
infile.clear();
//读取读者数据
infile.open(reader_path);
infile>>size;
while(!
infile.eof()&&size!
=0)
{
infile>>rr;
readers.push_back(rr);
size--;
}
infile.close();
return;
}
#include"classes.h"
localechina("chs");//usechinacharacter
/*voidprintout_book(book&b)
{
cout< < < < < < < <}*/
//这个函数我用重载运算符<<的函数代替了
//对应全局变量的定义
vectorbooks;
vectorreaders;
//book重载操作符函数定义
wistream&operator>>(wistream&in,book&b)
{
cout<<"请输入书号"< in>>b.id;
cout<<"请输入书名"< in>>b.title;
cout<<"请输入作者名"< in>>b.firstauthor;
cout<<"请输入出版社"< in>>b.publisher;
cout<<"请输入印刷日期(年月)"< in>>b.birthday;
cout<<"请输入所属类别"< in>>b.location;
cout<<"请输入价格"< in>>b.price;
cout<<"请输入数量"< in>>b.number;
returnin;
}
wostream&operator<<(wostream&out,book&b)
{
out< < < < < < < < < returnout;
}
wifstream&operator>>(wifstream&in,book&b)
{
in>>b.id>>b.title>>b.firstauthor
>>b.publisher>>b.birthday.year>>b.birthday.month
>>b.location>>b.price>>b.number;
returnin;
}
wofstream&operator<<(wofstream&out,book&b)
{
out< < < < < < < < returnout;
}
//reader重载操作符函数定义
wistream&operator>>(wistream&in,reader&r)
{
//由于没有对reader的操作,这个函数尚未被使用
cout<<"请输入读者姓名"< in>>r.name;
cout<<"请输入性别(男为0,女为1)"< in>>r.s;
cout<<"请输入卡号"< in>>r.cardnum;
cout<<"请输入学位(学士为0,硕士为1,博士为2)"< in>>r.e;
r.already=0; //已借书本数默认为0
returnin;
}
wostream&operator<<(wostream&out,reader&r)
{
out< out< out< out< out< returnout;
}
wifstream&operator>>(wifstream&in,reader&r)
{
in>>r.cardnum>>r.name>>r.s
>>r.e>>r.already;
returnin;
}
wofstream&operator<<(wofstream&out,reader&r)
{
out< < < < < returnout;
}
//main函数中调用定义
voidscan_book()//建立浏览函数
{
for(inti=0;i {
wcout< }
}
voidsearch_book()//建立搜索函数
{
intg;
cout<<"************你有以下查询方式可供选择************"< cout<<"1按书名查询"< cout<<"2按编号查询"< cout<<"3按作者查询"< cout<<"************请选择查询方式************"< cin>>g;
switch(g)
{
case1:
{
wchar_tname[20];
cout<<"************请输入书名************"< wcin>>name;
for(intr=0;r {
if(wcscmp(books[r].title,name)==0)
{
wcout< return;
}
}
cout<<"查无此书"< return;
break;
}
case2:
{
intstr;
cout<<"************请输入图书编号************"< cin>>str;
for(intl=0;l {
if(books[l].id==str)
{
wcout< return;
}
}
cout<<"查无此书"< return;
break;
}
case3:
{
wchar_tauth[10];
cout<<"************请输入图书作者************"< wcin>>auth;
for(intp=0;p {
if(wcscmp(books[p].firstauthor,auth)==0)
{
wcout< return;
}
}
cout<<"查无此书"< return;
break;
}
default:
{
cout<<"查无此书"< return;
}
}
}
//!
!
!
实现逻辑有问题,无法建立书-借书人的对应关系
voidborrow_book(intt)//建立借书函数
{
if(readers[t].already>=5)
{
cout<<"抱歉,你的借书数已满,不可再借"< return;
}
intlog;
cout<<"************请输入要借出图书的登陆号************"< wcin>>log;
for(inti=0;i {
if(log==books[i].id)
{
if(books[i].number>0)
{
inty;
cout<<"确认借出书籍吗?
确认请输入1取消请输入0"< cin>>y;
if(y==1)
{
cout<<"借阅成功!
";
--(books[i].number);
++(readers[t].already);
return;
}
elsereturn;
}
else
{
cout<<"图书已借完"<