|
綜合使用了數(shù)組,指針,結(jié)構(gòu)體的知識
結(jié)果截圖如下
Screenshot_20191119-102837.png (160.55 KB, 下載次數(shù): 82)
下載附件
2019-11-19 10:34 上傳
源代碼如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- 系統(tǒng)名稱:圖書管理系統(tǒng)
- 系統(tǒng)功能:增加圖書 刪除圖書 查找圖書 查看圖書 借書 還書 個人借書情況等
- 作者:陳銀虎
- 完成時間:2019年11月19日
- */
- struct Book
- {
- char name[32]; //書名
- int count; //本數(shù)
- int flag; //是否可借 1表示可借
- };
- typedef struct Book book;
- struct Student
- {
- char name[32]; //學(xué)生名
- int id; //學(xué)生學(xué)號
- char *book[5]; //已經(jīng)借的書
- int count; //借的本數(shù)
- };
- typedef struct Student stu;
- book *g_book[1024] = {0}; //圖書館所有書
- stu *g_student[1024] = {0}; //圖書館記錄的所有借書的學(xué)生
- int g_BookCount = 0; //圖書館書的本數(shù)
- int g_StudentCount = 0; //借書的人數(shù)
- 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、退出系統(tǒng)\n");
- printf("\t\t----------------------------------------------\n");
- }
- /*
- 函數(shù)描述:圖書館初始化,存放一些圖書
- 函數(shù)參數(shù):無
- 函數(shù)返回值:無
- */
- 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, "數(shù)學(xué)");
- 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;
- }
- /*
- 函數(shù)描述:顯示圖書館所有書本
- 函數(shù)參數(shù):無
- 函數(shù)返回值:無
- */
- void ShowBook()
- { printf("\t\t借閱情況:\n");
- printf("\t\t圖書館總藏書數(shù):%d\n",g_BookCount);
- printf("\t\t總借閱人數(shù):%d\n",g_StudentCount);
- printf("\t\t書本情況:\n");
- int i;
- for (i = 0; i < g_BookCount; i++)
- {
- printf("\t\t 書名:%s 本數(shù) %d 是否可借 %s\n", g_book[i]->name, g_book[i]->count,
- (g_book[i]->flag == 1) ? "可借" : "不可借");
- }
- printf("\n");
- printf("\t\t具體個人的借閱情況請在返回主菜單后到借閱情況\n");
- sleep(4);
- }
- /*
- 函數(shù)描述: 查詢圖書館的所有書本
- 函數(shù)參數(shù): 無
- 函數(shù)返回值:無
- */
- void SearchBook()
- { char s[32]={0};
- printf("\t\t輸入你要查詢的書名:\n");
- scanf("%s",s);
- int i=TraverseBook(s);
- if (i == -1) //未查詢到結(jié)果
- {
- printf("\t\t圖書館內(nèi)無此書!\n");
- return;
- }
- else
- {
- if (g_book[i]->flag == 1) //可借
- {
- printf("\t\t可借,還有%d本。\n",g_book[i]->count);
- }
- else
- {
- printf("\t\t此書已被借完!\n"); //不可借
- }
- }
-
- }
-
- /*
- 函數(shù)描述:根據(jù)id查找學(xué)生是否存在
- 函數(shù)參數(shù):學(xué)號
- 函數(shù)返回值:學(xué)生存在返回下標(biāo) 學(xué)生不存在返回-1
- */
- int TraverseStudent(int id)
- {
- int i;
- for (i = 0; i < g_StudentCount; i++)
- {
- if (id == g_student[i]->id)
- {
- // printf("存在!"); //調(diào)試用
- return i;
- }
- }
- return -1;
- }
- /*
- 函數(shù)描述:根據(jù)書名判斷書是否存在
- 函數(shù)參數(shù):書名
- 函數(shù)返回值:存在則返回書本的下標(biāo),不存在返回-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;
- }
- /*
- 函數(shù)描述:增加書籍
- 函數(shù)參數(shù):無
- 函數(shù)返回值:無
- */
- void AddBook()
- {
- printf("\t\t請輸入要增加的書籍名稱:\n");
- char name[32] = {0};
- int num;
- scanf ("%s",name);
- //判斷書籍是否已經(jīng)存在
- int result = TraverseBook(name);
- if (result != -1)
- {
- printf("\t\t該書已存在,請輸入你要增加的數(shù)量:\n");
- scanf("%d",&num);
- g_book[result]->count +=num;
- printf("書籍增加成功,該書現(xiàn)在有%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請輸入要增加的本數(shù):\n");
- scanf ("%d",&num);
- g_book[g_BookCount - 1]->count = num;
- printf("\t\t增加成功,該書現(xiàn)在有%d本\n",num);
- return;
- }
- /*
- 函數(shù)描述:刪除輸入的書目
- 函數(shù)參數(shù):無
- 函數(shù)返回值:無
- */
- 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");
- }
- }
- /*
- 函數(shù)描述:借書部分
- 函數(shù)參數(shù):無
- 函數(shù)返回值:無
- */
- void BorrowBook()
- {
- int id = 0;
- printf("\t\t請輸入學(xué)號:\n");
- scanf("%d", &id);
- //遍歷g_student數(shù)組,判斷學(xué)生是否借過書
- int result = TraverseStudent(id); //判斷id是否存在
- int sturesult = result;
- if (result == -1) //不存在,給新的學(xué)生申請內(nèi)存
- {
- 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該書已經(jīng)被借完!\n");
-
- }
- else
- {
- g_book[result]->count--;
- if (0 == g_book[result]->count)
- {
- g_book[result]->flag = 0; //書本不可借
- }
- g_student[g_StudentCount - 1]->id = id; //記錄學(xué)生的學(xué)號
- 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++; //借書的本數(shù)加一
- printf("\t\t借書成功\n");
- }
- }
- }
- //存在 判斷借書是否達(dá)到上限
- else
- {
-
- if (g_student[sturesult]->count == 5 )
- {
- printf("借書已達(dá)上限!\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該書已經(jīng)被借完!\n");
- }
- else
-
- { //書本存在
- g_book[result1]->count--;
- if (0 == g_book[result1]->count)
- {
- g_book[result]->flag = 0; //書本不可借
- }
- // printf("\t\t測試段錯誤用\n");
- //給這個學(xué)生的書名分配內(nèi)存
- 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一開始代表的是學(xué)號下標(biāo),后來改成了書名下標(biāo),這里取學(xué)號下標(biāo)保存 */
- g_student[sturesult]->count++;
- printf("\t\t借書成功!\n");
- }
- }
- }
-
-
- }
- }
-
- }
- /*
- 函數(shù)描述:根據(jù)學(xué)號以及書名確定還書情況
- 函數(shù)參數(shù):無
- 函數(shù)返回值:無
- */
- void Payback()
- {
- int id,sturesult,bookresult=-1,i;
- char name[32] = {0};
-
- printf("\t\t請輸入學(xué)號:\n");
- scanf("%d",&id);
-
- //判斷學(xué)號是否存在
- sturesult = TraverseStudent(id);
- if (sturesult == -1)
- {
- printf("\t\t學(xué)號有誤!\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
- {
-
- //學(xué)生書名刪除
- for (i = bookresult;i<g_student[sturesult]->count;i++)
- {
- g_student[sturesult]->book[i]=g_student[sturesult]->book[i+1];
-
- }
-
- g_student[sturesult]->count--;//學(xué)生借書冊數(shù)減一
- int x = TraverseBook(name);
- g_book[x]->count++; //圖書館書加一
- printf("\t\t還書成功!\n");
- }
-
- }
- }
- /*
- 函數(shù)描述:根據(jù)輸入學(xué)號查看該學(xué)生的借書情況
- 函數(shù)參數(shù):無
- 函數(shù)返回值:無
- */
- void Status()
- {
- int id,result,i;
- printf("\t\t請輸入你的學(xué)號:\n");
- scanf("%d",&id);
- result= TraverseStudent(id);
- if (-1 == result)
- {
- printf("\t\t該學(xué)號不存在!\n");
- }
- else
- {
- printf("\t\t借書數(shù)量:%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;
- }
復(fù)制代碼
|
評分
-
查看全部評分
|