综合使用了数组,指针,结构体的知识
结果截图如下
源代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- 系统名称:图书管理系统
- 系统功能:增加图书 删除图书 查找图书 查看图书 借书 还书 个人借书情况等
- 作者:陈银虎
- 完成时间:2019年11月19日
- */
- 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 < 9; 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, "语文");
-
- strcpy(g_book[3]->name, "思想品德");
- strcpy(g_book[4]->name, "历史");
- strcpy(g_book[5]->name, "马克思主义");
-
- strcpy(g_book[6]->name, "C语言");
- strcpy(g_book[7]->name, "单片机");
- strcpy(g_book[8]->name, "嵌入式");
- g_book[0]->count = 3;
- g_book[1]->count = 5;
- g_book[2]->count = 1;
- g_book[3]->count = 3;
- g_book[4]->count = 5;
- g_book[5]->count = 10;
- g_book[6]->count = 2;
- g_book[7]->count = 15;
- g_book[8]->count = 7;
- }
- /*
- 函数描述:显示图书馆所有书本
- 函数参数:无
- 函数返回值:无
- */
- void ShowBook()
- { printf("\t\t借阅情况:\n");
- printf("\t\t图书馆总藏书数:%d\n",g_BookCount);
- printf("\t\t总借阅人数:%d\n",g_StudentCount);
- 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) ? "可借" : "不可借");
- }
- printf("\n");
- printf("\t\t具体个人的借阅情况请在返回主菜单后到借阅情况\n");
- sleep(4);
- }
- /*
- 函数描述: 查询图书馆的所有书本
- 函数参数: 无
- 函数返回值:无
- */
- void SearchBook()
- { char s[32]={0};
- printf("\t\t输入你要查询的书名:\n");
- scanf("%s",s);
- int i=TraverseBook(s);
- if (i == -1) //未查询到结果
- {
- printf("\t\t图书馆内无此书!\n");
- return;
- }
- else
- {
- if (g_book[i]->flag == 1) //可借
- {
- printf("\t\t可借,还有%d本。\n",g_book[i]->count);
- }
- else
- {
- printf("\t\t此书已被借完!\n"); //不可借
- }
- }
-
- }
-
- /*
- 函数描述:根据id查找学生是否存在
- 函数参数:学号
- 函数返回值:学生存在返回下标 学生不存在返回-1
- */
- int TraverseStudent(int id)
- {
- int i;
- for (i = 0; i < g_StudentCount; i++)
- {
- if (id == g_student[i]->id)
- {
- // printf("存在!"); //调试用
- 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 AddBook()
- {
- printf("\t\t请输入要增加的书籍名称:\n");
- char name[32] = {0};
- int num;
- scanf ("%s",name);
- //判断书籍是否已经存在
- int result = TraverseBook(name);
- if (result != -1)
- {
- printf("\t\t该书已存在,请输入你要增加的数量:\n");
- scanf("%d",&num);
- g_book[result]->count +=num;
- printf("书籍增加成功,该书现在有%d本\n",g_book[result]->count);
- return;
- }
- //书籍不存在,分配空间
- else
- { g_BookCount ++;
- g_book[g_BookCount - 1] = (book *)malloc(sizeof(book)*1);
- if (NULL == g_book[g_BookCount - 1])
- {
- printf("malloc failure!\n");
- }
- g_book[g_BookCount -1]->flag = 1;
- }
- strcpy(g_book[g_BookCount -1]->name,name);
- printf("\t\t请输入要增加的本数:\n");
- scanf ("%d",&num);
- g_book[g_BookCount - 1]->count = num;
- printf("\t\t增加成功,该书现在有%d本\n",num);
- return;
- }
- /*
- 函数描述:删除输入的书目
- 函数参数:无
- 函数返回值:无
- */
- void DeleteBook()
- {
- int i,result=0;
- printf("\t\t请输入要删除的书目:\n");
- char name[32]={0};
- scanf ("%s",name);
- result = TraverseBook(name);
- if (-1 == result)
- {
- printf("\t\t书本不存在,删除失败!\n");
- return ;
- }
- else
- {
- for (i = result;i < g_BookCount;i++)
- {
- g_book[i] = g_book[i+1];
- }
- g_BookCount--;
- printf("\t\t删除成功!\n");
- }
- }
- /*
- 函数描述:借书部分
- 函数参数:无
- 函数返回值:无
- */
- void BorrowBook()
- {
- int id = 0;
- printf("\t\t请输入学号:\n");
- scanf("%d", &id);
- //遍历g_student数组,判断学生是否借过书
- int result = TraverseStudent(id); //判断id是否存在
- int sturesult = result;
- 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");
- char name[32] = {0};
- scanf("%s", name);
- //判断书在不在
- result = TraverseBook(name);
-
- if (-1 == result) //书本不存在
- {
- printf("\t\t书本不存在,借书失败\n");
- return;
- }
- else //书本存在
- {
- if (g_book[result]->flag == 0)
- {
- printf("\t\t该书已经被借完!\n");
-
- }
- else
- {
- g_book[result]->count--;
- if (0 == g_book[result]->count)
- {
- g_book[result]->flag = 0; //书本不可借
- }
- g_student[g_StudentCount - 1]->id = id; //记录学生的学号
- 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
- {
-
- if (g_student[sturesult]->count == 5 )
- {
- printf("借书已达上限!\n");
- return;
- }
- else
- {
- printf("\t\t要借哪本书?\n");
-
- char name1[32]={0};
- scanf("%s",name1);
- //判断书是否存在
- int result1 = TraverseBook(name1);
- {
- if (-1==result1)//书本不存在
- {
- printf("\t\t书本不存在,借书失败!\n");
- return;
- }
- else
- { if (g_book[result1]->flag == 0 )
-
- {
- printf("\t\t该书已经被借完!\n");
- }
- else
-
- { //书本存在
- g_book[result1]->count--;
- if (0 == g_book[result1]->count)
- {
- g_book[result]->flag = 0; //书本不可借
- }
- // printf("\t\t测试段错误用\n");
- //给这个学生的书名分配内存
- g_student[sturesult]->book[g_student[sturesult]->count] = (char*)malloc(sizeof(char)*128);
- if (NULL == g_student[sturesult]->book)
- {
- printf("malloc failure!\n");
- return;
- }
- strcpy(g_student[sturesult]->book[g_student[sturesult]->count],name1);
- /*sturesult: 由于前部分result一开始代表的是学号下标,后来改成了书名下标,这里取学号下标保存 */
- g_student[sturesult]->count++;
- printf("\t\t借书成功!\n");
- }
- }
- }
-
-
- }
- }
-
- }
- /*
- 函数描述:根据学号以及书名确定还书情况
- 函数参数:无
- 函数返回值:无
- */
- void Payback()
- {
- int id,sturesult,bookresult=-1,i;
- char name[32] = {0};
-
- printf("\t\t请输入学号:\n");
- scanf("%d",&id);
-
- //判断学号是否存在
- sturesult = TraverseStudent(id);
- if (sturesult == -1)
- {
- printf("\t\t学号有误!\n");
- return;
- }
- else
- {
- printf("\t\t你要还哪本书?\n");
- scanf ("%s",name);
- //判断书是否存在
- for(i = 0;i < g_student[sturesult]->count;i++)
- { //判断书名与所借书名是否相同
- if (strcmp(name,g_student[sturesult]->book[i]) == 0)
- {
- bookresult = i;
- break;
- }
- }
-
- if ( bookresult == -1)
- {
- printf("\t\t你没有借过这本书!\n");
- }
- else
- {
-
- //学生书名删除
- for (i = bookresult;i<g_student[sturesult]->count;i++)
- {
- g_student[sturesult]->book[i]=g_student[sturesult]->book[i+1];
-
- }
-
- g_student[sturesult]->count--;//学生借书册数减一
- int x = TraverseBook(name);
- g_book[x]->count++; //图书馆书加一
- printf("\t\t还书成功!\n");
- }
-
- }
- }
- /*
- 函数描述:根据输入学号查看该学生的借书情况
- 函数参数:无
- 函数返回值:无
- */
- void Status()
- {
- int id,result,i;
- printf("\t\t请输入你的学号:\n");
- scanf("%d",&id);
- result= TraverseStudent(id);
- if (-1 == result)
- {
- printf("\t\t该学号不存在!\n");
- }
- else
- {
- printf("\t\t借书数量:%d\n",g_student[result]->count);
- for(i = 0;i < g_student[result]->count;i++)
- {
- printf("\t\t书名:%s\n",g_student[result]->book[i]);
- }
- }
- }
- int main()
- {
- char choice[2] = {0};
- init();
- while (1)
- {
- menu();
- scanf("%s", choice);
- switch(choice[0])
- {
- case '1':
- AddBook();
- sleep(4);
- break;
- case '2':
- DeleteBook();
- sleep(4);
- break;
- case '3':
- ShowBook();
- break;
- case '4':
- SearchBook();
- sleep(4);
- break;
- case '5':
- BorrowBook();
- sleep(4);
- break;
- case '6':
- Payback();
- sleep(4);
- break;
- case '7':
- Status();
- sleep(4);
- break;
- case '8':
- exit(0);
- break;
- }
- }
- return 0;
- }
复制代码
|