#include
#include
#include
#include
#include
using namespace std;
#define Max 4 //定义最大借书量为3本/人
typedef struct book
{
char book_num[10];
char book_name[20];
char book_writer[10];
char book_fl[10];
char book_publisher[20];
char book_time[20];
char book_price[20];
int book_xy; //现存量
int book_kc; //库存量
struct book *next;
} BK;
typedef struct borrow //借书信息
{
char borrow_book_num[20];
char limit_date[10];
} BO;
typedef struct reader //读者信息
{
char reader_num[20];
char reader_name[20];
int right;
BO borrow[Max];
struct reader *next;
} RD;
extern BK *h_book;
extern RD *h_reader;
int menu(); //主菜单
void menu_select(); //主菜单选择函数
void chushihua(); //初始化
void add_book(); //新书入库
void add_reader(); //读者登记
void borrow_book(); //借书
void return_book(); //还书
void print_reader(); //显示读者信息
void print_book(); //显示图书信息
void save(); //保存
void save_reader(); //保存读者信息
void save_book(); //保存图书信息
void load(); //加载
void load_reader(); //加载读者信息
void load_book(); //加载图书信息
BK *h_book;
RD *h_reader;
int Menu() /*主菜单*/
{
int n;
system(“color 1F”);
cout<<"===============================================\n";
cout<<" 安徽科技学院图书管理系统 \n";
cout<<"===============================================\n";
cout<<"-------------------- 1添加新书 ----------------\n";
cout<<"-------------------- 2读者登记 ----------------\n";
cout<<"-------------------- 3借阅图书 ----------------\n";
cout<<"-------------------- 4归还图书 ----------------\n";
cout<<"-------------------- 5浏览图书信息 ------------\n";
cout<<"-------------------- 6浏览读者信息 ------------\n";
cout<<"-------------------- 7退出图书管理系统 --------\n";
cout<<"===============================================\n";
cout<<"请选择菜单的数字:";
for(;;)
{
cin>>n; //输入数字
if(n<1||n>7)
cout<<"输入错误!请重新输入:";
else break;
}
return n;
}
void menu_select() //主菜单选择函数
{
for(;;) //循环调用
{
switch(Menu()) //功能选择
{
case 0:
system("cls");
save();
printf("\n\n\t文件保存成功!\n");
printf("\n\n\t欢迎下次使用本系统!\n");
getch();
exit(0);
case 1:
add_book(); //新书入库
break;
case 2:
add_reader(); //读者登记
break;
case 3:
borrow_book(); //借书
break;
case 4:
return_book(); //还书
break;
case 5:
print_book(); //显示图书信息
break;
case 6:
print_reader(); //显示读者信息
break;
case 7:
system("cls"); //清屏
cout<
cout<<"图书名称:";
cin>>p0->book_name;
cout<<"图书作者:";
cin>>p0->book_writer;
cout<<"图书分类号:";
cin>>p0->book_fl;
cout<<"图书出版单位:";
cin>>p0->book_publisher;
cout<<"图书出版时间:";
cin>>p0->book_time;
cout<<"图书价格:";
cin>>p0->book_price;
cout<<"图书数量:";
cin>>p0->book_kc;
p0->book_xy=p0->book_kc; //图书现有量=库存量
p0->next=NULL;
cout<<"图书信息初始化完毕!按任意键继续下一步操作!";
getch();
system("cls");
}
void add_book() //新书入库
{
BK *p,*p0,*p1;
p=p1=h_book;
cout<<"\n添加图书模块...\n";
cout<<"\n请输入添加的新书信息......\n";
p0=new book;
cout<<"图书编号:";
cin>>p0->book_num;
while(strcmp(p0->book_num,p1->book_num)!=0&&p1->next!=NULL)
p1=p1->next;
if(strcmp(p0->book_num,p1->book_num)==0) //此处分两种情况,若图书编号存在,则直接入库,只须输入书的数量
{
cout<<"\n此种图书已存在!!!"<
p1->book_kc+=p0->book_kc;
p1->book_xy+=p0->book_kc;
}
else //若不存在,则需要输入其他的信息,然后在进行插入操作
{
cout<<"图书名称:";
cin>>p0->book_name;
cout<<"图书作者:";
cin>>p0->book_writer;
cout<<"图书分类号:";
cin>>p0->book_fl;
cout<<"图书出版单位:";
cin>>p0->book_publisher;
cout<<"图书出版时间:";
cin>>p0->book_time;
cout<<"图书价格:";
cin>>p0->book_price;
cout<<"图书数量:";
cin>>p0->book_kc;//库存数量
while(p->next)
{
p=p->next;
}
if(h_book==NULL) h_book=p0; //此处分两种情况,链表中没有数据,head直接指向p0处
else p->next=p0; //链表中有数据,链表中最后元素的next指向p0处
p0->next=NULL;
p0->book_xy=p0->book_kc;
}
cout<<"\n添加完毕!!!\n";
getch();
system("cls");
}
void add_reader() //添加读者
{
RD *p0;
int i;
cout<<"\n读者初始化,请输入读者信息!\n";
p0=new reader; //申请新结点存储空间
h_reader=p0;
cout<<"请输入读者学号:";
cin>>p0->reader_num;
cout<<"请输入读者姓名:";
cin>>p0->reader_name;
p0->right=0;
for(i=0; i
strcpy(p0->borrow[i].limit_date,”0″);
}
p0->next=NULL;
cout<<"\n读者信息初始化完毕!!!\n";
getch();
system("cls");
}
void borrow_book() //借书
{
BK *p0;
RD *p1;
char bo_num[20],rea_num[20],lim_date[10];
int i;
p0=h_book;
p1=h_reader;
cout<<"\n借书模块...\n";
cout<<"\n请输入借书的读者学号:";
cin>>rea_num;//读者学号
while(p1->next!=NULL&&strcmp(rea_num,p1->reader_num)!=0) //判断读者是否存在
p1=p1->next;
if(p1->next==NULL&&strcmp(rea_num,p1->reader_num)!=0)
{
cout<<"\n此读者学号不存在!按任意键返回!\n";
goto END;
}
cout<<"\n请输入你要借的书的编号:";
cin>>bo_num;
while(strcmp(bo_num,p0->book_num)!=0&&p0->next!=NULL)
p0=p0->next;
if(p0->next==NULL&&strcmp(bo_num,p0->book_num)!=0) //判断是否存在读者要借的书
{
cout<<"\n此图书编号不存在!按任意键返回!\n";
goto END;
}
else if(p0->book_xy<=0) //判断所借书是否还有
{
cout<<"\n抱歉,此书已借完!请等待新书的到来!!\n按任意键返回....";
goto END;
}
else if(p1->right>Max||p1->right==Max) //判断最大借书量
{
cout<<"\n不好意思,您的借书数目已满!不能继续借书!\n按任意键返回....";
goto END;
}
else if(strcmp(p1->borrow[0].borrow_book_num,”0″)!=0)
{
for(i=0; i
{
cout<<"\n抱歉!同一个读者不能同借两本相同的书!\n按任意键返回!";
goto END;
}
else if(strcmp(p1->borrow[i].borrow_book_num,”0″)==0)
{
cout<<"\n请输入你要归还图书的日期:";
cin>>lim_date;
strcpy(p1->borrow[p1->right++].borrow_book_num,bo_num);
strcpy(p1->borrow[p1->right-1].limit_date,lim_date);
p0->book_xy–;
cout<<"\n读者学号";
cout<
cout<<"借书完毕!按任意键继续!";
goto END;
}
}
}
else //读者从未借过书
{
cout<<"\n请输入归还日期:";
cin>>lim_date;
strcpy(p1->borrow[p1->right++].borrow_book_num,bo_num);
strcpy(p1->borrow[p1->right-1].limit_date ,lim_date );
p0->book_xy–;
//p0->book_kc–;
cout<<"\n读者学号";
cout<
cout<<"借书完毕!按任意键继续下步操作!";
}
END: //end
getch();
system("cls");
}
void return_book() //还书
{
BK *p;
RD *q;
int i,j,find=0;
char return_book_num[20],return_reader_num[20];
p=h_book;
q=h_reader;
cout<<"\n还书模块...\n";
cout<<"\n请输入要还书的读者学号:";
cin>>return_reader_num;
while(q->next!=NULL&&strcmp(return_reader_num,q->reader_num)!=0) //判断是否存在该读者
q=q->next;
if(q->next==NULL&&strcmp(return_reader_num,q->reader_num)!=0)
{
find=2;
cout<<"\n此读者学号不存在!按任意键返回!\n";
goto end;
}
cout<<"\n请输入读者所还书的编号:";
cin>>return_book_num;
while(p->next!=NULL&&strcmp(return_book_num,p->book_num)!=0)
p=p->next;
if(p->next==NULL&&strcmp(return_book_num,p->book_num)!=0)//判断是否存在要借的书
{
find=2;
cout<<"\n错误!此图书编号不存在!按任意键返回!\n";
goto end;
}
for(i=0; i
{
find=1;
for(j=i; j
strcpy(q->borrow[j].limit_date,q->borrow[j+1].limit_date);
}
strcpy(q->borrow[Max-1].borrow_book_num,”0″);
strcpy(q->borrow[Max-1].limit_date,”0″);
p->book_xy++;
q->right–;
cout<<"\n学号";
cout<
cout<<"\t";
cout<
cout<<"\t";
cout<
cout<<"\t";
cout<
cout<<"\t";
cout<
cout<<"\t";
cout<
cout<<"\t";
cout<
cout<<"\t";
cout<
}
cout<<"\n图书信息显示完毕!按任意键继续下一步操作..";
getch();
system("cls");
}
void print_reader() //查询读者信息
{
RD *p;
int i;
p=h_reader;
system("color 4");
cout<<"\n读者信息如下:\n\n";
cout<<"--------------------------------------------------------------------------------"<
cout<
printf(“\t\t%s”,p->borrow[i].limit_date);
}
printf(“\n”);
p=p->next;
}
cout<<"--------------------------------------------------------------------------------"<
free(p0); //释放所有结点
}
h_reader=NULL;
fclose(fp_reader); //关闭文件
}
void save_book() //保存图书信息
{
FILE *fp_book; //创建文件型指针
BK *p,*p0;
p=h_book;
if((fp_book=fopen(“Book.txt”,”wb”))==NULL) //创建文件,进行保存
{
printf(“\n文件保存失败!\n请重新启动本系统…\n”);
exit(0);
}
while(p!=NULL)
{
if(fwrite(p,sizeof(BK),1,fp_book)!=1) //将链表中的信息写入文件中
printf(“\n写入文件失败!\n请重新启动本系统!\n”);
p0=p;
p=p->next;
free(p0);
}
h_book=NULL;
fclose(fp_book); //关闭文件
}
void load() //加载信息
{
load_reader();
load_book();
}
void load_reader() //加载读者信息
{
RD *p1,*p2,*p3;
FILE *fp; //创建文件型指针
fp=fopen(“book.txt”,”rb”); //打开文件
p1=(RD *)malloc(sizeof(RD));
fread(p1,sizeof(RD),1,fp);
h_reader=p3=p2=p1;
while(! feof(fp)) //读出信息,重新链入链表
{
p1=(RD *)malloc(sizeof(RD));
fread(p1,sizeof(RD),1,fp);
p2->next=p1;
p3=p2;
p2=p1;
}
p3->next=NULL;
free(p1);
fclose(fp); //关闭文件
}
void load_book() //加载图书信息
{
BK *p1,*p2,*p3;
FILE *fp; //创建文件型指针
fp=fopen(“Book.txt”,”rb”); //打开文件
p1=(BK *)malloc(sizeof(BK));
fread(p1,sizeof(BK),1,fp);
h_book=p3=p2=p1;
while(! feof(fp)) //读出信息,重新链入链表
{
p1=(BK *)malloc(sizeof(BK));
fread(p1,sizeof(BK),1,fp);
p2->next=p1;
p3=p2;
p2=p1;
}
p3->next=NULL;
free(p1);
fclose(fp); //关闭文件
}
int main()
{
FILE *fp_book,*fp_reader; /*创建文件型指针*/
if((fp_book=fopen(“Book.txt”,”rb”))==NULL||(fp_reader=fopen(“Reader.txt”,”rb”))==NULL)
{
chushihua();
}
else load();
menu_select(); //调用主菜单x选择函数
}