#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book
{
char name[32]; //书名
int count; //本数
int flag; //是否可借 1表示可借
};
typedef struct Book book;
struct Student
{
char name[32]; //学生名
int id; //学生学号
char *book[5]; //已经借的书
int count; //借的本数
};
typedef struct Student stu;
book *g_book[1024] = {0}; //图书馆所有书
stu *g_student[1024] = {0}; //图书馆记录的所有借书的学生
int g_BookCount = 0; //图书馆书的本数
int g_StudentCount = 0; //借书的人数
void menu()
{
system("clear");
printf("\t\t-----------------------------------------------\n");
printf("\t\t 1、增加图书 2、删除图书\n");
printf("\t\t 3、查看图书 4、查找图书\n");
printf("\t\t 5、借书 6、还书\n");
printf("\t\t 7、借书情况 8、退出系统\n");
printf("\t\t----------------------------------------------\n");
}
/*
函数描述:图书馆初始化,存放一些图书
函数参数:无
函数返回值:无
*/
void init()
{
int i;
for (i = 0; i < 3; i++)
{
g_book[i] = (book *)malloc(sizeof(book) * 1);
if (NULL == g_book[i])
{
printf("mallco failrue!\n");
}
g_book[i]->flag = 1; //表示可借
g_BookCount++;
}
strcpy(g_book[0]->name, "数学");
strcpy(g_book[1]->name, "英语");
strcpy(g_book[2]->name, "语文");
g_book[0]->count = 3;
g_book[1]->count = 5;
g_book[2]->count = 1;
}
/*
函数描述:增加书本
*/
void AddBook()
{
printf("\t\t请输入书名\n");
char n[32] = {0};
scanf("%s",n);
printf("\t\t请输入本数\n");
int num;
scanf("%d",&num);
int res = TraverseBook(n);
if (-1 != res)
{
g_book[res]->count = g_book[res]->count + num;
}
else
{
g_BookCount++;
g_book[g_BookCount - 1] = (book *)malloc(sizeof(book) * 1);
if (NULL == g_book[g_BookCount - 1])
{
printf("malloc fialure!\n");
return;
}
strcpy(g_book[g_BookCount - 1]->name,n);
g_book[g_BookCount - 1]->count = g_book[g_BookCount - 1]->count + num;
g_book[g_BookCount - 1]->flag = 1;
}
}
//删除书本
void DeleteBook()
{
int i;
printf("\t\t请输入要删除的书名\n");
char n[32] = {0};
scanf("%s",n);
int res = TraverseBook(n);
if(-1 == res)
{
printf("\t\t没有这本数,请先添加后删除\n");
return;
}
else
{
for (i = res; i < g_BookCount - 1; i++)
{
g_book[i] = g_book[i + 1];
}
g_BookCount--;
}
}
/*
函数描述:显示图书馆所有学生借书情况
*/
void ShowStudent()
{
printf("\t\t借书情况:\n");
int i;
int j;
for (i = 0; i < g_StudentCount; i++)
{
printf("\t\t姓名:%s\n", g_student[i]->name);
printf("\t\t学号:%d\n",g_student[i]->id);
printf("\t\t已借的书: ");
for (j = 0; j < g_student[i]->count; j++)
{
printf("%s ",g_student[i]->book[j]);
}
printf("\n");
printf("\t\t借书本数:%d\n",g_student[i]->count);
}
}
/*
函数描述:显示图书馆所有书本
*/
void ShowBook()
{
printf("\t\t书本情况:\n");
int i;
for (i = 0; i < g_BookCount; i++)
{
printf("\t\t 书名:%s 本数 %d 是否可借 %s\n", g_book[i]->name, g_book[i]->count,
(g_book[i]->flag == 1) ? "可借" : "不可借");
}
}
/*
函数描述:根据id查找学生是否存在
函数参数:学号
函数返回值:学生存在返回下标 学生不存在返回-1
*/
int TraverseStudent(int id)
{
int i;
for (i = 0; i < g_StudentCount; i++)
{
if (id == g_student[i]->id)
{
return i;
}
}
return -1;
}
/*
函数描述:根据书名判断书是否存在
函数参数:书名
函数返回值:存在则返回书本的下标,不存在返回-1
*/
int TraverseBook(char *n)
{
int i;
for (i = 0; i < g_BookCount; i++)
{
if (strcmp(n, g_book[i]->name) == 0)
{
return i;
}
}
return -1;
}
//查找书本
void ReachBook()
{
printf("\t\t请输入书名\n");
char n[32] = {0};
scanf("%s",n);
int res = TraverseBook(n);
if (-1 == res)
{
printf("\t\t没有此书\n");
return;
}
else
{
printf("\t\t 查找情况\n");
printf("\t\t 书名:%s 本数 %d 是否可借 %s\n", g_book[res]->name, g_book[res]->count,
(g_book[res]->flag == 1) ? "可借" : "不可借");
}
}
//借书
void BorrowBook()
{
int id = 0;
printf("\t\t请输入学号:\n");
scanf("%d", &id);
//遍历g_student数组,判断学生是否借过书
int result = TraverseStudent(id); //判断id是否存在
if (result == -1) //不存在,给新的学生申请内存
{
g_StudentCount++;
g_student[g_StudentCount - 1] = (stu *)malloc(sizeof(stu) * 1);
if (NULL == g_student[g_StudentCount - 1])
{
printf("malloc fialure!\n");
return;
}
printf("\t\t请输入姓名\n");
scanf("%s",g_student[g_StudentCount - 1]->name);
printf("\t\t要借哪本书:\n");
char name[32] = {0};
scanf("%s", name);
//判断书在不在
result = TraverseBook(name);
g_student[g_StudentCount - 1]->id = id; //记录学生的学号
if (-1 == result) //书本不存在
{
printf("\t\t书本不存在,结束失败\n");
return;
}
else //书本存在
{
if (g_book[result]->flag == 0)
{
printf("\t\t此书已被借光\n");
return;
}
g_book[result]->count--;
if (0 == g_book[result]->count)
{
g_book[result]->flag = 0; //书本不可借
}
g_student[g_StudentCount - 1]->book[g_student[g_StudentCount - 1]->count] = (char *)malloc(sizeof(char) * 128);
if (NULL == g_student[g_StudentCount - 1]->book)
{
printf("malloc failure!\n");
return;
}
strcpy(g_student[g_StudentCount - 1]->book[g_student[g_StudentCount - 1]->count], name); //保存书名
g_student[g_StudentCount - 1]->count++; //借书的本数加一
printf("\t\t借书成功\n");
}
}
else //存在 判断结束是否达到上限
{
int result1 = TraverseStudent(id); //判断id是否存在
printf("\t\t要借哪本书:\n");
char name[32] = {0};
scanf("%s", name);
//判断书在不在
result = TraverseBook(name);
if (-1 == result) //书本不存在
{
printf("\t\t书本不存在,结束失败\n");
return;
}
else //书本存在
{
g_book[result]->count--;
if (0 == g_book[result]->count)
{
g_book[result]->flag = 0; //书本不可借
}
g_student[result1]->book[g_student[result1]->count] = (char *)malloc(sizeof(char) * 128);
if (NULL == g_student[result1]->book)
{
printf("malloc failure!\n");
return;
}
strcpy(g_student[result1]->book[g_student[result1]->count], name); //保存书名
g_student[result1]->count++; //借书的本数加一
printf("\t\t借书成功\n");
//g_BookCount--;
}
}
}
//还书
void ReturnBook()
{
printf("\t\t请输入学号\n");
int num;
scanf("%d",&num);
int res = TraverseStudent(num);
if (-1 == res)
{
printf("\t\t无借阅记录\n");
return;
}
else
{
printf("\t\t请输入要还的书名\n");
char n[32] = {0};
scanf("%s",n);
int i;
int j;
for (i = 0; i < g_student[res]->count; i++)
{
if (strcmp(n,g_student[res]->book[i]) == 0)
{
for(j = i; j < g_student[res]->count - 1; j++)
{
g_student[res]->book[j] = g_student[res]->book[j + 1];
}
g_book[TraverseBook(n)]->count++;
g_book[TraverseBook(n)]->flag = 1;
g_student[res]->count--;
printf("\t\t还书成功\n");
}
}
}
}
int main()
{
char choice[2] = {0};
init();
while (1)
{
menu();
scanf("%s", choice);
switch(choice[0])
{
case '1':
AddBook();
printf("\n按任意键返回\n");
getchar();
getchar();
break;
case '2':
DeleteBook();
printf("\n按任意键返回\n");
getchar();
getchar();
break;
case '3':
ShowBook();
printf("\n按任意键返回\n");
getchar();
getchar();
break;
case '4':
ReachBook();
printf("\n按任意键返回\n");
getchar();
getchar();
break;
case '5':
BorrowBook();
printf("\n按任意键返回\n");
getchar();
getchar();
break;
case '6':
ReturnBook();
printf("\n按任意键返回\n");
getchar();
getchar();
break;
case '7':
ShowStudent();
printf("\n按任意键返回\n");
getchar();
getchar();
break;
case '8':
exit(0);
break;
}
}
return 0;
}