#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
#define N 20 //可任意设置book结构体里元素的长度
void chaxun();
void xianshi();
int n=0;
int success=1;
int i=1;
struct student
{
int num; //学号
char name[N]; //姓名
char banbie[N]; //班别
char time[N];//借阅时间
};
struct book
{
int times; //记录已经被借阅的次数
int tag; //tag=1表示图书还没借出去,tag=0表示图书已经借出去
int choice;
int dengluhao;
int jiage;
char shuming[N];
char zuozhe[N];
char fenleihao[N];
char chubandanwei[N];
struct student student1;
char chubanshijian[N];
book*next;
}*head=NULL,*p1,*p2,*p,*q;
//建立链表
book *creat()
{
p1=new(book);
cout<<" 请输入要增加的书的登录号(登录号为0时停止输入)"<<endl<<" ";
cin>>p1->dengluhao;
while(p1->dengluhao!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
cout<<" 请输入借书的标记tag(1或者0): "<<endl<<" ";
cin>>p1->tag; //初始输入tag为1,表示图书还没有被借出去
cout<<" 请输入书籍名: "<<endl<<" ";
cin>>p1->shuming;
cout<<" 请输入作者名: "<<endl<<" ";
cin>>p1->zuozhe;
cout<<" 请输入分类号: "<<endl<<" ";
cin>>p1->fenleihao;
cout<<" 请输入出版单位: "<<endl<<" ";
cin>>p1->chubandanwei;
cout<<" 请输入出版时间: "<<endl<<" ";
cin>>p1->chubanshijian;
cout<<" 请输入价格: ";
cin>>p1->jiage;
cout<<" 请输入借阅的次数:"<<endl<<" ";
cin>>p1->times;
p2=p1;
cout<<" 请输入要增加的书的登录号(登录号为0时停止输入)"<<endl<<" ";
p1=new(book);
cin>>p1->dengluhao;
}
delete p1;
p2->next=NULL;
return head;
}
//显示所有图书
void xianshi(book*head)
{
book*p;
p=head;
if(p==NULL)
{
cout<<"书库里没有书"<<endl;
return;
}
else
{cout<<setw(10)<<"登录号"<<setw(10)<<"书名"<<setw(10)
<<"作者名"<<setw(10)<<"分类号"<<setw(10)<<"出版单位"
<<setw(10)<<"出版时间"<<setw(10)<<"价格"<<setw(10)
<<endl<<endl<<endl<<endl;
for(;p!=NULL;)
{
cout<<setw(10)<<p->dengluhao<<setw(10)<<p->shuming<<setw(10)
<<setw(10)<<p->zuozhe<<setw(10)<<p->fenleihao<<setw(10)
<<p->chubandanwei<<setw(10)<<p->chubanshijian<<setw(10)
<<p->jiage<<endl;
p=p->next;
}
}
}
book*search1(int dengluhao) //按登录号查找图书
{
book *p;
if(head==NULL)
{
cout<<" 书库里没有书"<<endl;
return NULL;
}
p=head;
while(dengluhao!=p->dengluhao && p->next!=NULL)
{
p=p->next;
}
if(dengluhao==p->dengluhao)
{
return p;
}
else
{
cout<<" 该书不存在"<<endl;
return NULL;
}
}
book*search2(char shuming[N]) //按书名查找图书
{
book *p;
if(head==NULL)
{
cout<<" 书库里没有书"<<endl;
return NULL;
}
p=head;
while(strcmp(shuming,p->shuming)!=0 && p->next!=NULL)
{
p=p->next;
}
if(strcmp(shuming,p->shuming)==0)
return p;
else
{
cout<<" 该书不存在"<<endl;
return NULL;
}
}
book*search3(char zuozhe[N]) //按作者查找图书
{
book *p;
if(head==NULL)
{
cout<<" 书库里没有书"<<endl;
return NULL;
}
p=head;
while(strcmp(zuozhe,p->zuozhe)!=0 && p->next!=NULL)
{
p=p->next;
}
if(strcmp(zuozhe,p->zuozhe)==0)
return p;
else
{
cout<<" 该书不存在"<<endl;
return NULL;
}
}
//删除图书
book*shanchu(book*head)
{
int dengluhao;
cout<<" 请输入要删除图书的登录号"<<endl<<" ";
cin>>dengluhao;
book *p1,*p2;
if(head==NULL)
{
cout<<" 书库里没有书"<<endl;
success=0;
return head;
}
p1=head;
while(dengluhao!=p1->dengluhao && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(dengluhao==p1->dengluhao)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
success=1;
n--;
xianshi(head);
}
else
{
cout<<" 不存在该登录号的书"<<endl<<endl;
success=0;
}
return head;
}
//显示查找的图书
void display(book*p)
{
cout<<setw(10)<<"登录号"<<setw(10)<<"书名"<<setw(10)
<<"作者名"<<setw(10)<<"分类号"<<setw(10)<<"出版单位"
<<setw(10)<<"出版时间"<<setw(10)<<"价格"<<setw(10)
<<endl<<endl<<endl<<endl;
if(p!=NULL)
cout<<p->dengluhao<<setw(10)<<p->shuming<<setw(10)
<<setw(10)<<p->zuozhe<<setw(10)<<p->fenleihao<<setw(10)
<<p->chubandanwei<<setw(10)<<p->chubanshijian<<setw(10)
<<p->jiage<<endl;
}
//查找图书
void chaxun()
{
int choice;
book *b;
int dengluhao;
cout<<" 查找方式:"<<endl<<"1按登录号查询 2按书名查询 3按作者查询 0退出:";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<" 请输入要查询的登录号:"<<endl<<" ";
cin>>dengluhao;
b=search1(dengluhao);
if (b==NULL)
{
cout<<" 该图书不存在"<<endl;
break;
}
display(b);
}
break;
case 2:
{
char shuming[N];
cout<<" 输入书名:"<<endl<<" ";
cin>>shuming;
b=search2(shuming);
if(b==NULL)
{
cout<<" 该图书不存在"<<endl;
break;
}
display(b);
}
break;
case 3:
{
char zuozhe[N];
cout<<" 输入作者名:"<<endl<<" ";
cin>>zuozhe;
b=search3(zuozhe);
if(b==NULL)
{
cout<<" 该图书不存在"<<endl;
break;
}
display(b);
}
break;
default: cout<<" 输入错误"<<endl;break;
}
}
//借阅图书
book *jieyue(book *head)
{
int num;
if(head==NULL)
{
cout<<" 书库里没有书!"<<endl;
return head;
}
cout<<" 请输入要借阅书的名字:"<<endl<<" ";
char shuming[N];
cin>>shuming;
book *p;
p=head;
while(p!=NULL)
{
if(strcmp(shuming,p->shuming)!=0 && p->next!=NULL)
p=p->next;
else if(strcmp(shuming,p->shuming)==0)
{
display(p);
p=p->next;
}
else
{
cout<<" 不存在此书";
return head;
}
}
cout<<" 请输入借阅书的登录号"<<endl<<" ";
cin>>num;
cout<<" 是否借阅本书:"
<<"1: 确定"<<setw(10)<<"2: 取消"<<endl<<" ";
int a;
int i=1;
while(i)
{
cin>>a;
if(a==1)
{
book *q;
q=head;
while(q!=NULL)
{
if(num!=q->dengluhao && q->next!=NULL)
q=q->next;
else
break;
}
if(q->tag==0)
{
int b;
cout<<" 此书已经被借阅"<<endl;
cout<<" 请问是否查询此书的借阅信息:"<<endl;
cout<<" 1: 是"<<setw(10)<<" 2: 否"<<endl<<" ";
while(1)
{
cin>>b;
if(b==1)
{
cout<<" 借阅此书的学生信息是:"<<endl;
cout<<setw(10)<<"学号"
<<setw(10)<<"姓名"
<<setw(10)<<"班别"
<<setw(10)<<"借阅时间"<<endl;
cout<<setw(10)<<q->student1.num
<<setw(10)<<q->student1.name
<<setw(10)<<q->student1.banbie
<<setw(10)<<q->student1.time<<endl;
break;
}
else if(b==2)
return head;
else
cout<<" 输入有误,请重新输入:";
}
}
else
{
q->tag=0;
(q->times)++;
success=0;
i=0;
cout<<" 请输入你的学号:"<<endl<<" ";
cin>>q->student1.num;
cout<<" 请输入你的姓名:"<<endl<<" ";
cin>>q->student1.name;
cout<<" 请输入你的班别:"<<endl<<" ";
cin>>q->student1.banbie;
cout<<" 请输入你的借阅时间:"<<endl<<" ";
cin>>q->student1.time;
}
i=0;
}
else if(a==2)
return NULL;
else
cout<<" 输入错误,请重新输入:";
}
return head;
}
//归还图书
book *guihuai(book *head)
{
if(head==NULL)
{
cout<<" 书库未借出过任何书籍! "<<endl;
return head;
}
cout<<" 请输入要归还书的登录号:"<<endl<<" ";
int dengluhao;
cin>>dengluhao;
book *p1;
p1=head;
while(dengluhao==p1->dengluhao&& p1->next!=NULL)
{
p1=p1->next;
}
if(dengluhao==p1->dengluhao)
{
cout<<" 确定要归还该书吗?"<<endl
<<" 1.确定"<<'\t'<<" 2.取消"<<endl<<" ";
int num; cin>>num;
switch(num)
{
case 1:
cout<<" 成功归还!"<<endl;
p1->tag=1;break;
case 2: break;
default:cout<<" 输入有误!"<<endl;break;
}
}
else
cout<<"此书不存在"<<endl<<endl;
return head;
}
int main()
{
while(1)
{
cout<<" 欢迎进入广东海洋大学图书馆管理系统"<<endl;
cout<<" **************************** "<<endl;
cout<<" **** 1 添加图书 **** "<<endl;
cout<<" **** 2 删除图书 **** "<<endl;
cout<<" **** 3 图书查询 **** "<<endl;
cout<<" **** 4 显示图书 **** "<<endl;
cout<<" **** 5 借阅图书 **** "<<endl;
cout<<" **** 6 归还图书 **** "<<endl;
cout<<" **** 0 退出 **** "<<endl;
cout<<" ****************************"<<endl<<endl;
cout<<endl<<"请按键选择您需要的操作:"<<endl<<" ";
int xuanzhe2;
cin>>xuanzhe2;
while(1)
{
if(xuanzhe2==1)
{
int choice;
head=creat();
cout<<" 1: 返回主菜单"<<setw(20)
<<" 其他: 继续添加"<<endl<<" ";
cin>>choice;
if(choice==1)
break;
}
else if(xuanzhe2==2)
{
int choice;
head=shanchu(head);
if(success==1)
cout<<" 图书删除成功"<<endl;
cout<<" 1: 返回主菜单"<<setw(20)
<<" 其他: 继续删除"<<endl<<" ";
cin>>choice;
if(choice==1)
break;
}
else if(xuanzhe2==3)
{
int choice;
int dengluhao;
chaxun();
cout<<" 1: 返回主菜单"<<setw(20)
<<" 其他: 继续查询"<<endl<<" ";
cin>>choice;
if(choice==1)
break;
}
else if(xuanzhe2==4)
{
xianshi(head);
break;
}
else if(xuanzhe2==5)
{
head=jieyue(head);
int choice;
cout<<" 1: 返回主菜单"<<setw(10)
<<" 其他: 继续借阅"<<endl<<" ";
cin>>choice;
if(choice==1)
break;
}
else if(xuanzhe2==6)
{
head=guihuai(head);
int choice;
cout<<" 1: 返回主菜单"<<setw(10)
<<" 其他: 继续归还"<<endl<<" ";
cin>>choice;
if(choice==1)
break;
}
else if(xuanzhe2==0)
{
cout<<"您的十分满意,我们无限开心,谢谢使用!"<<endl;
cout<<"版权所有 盗版必究"<<endl;
return 0;
}
else
cout<<" 输入错误,请重新输入"<<endl;
}
}
return 0;
}
1,添加图书
,2
2删除图书
3,图书查询
4,显示图书
5,借阅图书
6,归还图书
0 退出系统