// i.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include //陶杰
#include
#include
#include
void main();
void main_1();
void main_2();
typedef struct BookMessage
{
char BookNo[10] ;
char BookName[30];
char Writer[30];
char Bookpubhouse[30];
int BookNum;
char User[30];
struct BookMessage *next;
}bookmessage,*BookList;
typedef struct user
{
char UserID[10];
char UserPassword[10];
char BookOne[10];
char BookTwo[10];
int BookNum;
struct user *next;
}User,*UserList;
bookmessage *start=(BookList)malloc(sizeof(BookMessage));
User *head=(UserList)malloc(sizeof(User));
//图书管理
void InsertBook(/*BookList &start*/) //新增图书
{
bookmessage *s=start;
while(s->next!=NULL)
s=s->next;
bookmessage *s1=(BookList)malloc(sizeof(BookMessage));
printf("输入图书序号:\n");
scanf("%s",s1->BookNo);
printf("输入图书名:\n");
scanf("%s",s1->BookName);
printf("输入图书作者:\n");
scanf("%s",s1->Writer);
printf("输入图书数量:\n");
scanf("%d",&s1->BookNum);
printf("输入图书出版社:\n");
scanf("%s",s1->Bookpubhouse);
s1->next=NULL;
s->next=s1;
}
void AppendBookNum(/*BookList &start*/) //修改图书数目
{
bookmessage *s=start;
int n,c;
printf("输入图书名或书序号:\n");
printf("1:书序号查找: 2:书名查找: \n");
char bookno[20],bookname[20];
scanf("%d",&c);
if(c==1)
{
printf("输入书号:\n");
scanf("%s",bookno);
while(s!=NULL)
{
if(!strcmp(s->BookNo,bookno))
{
printf("现有图书的数量:%d\n",s->BookNum);
printf("输入增加的本数:\n");
scanf("%d",&n);
s->BookNum=n+s->BookNum;
printf("现有图书的数量:%d\n",s->BookNum);
return;
}
else s=s->next;
}
if(s==NULL) printf("不存在此书\n");
}
else if(c==2)
{
printf("输入书名:\n");
scanf("%s",bookname);
while(s!=NULL)
{
if(!strcmp(s->BookName,bookname))
{
printf("现有图书的数量:%d\n",s->BookNum);
printf("输入增加的本数:\n");
scanf("%d",&n);
s->BookNum=s->BookNum+n;
return;
}
else s=s->next;
}
if(s==NULL) printf("不存在此书\n");
}
else
printf("选择错误\n");
}
void Setuser(/*UserList &head*/) //新增用户
{
user *p=head;
while(p->next!=NULL)
p=p->next;
user *s=(UserList)malloc(sizeof(User));
printf("输入用户ID:\n");
scanf("%s",s->UserID);
printf("输入用户密码:\n");
scanf("%s",s->UserPassword);
strcpy(s->BookOne,"");
strcpy(s->BookTwo,"");
s->BookNum=0;
s->next=NULL;
p->next=s;
}
void SearchUser(/*UserList &head*/) //查找用户
{
user *p=head;
printf("输入帐号:");
char id[10];
scanf("%s",id);
while(p!=NULL)
{
if(!strcmp(p->UserID,id))
{
if(p->BookNum==0)
printf("帐号:%s\n该用户未借书\n",p->UserID); // 该用户未借书
else
printf("帐号:%s,书名:%s\t%s\n",p->UserID,p->BookOne, p->BookTwo);
return;
}
else p=p->next;
}
if(p==NULL) printf("不存在该用户\n");
}
void DisplayAllUser(/*UserList &head*/) //显示所有用户
{
User *p=head->next;
while(p!=NULL)
{
if(p->BookNum==0)
{
printf("%s\n",p->UserID);
}//用户未借书
else if (p->BookNum==1)
{
if (p->BookOne=="")
{
printf("帐号:%s,书名:%s\n",p->UserID,p->BookTwo);
}
else
{
printf("帐号:%s,书名:%s\n",p->UserID,p->BookOne);
}
}
else
{
printf("%s,%s,%s\n",p->UserID,p->BookOne, p->BookTwo);
}
p=p->next;
}
}
void DelUser(/*UserList &head*/) //删除用户
{
printf("请输入要删除的用户帐号:");
char id[10];
scanf("%s",id);
user *p=head,*j;
if(!strcmp(p->next->UserID,id) && p->next->BookNum==0)
{
if(p->next->next==NULL)
{
free(p->next);
p->next=NULL;
return;
}
else
{
j=p->next;
p->next=j->next;
free(j);
return;
}
}
else if(!strcmp(p->next->UserID,id) && p->next->BookNum!=0)
printf("用户有书未还 \n"); //用户有书未还
else
{
printf("error");
return;
}
}
//图书借阅
void SearchBook (/*BookList &start*/) //检索图书
{
BookMessage *s=start;
printf("请输入书名:");
char name[30];
scanf("%s",name);
while(s!=NULL)
{
if(!strcmp(s->BookName,name))
{
printf("图书序号:%s,图书名:%s,图书作者:%s,\n图书出版社:%s,图书数量:%d\n",
s->BookNo, s->BookName,s->Writer,s->Bookpubhouse,s->BookNum);
return;
}
else s=s->next;
}
if (s==NULL) printf("不存在该书\n"); // 没有所需图书
}
void BorrowBooks(/*UserList &head,BookList &start*/) //图书借阅
{
User *p=head;
BookMessage *s=start;
char id[10],password[10];
printf("输入帐号:");
scanf("%s",id);
printf("输入密码:");
scanf("%s",password);
while(p!=NULL)
{
if(!strcmp(p->UserID,id) && !strcmp(p->UserPassword,password))
{
char book[30];
if(p->BookNum {
printf("输入图书序号或书名:\n");
scanf("%s",book);
while(s!=NULL)
{
if(!strcmp(s->BookNo,book) || !strcmp(s->BookName,book))
{
if(s->BookNum==0)
{
printf("该书全借出\n");
return;
} //该书全借出
else
{
s->BookNum=(s->BookNum)-1;
if(p->BookNum==0)
strcpy(p->BookOne,s->BookNo);
else if(p->BookNum==1 && p->BookOne=="")
strcpy(p->BookOne,s->BookNo);
else
strcpy(p->BookTwo,s->BookNo);
p->BookNum+=1;
return;
}
}
else s=s->next;
}
if(s==NULL)
{
printf("无此书\n");
return;
} // 无此书
}
}
else p=p->next;
}
if(p==NULL)
printf("用户ID错误\n"); //用户ID错误
}
void ReturnBook(/*UserList &head,BookList &start*/) //归还图书
{
BookMessage *s=start;
user *p=head;
char id[10],bookno[10];
printf("输入帐号:\n");
scanf("%s",id);
while(p!=NULL)
{
if(!strcmp(p->UserID,id))
{
printf("输入书序号或书名:\n");
scanf("%s",bookno);
while(s!=NULL)
{
if(!strcmp(s->BookNo,bookno))
{
if(p->BookNum>0)
{
if(!strcmp(p->BookOne,bookno))
{
strcpy(p->BookOne,"");
p->BookNum-=1;
s->BookNum+=1;
}
else if(!strcmp(p->BookTwo,bookno))
{
strcpy(p->BookTwo,"");
p->BookNum-=1;
s->BookNum+=1;
}
}
else
{
printf("用户未借书\n");
return;
} //用户未借书
}
else s=s->next;
}
if(s==NULL)
{
printf("书号错误\n");
return;
} //书号错误
}
else p=p->next;
}
if(p==NULL)
printf("用户ID错误\n"); //用户ID错误
}
void main_1()
{
int sell;
do
{
printf(">>1:新增图书 2:修改图书数量 3:新增用户 4:搜索用户 5:显示用户资料 \n 6:删除用户 0:返回上级\n");
scanf("%d",&sell);
switch (sell)
{
case 1:InsertBook(/*bookmessage*/);break; /*新增图书*/
case 2:AppendBookNum(/*BookMessage*/);break; /*修改图书数量*/
case 3:Setuser(/*User*/);break; /*新增用户*/
case 4:SearchUser(/*User*/);break; /*搜索用户*/
case 5:DisplayAllUser(/*User*/);break; /*显示用户资料*/
case 6:DelUser(/*User*/);break; /*删除用户*/
case 0:return;
default:printf("\n**选择错误\n");break;
}
}while(sell!=0);
}
void main_2()
{
int sell;
do
{
printf(">>1:检索图书 2:归还图书 3:借阅图书 0:返回上级\n");
scanf("%d",&sell);
switch (sell)
{
case 1:SearchBook(/*BookMessage*/);break; /*检索图书*/
case 2:ReturnBook(/*User,BookMessage*/);break; /*归还图书*/
case 3:BorrowBooks(/*User,BookMessage*/);break; /*借阅图书*/
case 0:return;
default:printf("\n**选择错误\n");break;
}
}while(sell!=0);
}
void main()
{
head->next=NULL;
start->next=NULL;
int sell;
do
{
printf(">>1:图书管理系统: 2:图书借阅系统: 3:清屏 0:退出\n");
scanf("%d",&sell);
switch (sell)
{
case 1:main_1();break; /*图书管理系统*/
case 2:main_2();break; /*图书借阅系统*/
case 3:
{
system("cls");
break;
}
case 0:break; /*退出*/
default:printf("\n**选择错误\n");break;
}
}while(sell!=0);
}