|
綜合使用了數(shù)組,指針,結(jié)構(gòu)體的知識(shí)
結(jié)果截圖如下
源代碼如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- 系統(tǒng)名稱(chēng):圖書(shū)管理系統(tǒng)
- 系統(tǒng)功能:增加圖書(shū) 刪除圖書(shū) 查找圖書(shū) 查看圖書(shū) 借書(shū) 還書(shū) 個(gè)人借書(shū)情況等
- 作者:陳銀虎
- 完成時(shí)間:2019年11月19日
- */
- struct Book
- {
- char name[32]; //書(shū)名
- int count; //本數(shù)
- int flag; //是否可借 1表示可借
- };
- typedef struct Book book;
- struct Student
- {
- char name[32]; //學(xué)生名
- int id; //學(xué)生學(xué)號(hào)
- char *book[5]; //已經(jīng)借的書(shū)
- int count; //借的本數(shù)
- };
- typedef struct Student stu;
- book *g_book[1024] = {0}; //圖書(shū)館所有書(shū)
- stu *g_student[1024] = {0}; //圖書(shū)館記錄的所有借書(shū)的學(xué)生
- int g_BookCount = 0; //圖書(shū)館書(shū)的本數(shù)
- int g_StudentCount = 0; //借書(shū)的人數(shù)
- void menu()
- {
- system("clear");
- printf("\t\t-----------------------------------------------\n");
- printf("\t\t 1、增加圖書(shū) 2、刪除圖書(shū)\n");
- printf("\t\t 3、查看圖書(shū) 4、查找圖書(shū)\n");
- printf("\t\t 5、借書(shū) 6、還書(shū)\n");
- printf("\t\t 7、借書(shū)情況 8、退出系統(tǒng)\n");
- printf("\t\t----------------------------------------------\n");
- }
- /*
- 函數(shù)描述:圖書(shū)館初始化,存放一些圖書(shū)
- 函數(shù)參數(shù):無(wú)
- 函數(shù)返回值:無(wú)
- */
- 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, "英語(yǔ)");
- strcpy(g_book[2]->name, "語(yǔ)文");
-
- strcpy(g_book[3]->name, "思想品德");
- strcpy(g_book[4]->name, "歷史");
- strcpy(g_book[5]->name, "馬克思主義");
-
- strcpy(g_book[6]->name, "C語(yǔ)言");
- strcpy(g_book[7]->name, "單片機(jī)");
- 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ù)參數(shù):無(wú)
- 函數(shù)返回值:無(wú)
- */
- void ShowBook()
- { printf("\t\t借閱情況:\n");
- printf("\t\t圖書(shū)館總藏書(shū)數(shù):%d\n",g_BookCount);
- printf("\t\t總借閱人數(shù):%d\n",g_StudentCount);
- printf("\t\t書(shū)本情況:\n");
- int i;
- for (i = 0; i < g_BookCount; i++)
- {
- printf("\t\t 書(shū)名:%s 本數(shù) %d 是否可借 %s\n", g_book[i]->name, g_book[i]->count,
- (g_book[i]->flag == 1) ? "可借" : "不可借");
- }
- printf("\n");
- printf("\t\t具體個(gè)人的借閱情況請(qǐng)?jiān)诜祷刂鞑藛魏蟮浇栝喦闆r\n");
- sleep(4);
- }
- /*
- 函數(shù)描述: 查詢圖書(shū)館的所有書(shū)本
- 函數(shù)參數(shù): 無(wú)
- 函數(shù)返回值:無(wú)
- */
- void SearchBook()
- { char s[32]={0};
- printf("\t\t輸入你要查詢的書(shū)名:\n");
- scanf("%s",s);
- int i=TraverseBook(s);
- if (i == -1) //未查詢到結(jié)果
- {
- printf("\t\t圖書(shū)館內(nèi)無(wú)此書(shū)!\n");
- return;
- }
- else
- {
- if (g_book[i]->flag == 1) //可借
- {
- printf("\t\t可借,還有%d本。\n",g_book[i]->count);
- }
- else
- {
- printf("\t\t此書(shū)已被借完!\n"); //不可借
- }
- }
-
- }
-
- /*
- 函數(shù)描述:根據(jù)id查找學(xué)生是否存在
- 函數(shù)參數(shù):學(xué)號(hào)
- 函數(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ù)參數(shù):書(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ù):無(wú)
- 函數(shù)返回值:無(wú)
- */
- void AddBook()
- {
- printf("\t\t請(qǐng)輸入要增加的書(shū)籍名稱(chēng):\n");
- char name[32] = {0};
- int num;
- scanf ("%s",name);
- //判斷書(shū)籍是否已經(jīng)存在
- int result = TraverseBook(name);
- if (result != -1)
- {
- printf("\t\t該書(shū)已存在,請(qǐng)輸入你要增加的數(shù)量:\n");
- scanf("%d",&num);
- g_book[result]->count +=num;
- printf("書(shū)籍增加成功,該書(shū)現(xiàn)在有%d本\n",g_book[result]->count);
- return;
- }
- //書(shū)籍不存在,分配空間
- 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請(qǐng)輸入要增加的本數(shù):\n");
- scanf ("%d",&num);
- g_book[g_BookCount - 1]->count = num;
- printf("\t\t增加成功,該書(shū)現(xiàn)在有%d本\n",num);
- return;
- }
- /*
- 函數(shù)描述:刪除輸入的書(shū)目
- 函數(shù)參數(shù):無(wú)
- 函數(shù)返回值:無(wú)
- */
- void DeleteBook()
- {
- int i,result=0;
- printf("\t\t請(qǐng)輸入要?jiǎng)h除的書(shū)目:\n");
- char name[32]={0};
- scanf ("%s",name);
- result = TraverseBook(name);
- if (-1 == result)
- {
- printf("\t\t書(shū)本不存在,刪除失!\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ù):無(wú)
- 函數(shù)返回值:無(wú)
- */
- void BorrowBook()
- {
- int id = 0;
- printf("\t\t請(qǐng)輸入學(xué)號(hào):\n");
- scanf("%d", &id);
- //遍歷g_student數(shù)組,判斷學(xué)生是否借過(guò)書(shū)
- int result = TraverseStudent(id); //判斷id是否存在
- int sturesult = result;
- if (result == -1) //不存在,給新的學(xué)生申請(qǐng)內(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要借哪本書(shū):\n");
- char name[32] = {0};
- scanf("%s", name);
- //判斷書(shū)在不在
- result = TraverseBook(name);
-
- if (-1 == result) //書(shū)本不存在
- {
- printf("\t\t書(shū)本不存在,借書(shū)失敗\n");
- return;
- }
- else //書(shū)本存在
- {
- if (g_book[result]->flag == 0)
- {
- printf("\t\t該書(shū)已經(jīng)被借完!\n");
-
- }
- else
- {
- g_book[result]->count--;
- if (0 == g_book[result]->count)
- {
- g_book[result]->flag = 0; //書(shū)本不可借
- }
- g_student[g_StudentCount - 1]->id = id; //記錄學(xué)生的學(xué)號(hào)
- 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); //保存書(shū)名
- g_student[g_StudentCount - 1]->count++; //借書(shū)的本數(shù)加一
- printf("\t\t借書(shū)成功\n");
- }
- }
- }
- //存在 判斷借書(shū)是否達(dá)到上限
- else
- {
-
- if (g_student[sturesult]->count == 5 )
- {
- printf("借書(shū)已達(dá)上限!\n");
- return;
- }
- else
- {
- printf("\t\t要借哪本書(shū)?\n");
-
- char name1[32]={0};
- scanf("%s",name1);
- //判斷書(shū)是否存在
- int result1 = TraverseBook(name1);
- {
- if (-1==result1)//書(shū)本不存在
- {
- printf("\t\t書(shū)本不存在,借書(shū)失!\n");
- return;
- }
- else
- { if (g_book[result1]->flag == 0 )
-
- {
- printf("\t\t該書(shū)已經(jīng)被借完!\n");
- }
- else
-
- { //書(shū)本存在
- g_book[result1]->count--;
- if (0 == g_book[result1]->count)
- {
- g_book[result]->flag = 0; //書(shū)本不可借
- }
- // printf("\t\t測(cè)試段錯(cuò)誤用\n");
- //給這個(gè)學(xué)生的書(shū)名分配內(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一開(kāi)始代表的是學(xué)號(hào)下標(biāo),后來(lái)改成了書(shū)名下標(biāo),這里取學(xué)號(hào)下標(biāo)保存 */
- g_student[sturesult]->count++;
- printf("\t\t借書(shū)成功!\n");
- }
- }
- }
-
-
- }
- }
-
- }
- /*
- 函數(shù)描述:根據(jù)學(xué)號(hào)以及書(shū)名確定還書(shū)情況
- 函數(shù)參數(shù):無(wú)
- 函數(shù)返回值:無(wú)
- */
- void Payback()
- {
- int id,sturesult,bookresult=-1,i;
- char name[32] = {0};
-
- printf("\t\t請(qǐng)輸入學(xué)號(hào):\n");
- scanf("%d",&id);
-
- //判斷學(xué)號(hào)是否存在
- sturesult = TraverseStudent(id);
- if (sturesult == -1)
- {
- printf("\t\t學(xué)號(hào)有誤!\n");
- return;
- }
- else
- {
- printf("\t\t你要還哪本書(shū)?\n");
- scanf ("%s",name);
- //判斷書(shū)是否存在
- for(i = 0;i < g_student[sturesult]->count;i++)
- { //判斷書(shū)名與所借書(shū)名是否相同
- if (strcmp(name,g_student[sturesult]->book[i]) == 0)
- {
- bookresult = i;
- break;
- }
- }
-
- if ( bookresult == -1)
- {
- printf("\t\t你沒(méi)有借過(guò)這本書(shū)!\n");
- }
- else
- {
-
- //學(xué)生書(shū)名刪除
- 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ū)冊(cè)數(shù)減一
- int x = TraverseBook(name);
- g_book[x]->count++; //圖書(shū)館書(shū)加一
- printf("\t\t還書(shū)成功!\n");
- }
-
- }
- }
- /*
- 函數(shù)描述:根據(jù)輸入學(xué)號(hào)查看該學(xué)生的借書(shū)情況
- 函數(shù)參數(shù):無(wú)
- 函數(shù)返回值:無(wú)
- */
- void Status()
- {
- int id,result,i;
- printf("\t\t請(qǐng)輸入你的學(xué)號(hào):\n");
- scanf("%d",&id);
- result= TraverseStudent(id);
- if (-1 == result)
- {
- printf("\t\t該學(xué)號(hào)不存在!\n");
- }
- else
- {
- printf("\t\t借書(shū)數(shù)量:%d\n",g_student[result]->count);
- for(i = 0;i < g_student[result]->count;i++)
- {
- printf("\t\t書(shū)名:%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ù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|