700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > c语言编写的图书馆管理系统

c语言编写的图书馆管理系统

时间:2018-07-13 13:00:29

相关推荐

c语言编写的图书馆管理系统

总共有八个功能:1.借书,2.还书,3.查看图书,4.查询图书,5.增加图书,6.删除图书,7.学生借书情况查询,8.退出系统。

#include <stdio.h>#include <stdlib.h>#include <string.h>#define NAME_LEN 50//定义scanf输入的汉字长度struct Student//定义学生{char name[32];int id;char count; //借书的本数;char book[5][20];//借的哪些书};typedef struct Student stu;stu *g_student[1000];struct Book{char name[32];int count; //剩余本数;};typedef struct Book book;book *g_book[1000];int bookcount = 8; //书的种类int studentcount = 0;//可以借书的人int menu(){printf("菜单\n");printf("********************************************\n");printf("1.借阅书籍 2.归还书籍\n");printf("3.查询书籍 4.查看书籍 \n");printf("5.增加书籍 6.删除书籍 \n");printf("7.学生借书情况查询 8.退出系统\n");printf("*********************************************\n");printf("请输入你的选择\n");int num;while (scanf_s("%d", &num) != 1 || num < 0 || num > 8){printf("你的选择有误,请重新选择\n");system("pause");fflush(stdin);//清空缓冲区}return num;}void Init1(){int i;for (i = 0; i < bookcount; i++){g_book[i] = (book*)malloc(sizeof(book) * 10);if (NULL == g_book[i]){printf("内存申请失败\n");break;}}strcpy_s(g_book[0]->name, "语文");strcpy_s(g_book[1]->name, "数学");strcpy_s(g_book[2]->name, "英语");strcpy_s(g_book[3]->name, "历史");strcpy_s(g_book[4]->name, "地理");strcpy_s(g_book[5]->name, "物理");strcpy_s(g_book[6]->name, "化学");strcpy_s(g_book[7]->name, "生物");g_book[0]->count = 3;g_book[1]->count = 4;g_book[2]->count = 2;g_book[3]->count = 5;g_book[4]->count = 1;g_book[5]->count = 5;g_book[6]->count = 2;g_book[7]->count = 7;}/*void Init2(){int i;for (i = 0; i < studentcount; i++){g_student[i] = (stu*)malloc(sizeof(stu) * 100);if (NULL == g_student[i]){printf("内存申请失败\n");break;}}strcpy_s(g_student[1]->name, "钱二");strcpy_s(g_student[2]->name, "孙三");strcpy_s(g_student[3]->name, "李四");strcpy_s(g_student[4]->name, "周五");strcpy_s(g_student[5]->name, "吴六");strcpy_s(g_student[6]->name, "郑七");strcpy_s(g_student[7]->name, "王八");g_student[0]->id = 10000000;g_student[0]->id = 10000000;g_student[1]->id = 10000001;g_student[2]->id = 10000002;g_student[3]->id = 10000003;g_student[4]->id = 10000004;g_student[5]->id = 10000005;g_student[6]->id = 10000006;g_student[7]->id = 10000007;g_student[0]->count = 0;g_student[1]->count = 0;g_student[2]->count = 0;g_student[3]->count = 0;g_student[4]->count = 0;g_student[5]->count = 0;g_student[6]->count = 0;g_student[7]->count = 0;}*/void tuichu(){system("cls");}void showbook(){int i;for (i = 0; i < bookcount; i++){printf("%d.书名:%s 剩余本数: %d\n",i,g_book[i]->name,g_book[i]->count);}}void refer(){int i;int count = 0;char name[NAME_LEN];printf("请输入你想要查询的书籍\n");scanf_s("%s", name,NAME_LEN);for (i = 0; i < bookcount; i++){if (strcmp(g_book[i]->name, name) == 0){printf("书名为%s的书籍存在且剩余数量还有%d本\n", g_book[i]->name, g_book[i]->count);}else{count++;}}if (count == bookcount){printf("该书不存在\n");}}void increase(){char name[NAME_LEN];int i;printf("请输入你要增加的书籍名字\n");scanf_s("%s", name, NAME_LEN);for (i = 0; i < bookcount; i++){if (strcmp(name, g_book[i]->name) == 0){printf("成功增加书籍\n");printf("%s这本书原来的数量为:%d\n",name,g_book[i]->count);g_book[i]->count++;printf("增加后数量为:%d\n",g_book[i]->count);break;}else{bookcount++;g_book[bookcount - 1] = (book*)malloc(sizeof(book) * 10);strcpy_s(g_book[bookcount - 1]->name, name);g_book[bookcount - 1]->count = 1;printf("成功增加书籍——%s,当前该图书数量为%d\n",name,g_book[bookcount - 1]->count);break;}}}void remove(){char name[NAME_LEN];int i;int count = 0;printf("请输入你要删除的书籍名字\n");scanf_s("%s", name, NAME_LEN);for (i = 0; i < bookcount; i++){if (strcmp(name, g_book[i]->name) == 0){if (g_book[i]->count != 0){printf("删除该书籍成功\n");printf("%s这本书原来的数量为:%d\n", name, g_book[i]->count);g_book[i]->count--;printf("删除后数量为:%d\n", g_book[i]->count);break;}else{printf("该书籍数量已经为0,无法继续删除\n");}}else{count++;}}if (count == bookcount){printf("该书籍不存在\n");}}void borrow(){int id,i,j;int count = 0;char name[NAME_LEN];char bookname[NAME_LEN];printf("请输入你的姓名和学号\n");scanf_s("%s",name,NAME_LEN);scanf_s("%d", &id);if (studentcount == 0){studentcount++;printf("学生信息不存在,为您创建信息存储\n");g_student[studentcount - 1] = (stu*)malloc(sizeof(stu) * 10);strcpy_s(g_student[studentcount - 1]->name, name);g_student[studentcount - 1]->id = id;printf("请输入你想借的书\n");scanf_s("%s",bookname,NAME_LEN);for (j = 0; j < bookcount; j++){if (strcmp(g_book[j]->name, bookname) == 0){if (g_book[j]->count != 0){g_book[j]->count = g_book[j]->count - 1;g_student[studentcount - 1]->count = g_student[studentcount]->count + 1;strcpy_s(g_student[studentcount - 1]->book[0], g_book[j]->name);printf("借书成功\n");break;}else{printf("该书数量为0,不可借\n");break;}}else{count++;}}if (count == bookcount){printf("该书不存在\n");}}else{for (i = 0; i < studentcount; i++){if (id != g_student[i]->id){studentcount++;printf("学生信息不存在,为您创建信息存储\n");g_student[studentcount - 1] = (stu*)malloc(sizeof(stu) * 10);strcpy_s(g_student[studentcount - 1]->name, name);g_student[studentcount - 1]->id = id;printf("请输入你想借的书\n");scanf_s("%s", bookname, NAME_LEN);for (j = 0; j < bookcount; j++){if (strcmp(bookname, g_book[j]->name) == 0){if (g_book[j]->count != 0){g_book[j]->count = g_book[j]->count - 1;g_student[studentcount - 1]->count = g_student[studentcount - 1]->count + 1;strcpy_s(g_student[studentcount - 1]->book[0], g_book[j]->name);printf("借书成功\n");break;}else{printf("该书数量为0,不可借\n");break;}}else{count++;}}if (count == bookcount){printf("该书不能存在\n");}}else{printf("请输入你想借的书\n");scanf_s("%s", bookname, NAME_LEN);for (j = 0; j < bookcount; j++){if (strcmp(bookname, g_book[j]->name) == 0){if (g_book[j]->count != 0){g_book[j]->count = g_book[j]->count - 1;g_student[i]->count = g_student[i]->count + 1;strcpy_s(g_student[i]->book[0], g_book[j]->name);printf("借书成功\n");break;}else{printf("该书数量为0,不可借\n");break;}}else{count++;}}if (count == bookcount){printf("该书不存在\n");}}}}}void gback(){int i, id;int j;char bookname[NAME_LEN];printf("请输入你的学号\n");scanf_s("%d", &id);if (studentcount == 0){printf("学生不存在\n");}else{for (i = 0; i < studentcount; i++){if (id == g_student[i]->id){printf("输入你要还的书名\n");scanf_s("%s", bookname, NAME_LEN);for (j = 0; j < bookcount; j++){if (strcmp(bookname, g_book[j]->name) == 0){g_book[j]->count++;break;}else{printf("书名不存在\n");}}}else{printf("学号不存在,请重新输入\n");}}}}void shows(){int i, id;if (studentcount == 0){printf("该图书馆管理系统目前还未开放使用\n");}else{printf("请输入你的学号\n");scanf_s("%d", &id);for (i = 0; i < studentcount; i++){if (id == g_student[i]->id){//printf("姓名:%s 学号: %d 目前借了 %d 本书",g_student[i]->name, g_student[i]->id, g_student[i]->count);}else{printf("学号不存在\n");}}}}int main(){Init1();//Init2();int quit = 0;while (quit == 0){switch (menu()){case 0:quit = 1; break;case 1:borrow(); break;case 2:gback();break;case 3: refer(); break;case 4: showbook(); break;case 5: increase();break;case 6: remove(); break;case 7: shows(); break;case 8: tuichu(); break;default:break;}system("pause");}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。