课程设计之小型图书馆管理系统
这是大学里首个作业,以前闲暇时看过别人写过的,也有一些经验了,看来有点知识用不到不代表不重要。
题目就不说了,万变不离其宗,我采取的数组实现的,比较简单吧,用链表我没那信心,等学完数据结构在折腾吧。附上代码,待以后改进,
#include<iostream>
#include<fstream> //文件IO库,i 为in,o 为 out,即文件输入输出库。
#include<string>
#include<cstring>
#include<iomanip> //控制输出的库
#include<algorithm> //常用算法库
using namespace std;
#define MaxNumber 100 //最多图书管理数目
typedef struct Date //日期结构体的定义
{
int year,month,day;
};
typedef struct book //图书信息结构体的定义
{
int b_code; //图书的唯一编号
string b_name; //书名
string b_author; //作者
string b_pulisher; //出版社
float b_price; //单价
Date b_pulishdate; //出版日期
string b_borrower; //借阅者
Date b_borrowdate; //借出日期
Date b_lenderdate; //归还日期
bool flag;
};
typedef struct Library //图书馆结构体定义
{
book Book[MaxNumber];
int num; //已有图书数目
};
class Tushu
{
private:
Library first;
public:
Tushu();
bool set(); // 0.新建
bool app(); //1.添加
bool del(); //2.删除
bool load(); //9.载入信息
bool read(); //10.读取
bool display(); //11.预览
bool borrow(); //6.借书
bool lender(); //7.还书
bool search(); //4.查找
bool search(int); //查找编号是否存在
int Search(int); //由图书编号查找第几本输入的书
bool Sort(); //3.排序
bool change(); //5.修改
void notice(); //打印催还书通知
};
Tushu::Tushu() //::为类限定符,类外定义需要
{
first.num=0;
for(int i=0;i<MaxNumber;++i) //对是否有借阅者信息的标识,初始化为false
first.Book[i].flag=0;
}
bool Tushu::set() //新建
{
cout<<"请输入初始信息:"<<endl;
first.num=0; //置图书数为0
cout<<"请输入图书唯一编号:"<<endl;
cin>>first.Book[first.num].b_code;
cout<<"请输入书名:"<<endl;
cin>>first.Book[first.num].b_name;
cout<<"请输入图书的作者:"<<endl;
cin>>first.Book[first.num].b_author;
cout<<"请输入出版社信息:"<<endl;
cin>>first.Book[first.num].b_pulisher;
cout<<"请输入图书的单价:"<<endl;
cin>>first.Book[first.num].b_price;
cout<<"请输入出版日期,并按年月日的格式输入:"<<endl;
cin>>first.Book[first.num].b_pulishdate.year>>first.Book[first.num].b_pulishdate.month>>first.Book[first.num].b_pulishdate.day;
first.num++;
load();
cout<<"初始信息已完成!"<<endl;
system("pause");
}
bool Tushu::load() //文件流对象,把内容输出到文本文件中
{
ofstream outfile("f1.dat",ios::out);
if(!outfile)
{
cerr<<"oper error!"<<endl;
exit(0);
}
for(int i=0;i<first.num;++i)
{
outfile<<first.Book[i].b_code<<" "<<first.Book[i].b_name<<" "<<first.Book[i].b_author<<" "<<first.Book[i].b_pulisher<<
" "<<first.Book[i].b_price<<" "<<first.Book[i].b_pulishdate.year<<" "<<first.Book[i].b_pulishdate.
month<<" "<<first.Book[i].b_pulishdate.day<<" "<<first.Book[i].b_borrower<<" "<<first.Book[i].b_borrowdate.year<<" "<<first.Book[i].b_borrowdate.month
<<" "<<first.Book[i].b_borrowdate.day<<" "<<first.Book[i].b_lenderdate.year<<" "<<first.Book[i].b_lenderdate.month<<
" "<<first.Book[i].b_lenderdate.day<<endl; //必须要有“ ”,否则连成一片无法正确读取 ,同时尾行要endl
}
outfile.close(); //关闭对象关联
}
bool Tushu::read() //从文本文件中读取数据
{
ifstream infile("f1.dat");
if(!infile) //读取错误
{
cerr<<"open error!"<<endl;
exit(0);
}
for(int i=0;i<first.num;++i)
{
infile>>first.Book[i].b_code>>first.Book[i].b_name>>first.Book[i].b_author>>first.Book[i].b_pulisher>>
first.Book[i].b_price>>first.Book[i].b_pulishdate.year>>first.Book[i].b_pulishdate.
month>>first.Book[i].b_pulishdate.day>>first.Book[i].b_borrower>>first.Book[i].b_borrowdate.year>>first.Book[i].b_borrowdate.month
>>first.Book[i].b_borrowdate.day>>first.Book[i].b_lenderdate.year>>first.Book[i].b_lenderdate.month>>
first.Book[i].b_lenderdate.day;
}
infile.close(); //结束关联
}
int Tushu::Search(int code) //根据图书编号查找 该是在第几次输入
{
for(int i=0;i<first.num;++i)
{
if(code==first.Book[i].b_code)
{
return i;break;
}
}
return 0;
}
bool Tushu::borrow() //借书,
{
system("cls");
int code;
cout<<"请输入以下借书信息"<<endl;
cout<<"1.请输入要借图书的编号:"<<endl;
cin>>code;
if(search(code))
{
int i=Search(code);
cout<<"请输入你的名字,并按回车键结束"<<endl;
cin>>first.Book[i].b_borrower;
cout<<"请输入借出日期:"<<endl;
cin>>first.Book[i].b_borrowdate.year>>first.Book[i].b_borrowdate.month>>first.Book[i].b_borrowdate.day;
cout<<"请输入归还日期:"<<endl;
cin>>first.Book[i].b_lenderdate.year>>first.Book[i].b_lenderdate.month>>first.Book[i].b_lenderdate.day;
cout<<" 借书成功!"<<endl;
first.Book[i].flag=true; //借书成功标识为true
load();
return true;
}
else
{
cout<<" sorry,no match book!"<<endl;
return false;
}
system("pause");
}
bool Tushu::display() //显示
{
read();
cout<<"★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★"<<endl;
cout<<"图书 书 作 出版社 单 出版 借阅 借出 还书 "<<endl;
cout<<"编号 名 者 信息 价 日期 者 日期 日期 "<<endl;
for(int i=0;i<first.num;++i)
{
cout<<first.Book[i].b_code<<" "<<setw(6)<<first.Book[i].b_name<<" "<<setw(6)
<<first.Book[i].b_author<<" "<<setw(8)<<first.Book[i].b_pulisher<<" "
<<setw(6)<<first.Book[i].b_price<<setw(7)<<first.Book[i].b_pulishdate.year<<"/"<<first.Book[i].b_pulishdate.
month<<"/"<<first.Book[i].b_pulishdate.day;
if(first.Book[i].flag) //有借阅者的话
{
cout<<" "<<setw(6)<<first.Book[i].b_borrower<<" "<<setw(6)<<first.Book[i].b_borrowdate.year<<'/'<<first.Book[i].b_borrowdate.
month<<'/'<<first.Book[i].b_borrowdate.day<<" "<<setw(6)<<first.Book[i].b_lenderdate.year<<'/'<<first.Book[i].b_lenderdate.month
<<'/'<<first.Book[i].b_lenderdate.day<<endl;
}
else
cout<<endl;
}
system("pause");
return true;
}
bool Tushu::app()
{
ofstream outfile("f1.dat",ios::app); //在文本文件末尾添加数据
while(1)
{
cout<<"请输入图书唯一编号:"<<endl;
cin>>first.Book[first.num].b_code;
cout<<"请输入书名:"<<endl;
cin>>first.Book[first.num].b_name;
cout<<"请输入图书的作者:"<<endl;
cin>>first.Book[first.num].b_author;
cout<<"请输入出版社信息:"<<endl;
cin>>first.Book[first.num].b_pulisher;
cout<<"请输入图书的单价:"<<endl;
cin>>first.Book[first.num].b_price;
cout<<"请输入出版日期,并按年月日的格式输入:"<<endl;
cin>>first.Book[first.num].b_pulishdate.year>>first.Book[first.num].b_pulishdate.month>>first.Book[first.num].b_pulishdate.day;
first.num++;
load();
char choice;
system("cls");
cout<<"还要继续添加吗?(Y/N)"<<endl;
cin>>choice;
if(choice=='N'||choice=='n') break;
else ;
}
}
void menu() //菜单
{
cout<<" ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★"<<endl;
cout<<" ★ Welcome! ☆"<<endl;
cout<<" ☆ Menu: ★"<<endl;
cout<<" ★ 1.新建 2.添加 3.删除 4.排序 ☆"<<endl;
cout<<" ☆ 5.查找 6.修改 7.借书 8.还书 ★"<<endl;
cout<<" ★ 9.打印催书通知 10.保存 11.载入 ☆"<<endl;
cout<<" ☆ 12.显示全部记录 13.退出 ★"<<endl;
cout<<" ★ 欢迎使用图书馆管理系统! ☆"<<endl;
cout<<" ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★"<<endl;
cout<<" 请输入关键码(0~12):";
}
void choice() //查找选择
{
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 查找方式(search way) ┃"<<endl;
cout<<" ┃ 1.按图书编号 2.按图书作者 ┃"<<endl;
cout<<" ┃ ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
}
void choicesort() //排序选择
{
cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 排序方式(search way) ┃"<<endl;
cout<<" ┃ 1.按图书编号(Down) 2.按作者姓名(Down) ┃"<<endl;
cout<<" ┃ 3.按图书编号(Up) 4.按作者姓名(Up) ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
}
bool Tushu::search()
{
system("cls");
choice();
int key;
cin>>key;
if(key==1)
{
int searchcode;
cout<<" 请输入要查找的图书编号:"<<endl;
cin>>searchcode;
for(int i=0;i<first.num;++i)
{
if(first.Book[i].b_code==searchcode)
{
cout<<"提示: 查找成功!"<<endl;
cout<<"图书 书 作 出版社 单 出版 借阅 借出 归还 "<<endl;
cout<<"编号 名 者 信息 价 日期 者 日期 日期 "<<endl;
cout<<first.Book[i].b_code<<" "<<setw(8)<<first.Book[i].b_name<<" "<<setw(8)<<first.Book[i].b_author<<" "<<setw(8)<<first.Book[i].b_pulisher<<" "
<<setw(8)<<first.Book[i].b_price<<" "<<setw(4)<<first.Book[i].b_pulishdate.year<<"/"<<first.Book[i].b_pulishdate.
month<<"/"<<first.Book[i].b_pulishdate.day;
if(first.Book[i].flag)
{
cout<<" "<<setw(6)<<first.Book[i].b_borrower<<" "<<setw(4)<<first.Book[i].b_borrowdate.year<<'/'<<first.Book[i].b_borrowdate.month<<
'/'<<first.Book[i].b_borrowdate.day<<" "<<setw(4)<<first.Book[i].b_lenderdate.year<<'/'<<first.Book[i].b_lenderdate.month<<'/'<<
first.Book[i].b_lenderdate.day<<endl;
}
system("pause");
return true;break;
}
}
cout<<" 查找失败!"<<endl;
system("pause");
return false;
}
if(key==2)
{
string searchauthor;
cout<<" 请输入要查找的图书作者:"<<endl;
cin>>searchauthor;
for(int i=0;i<first.num;++i)
{
if(first.Book[i].b_author==searchauthor)
{
cout<<"提示: 查找成功!"<<endl;
cout<<"图书 书 作 出版社 单 出版 借阅 借出 归还 "<<endl;
cout<<"编号 名 者 信息 价 日期 者 日期 日期 "<<endl;
cout<<first.Book[i].b_code<<" "<<setw(8)<<first.Book[i].b_name<<" "<<setw(8)<<first.Book[i].b_author<<" "<<setw(8)<<first.Book[i].b_pulisher<<" "
<<setw(8)<<first.Book[i].b_price<<" "<<setw(4)<<first.Book[i].b_pulishdate.year<<"/"<<first.Book[i].b_pulishdate.
month<<"/"<<first.Book[i].b_pulishdate.day;
if(first.Book[i].flag)
{
cout<<" "<<setw(6)<<first.Book[i].b_borrower<<" "<<setw(4)<<first.Book[i].b_borrowdate.year<<'/'<<first.Book[i].b_borrowdate.month<<
'/'<<first.Book[i].b_borrowdate.day<<" "<<setw(4)<<first.Book[i].b_lenderdate.year<<'/'<<first.Book[i].b_lenderdate.month<<'/'<<
first.Book[i].b_lenderdate.day<<endl;
}
system("pause");
return true;break;
}
}
cout<<" 查找失败!"<<endl;
system("pause");
return false;
}
}
bool Tushu::del()
{
system("cls");
choice();
int key;
cin>>key;
if(key==1)
{
int searchcode;
cout<<" 请输入要删除图书的编号:";
cin>>searchcode;
cout<<endl;
for(int i=0;i<first.num;++i)
{
if(searchcode==first.Book[i].b_code)
{
ofstream outfile("f1.dat",ios::out);
for(int j=0;j<first.num;++j)
{
if(first.num==1&&j==0&&i==0)
{
first.num--; // 直接置书本数减1
outfile.close();
cout<<" 删除成功!"<<endl;
system("pause");
return true;
}
if(j!=i) //J与i均为零的话,则 0!=0为假,则删除不能进行,所以需要考虑特殊情况
{
outfile<<first.Book[j].b_code<<" "<<first.Book[j].b_name<<" "<<first.Book[j].b_author<<" "<<first.Book[j].b_pulisher<<
" "<<first.Book[j].b_price<<" "<<first.Book[j].b_pulishdate.year<<" "<<first.Book[j].b_pulishdate.
month<<" "<<first.Book[j].b_pulishdate.day<<" "<<first.Book[j].b_borrower<<" "<<first.Book[j].b_borrowdate.year<<" "<<first.Book[j].b_borrowdate.month
<<" "<<first.Book[j].b_borrowdate.day<<" "<<first.Book[j].b_lenderdate.year<<" "<<first.Book[j].b_lenderdate.month<<
" "<<first.Book[j].b_lenderdate.day;
}
}
outfile.close();
cout<<" 删除成功!"<<endl;
first.num--;
system("pause");
return true;
}
}
cout<<" 没有该图书的相关信息!"<<endl;
system("pause");
return false;
}
if(key==2)
{
string searchname;
cout<<" 请输入要删除图书的作者姓名:";
cin>>searchname;
cout<<endl;
for(int k=0;k<first.num;++k)
{
if(first.Book[k].b_author==searchname)
{
ofstream outfile("f1.dat",ios::out);
for(int m=0;m<first.num;++m)
{
if(first.num==1&&m==0&&k==0)
{
first.num--; // 直接置书本数减1
outfile.close();
cout<<" 删除成功!"<<endl;
system("pause");
return true;
}
if(m!=k)
{
outfile<<first.Book[m].b_code<<" "<<first.Book[m].b_name<<" "<<first.Book[m].b_author<<" "<<first.Book[m].b_pulisher<<
" "<<first.Book[m].b_price<<" "<<first.Book[m].b_pulishdate.year<<" "<<first.Book[m].b_pulishdate.
month<<" "<<first.Book[m].b_pulishdate.day<<" "<<first.Book[m].b_borrower<<" "<<first.Book[m].b_borrowdate.year<<" "<<first.Book[m].b_borrowdate.month
<<" "<<first.Book[m].b_borrowdate.day<<" "<<first.Book[m].b_lenderdate.year<<" "<<first.Book[m].b_lenderdate.month<<
" "<<first.Book[m].b_lenderdate.day<<endl;;
}
}
outfile.close();
cout<<" 删除成功!"<<endl;
first.num--;
system("pause");
return true;
}
}
cout<<" 没有该图书相关的信息!"<<endl;
system("pause");
return false;
}
}
bool Tushu::search(int code) //判断是否有这本书
{
for(int i=0;i<first.num;++i)
{
if(first.Book[i].b_code==code)
{
return true;
}
}
return false;
}
int comp(const void *p1,const void *p2) //自定义排序函数 ,具体可百度sort(。。。) 与qsort(,,,)函数
{
return (*(book*)p2).b_code>(*(book*)p1).b_code;
}
int comp1(const void *p1,const void *p2) //同上
{
return (*(book*)p2).b_code<(*(book*)p1).b_code;
}
int Comp(const void *p1,const void *p2)
{
return strcmp((char*)p2,(char*)p1);
}
int Comp1(const void *p1,const void *p2)
{
return strcmp((char*)p1,(char*)p2);
}
bool Tushu::Sort()
{
choicesort();
int key;
cin>>key;
if(key==1)
{
qsort(first.Book,first.num,sizeof(first.Book[0]),comp);
cout<<"按图书编号排序(Down)完成!"<<endl;
load();
system("pause");
}
if(key==2)
{
qsort(first.Book,first.num,sizeof(first.Book[0]),Comp);
cout<<"按作者排序(Down)完成!"<<endl;
load();
system("pause");
}
if(key==3)
{
qsort(first.Book,first.num,sizeof(first.Book[0]),comp1);
cout<<"按图书编号排序(Up)完成!"<<endl;
load();
system("pause");
}
if(key==4)
{
qsort(first.Book,first.num,sizeof(first.Book[0]),Comp1);
cout<<"按作者编号排序(Up)完成!"<<endl;
load();
system("pause");
}
}
bool Tushu::change()
{
cout<<" 请输入要修改图书的信息,!,首先判断该图书是否存在"<<endl;
system("pause");
int b=search();
if(b)
{
cout<<"请重新输入要修改图书的编号!!"<<endl;
int searchcode;
cin>>searchcode;
cout<<endl;
int i=Search(searchcode);
cout<<"请输入要修改的信息:"<<endl;
cout<<"请输入图书唯一编号:"<<endl;
cin>>first.Book[i].b_code;
cout<<"请输入书名:"<<endl;
cin>>first.Book[i].b_name;
cout<<"请输入图书的作者:"<<endl;
cin>>first.Book[i].b_author;
cout<<"请输入出版社信息:"<<endl;
cin>>first.Book[i].b_pulisher;
cout<<"请输入图书的单价:"<<endl;
cin>>first.Book[i].b_price;
cout<<"请输入出版日期,并按年月日的格式输入:"<<endl;
cin>>first.Book[i].b_pulishdate.year>>first.Book[i].b_pulishdate.month>>first.Book[i].b_pulishdate.day;
load();
cout<<" 修改成功!"<<endl;
system("pause");
return true;
}
if(!b)
{
cout<<" 修改失败!"<<endl;
system("pause");
return false;
}
}
void Tushu::notice()
{
for(int i=0;i<first.num;++i)
{
if(first.Book[i].flag)
{
cout<<" 图书编号 借阅日期 归还日期"<<endl;
cout<<" "<<first.Book[i].b_code<<" "<<setw(8)<<first.Book[i].b_borrowdate.year<<'/'<<first.Book[i].b_borrowdate.month<<'/'<<
first.Book[i].b_borrowdate.day<<" "<<setw(8)<<first.Book[i].b_lenderdate.year<<'/'<<first.Book[i].b_lenderdate.month<<'/'<<
first.Book[i].b_lenderdate.day<<endl;
}
}
system("pause");
}
bool Tushu::lender()
{
int code;
cout<<" 请输入要还图书的编号!"<<endl;
cin>>code;
if(search(code))
{
int i=Search(code);
first.Book[i].flag=false;
cout<<" 还书成功!"<<endl;
system("pause");
return true;
}
else
{
cout<<"没有改图书!"<<endl;
system("pause");
return false;
}
}
int main()
{
menu();
int k;
cin>>k;
Tushu A;
while(1)
{
switch(k)
{
case 1:A.set();break;
case 2:A.app();break;
case 3:A.del();break;
case 4:A.Sort();break;
case 5:A.search();break;
case 6:A.change();break;
case 7:A.borrow();break;
case 8:A.lender();break;
case 9:A.notice();break;
case 10:A.load();break;
case 11:A.read();break;
case 12:A.display();break;
case 13:exit(0);break;
};
system("cls"); //清屏
menu();
cin>>k;
}
system("pause"); //暂停
return 0;
}