找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4233|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

自己寫的C語言圖書管理系統(tǒng)程序 綜合使用了數(shù)組,指針,結(jié)構(gòu)體的知識

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:568565 發(fā)表于 2019-11-19 10:34 來自手機 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
綜合使用了數(shù)組,指針,結(jié)構(gòu)體的知識

結(jié)果截圖如下

源代碼如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /*
  5. 系統(tǒng)名稱:圖書管理系統(tǒng)
  6. 系統(tǒng)功能:增加圖書 刪除圖書 查找圖書 查看圖書  借書 還書 個人借書情況等
  7. 作者:陳銀虎
  8. 完成時間:2019年11月19日
  9. */
  10. struct Book
  11. {
  12.         char name[32];    //書名
  13.         int count;        //本數(shù)
  14.         int flag;         //是否可借 1表示可借
  15. };
  16. typedef struct Book book;

  17. struct Student
  18. {
  19.         char name[32];      //學(xué)生名
  20.         int id;             //學(xué)生學(xué)號
  21.         char *book[5];      //已經(jīng)借的書
  22.         int count;          //借的本數(shù)
  23. };
  24. typedef struct Student stu;

  25. book *g_book[1024] = {0};     //圖書館所有書
  26. stu *g_student[1024] = {0};   //圖書館記錄的所有借書的學(xué)生
  27. int g_BookCount = 0;          //圖書館書的本數(shù)
  28. int g_StudentCount = 0;       //借書的人數(shù)

  29. void menu()
  30. {
  31.         system("clear");

  32.         printf("\t\t-----------------------------------------------\n");
  33.         printf("\t\t  1、增加圖書                    2、刪除圖書\n");
  34.         printf("\t\t  3、查看圖書                    4、查找圖書\n");
  35.         printf("\t\t  5、借書                        6、還書\n");
  36.         printf("\t\t  7、借書情況                    8、退出系統(tǒng)\n");
  37.         printf("\t\t----------------------------------------------\n");
  38. }

  39. /*
  40. 函數(shù)描述:圖書館初始化,存放一些圖書
  41. 函數(shù)參數(shù):無
  42. 函數(shù)返回值:無
  43. */
  44. void init()
  45. {
  46.         int i;
  47.         for (i = 0; i < 9; i++)
  48.         {
  49.                 g_book[i] = (book *)malloc(sizeof(book) * 1);
  50.                 if (NULL == g_book[i])
  51.                 {
  52.                         printf("mallco failrue!\n");
  53.                 }
  54.                 g_book[i]->flag = 1;    //表示可借
  55.                 g_BookCount++;
  56.         }

  57.         strcpy(g_book[0]->name, "數(shù)學(xué)");
  58.         strcpy(g_book[1]->name, "英語");
  59.         strcpy(g_book[2]->name, "語文");
  60.         
  61.         strcpy(g_book[3]->name, "思想品德");
  62.         strcpy(g_book[4]->name, "歷史");
  63.         strcpy(g_book[5]->name, "馬克思主義");
  64.         
  65.         strcpy(g_book[6]->name, "C語言");
  66.         strcpy(g_book[7]->name, "單片機");
  67.         strcpy(g_book[8]->name, "嵌入式");


  68.         g_book[0]->count = 3;
  69.         g_book[1]->count = 5;
  70.         g_book[2]->count = 1;

  71.         g_book[3]->count = 3;
  72.         g_book[4]->count = 5;
  73.         g_book[5]->count = 10;

  74.         g_book[6]->count = 2;
  75.         g_book[7]->count = 15;
  76.         g_book[8]->count = 7;
  77. }
  78. /*
  79. 函數(shù)描述:顯示圖書館所有書本
  80. 函數(shù)參數(shù):無
  81. 函數(shù)返回值:無
  82. */
  83. void ShowBook()
  84. {       printf("\t\t借閱情況:\n");
  85.         printf("\t\t圖書館總藏書數(shù):%d\n",g_BookCount);
  86.         printf("\t\t總借閱人數(shù):%d\n",g_StudentCount);
  87.         printf("\t\t書本情況:\n");
  88.         int i;
  89.         for (i = 0; i < g_BookCount; i++)
  90.         {
  91.                 printf("\t\t 書名:%s 本數(shù) %d 是否可借 %s\n", g_book[i]->name, g_book[i]->count,
  92.                                                 (g_book[i]->flag == 1) ? "可借" : "不可借");
  93.         }
  94.         printf("\n");
  95.         printf("\t\t具體個人的借閱情況請在返回主菜單后到借閱情況\n");
  96.         sleep(4);
  97. }
  98. /*
  99. 函數(shù)描述: 查詢圖書館的所有書本
  100. 函數(shù)參數(shù): 無
  101. 函數(shù)返回值:無
  102. */
  103. void SearchBook()

  104. {  char s[32]={0};
  105.   printf("\t\t輸入你要查詢的書名:\n");
  106.   scanf("%s",s);
  107.   int i=TraverseBook(s);
  108.     if (i == -1)                      //未查詢到結(jié)果
  109.     {
  110.            printf("\t\t圖書館內(nèi)無此書!\n");
  111.            return;
  112.         }
  113.         else
  114.         {
  115.      if (g_book[i]->flag == 1)       //可借
  116.      {
  117.        printf("\t\t可借,還有%d本。\n",g_book[i]->count);
  118.      }
  119.          else
  120.             {
  121.                 printf("\t\t此書已被借完!\n");  //不可借
  122.                 }
  123.     }
  124.    

  125. }


  126. /*
  127. 函數(shù)描述:根據(jù)id查找學(xué)生是否存在
  128. 函數(shù)參數(shù):學(xué)號
  129. 函數(shù)返回值:學(xué)生存在返回下標(biāo) 學(xué)生不存在返回-1
  130. */
  131. int TraverseStudent(int id)
  132. {
  133.         int i;
  134.         for (i = 0; i < g_StudentCount; i++)
  135.         {
  136.                 if (id == g_student[i]->id)
  137.                 {        
  138. //                  printf("存在!");        //調(diào)試用
  139.                         return i;
  140.                 }
  141.         }

  142.         return -1;
  143. }

  144. /*
  145. 函數(shù)描述:根據(jù)書名判斷書是否存在
  146. 函數(shù)參數(shù):書名
  147. 函數(shù)返回值:存在則返回書本的下標(biāo),不存在返回-1
  148. */
  149. int TraverseBook(char *n)
  150. {
  151.         int i;
  152.         for (i = 0; i < g_BookCount; i++)
  153.         {
  154.                 if (strcmp(n, g_book[i]->name) == 0)
  155.                 {
  156.                         return i;
  157.                 }
  158.         }

  159.         return -1;
  160. }
  161. /*
  162. 函數(shù)描述:增加書籍
  163. 函數(shù)參數(shù):無
  164. 函數(shù)返回值:無
  165. */
  166. void AddBook()
  167. {
  168.    printf("\t\t請輸入要增加的書籍名稱:\n");
  169.    char name[32] = {0};
  170.    int num;
  171.    scanf ("%s",name);
  172.    //判斷書籍是否已經(jīng)存在
  173.    int result = TraverseBook(name);
  174.    if (result != -1)
  175.    {
  176.      printf("\t\t該書已存在,請輸入你要增加的數(shù)量:\n");
  177.      scanf("%d",&num);
  178.      g_book[result]->count +=num;
  179.      printf("書籍增加成功,該書現(xiàn)在有%d本\n",g_book[result]->count);
  180.      return;
  181.    }

  182.    //書籍不存在,分配空間
  183.    else
  184.    {   g_BookCount ++;
  185.      g_book[g_BookCount - 1] = (book *)malloc(sizeof(book)*1);
  186.      if (NULL == g_book[g_BookCount - 1])
  187.      {
  188.      printf("malloc failure!\n");
  189.      }
  190.      g_book[g_BookCount -1]->flag = 1;

  191.    }
  192.    strcpy(g_book[g_BookCount -1]->name,name);
  193.    printf("\t\t請輸入要增加的本數(shù):\n");
  194.    scanf ("%d",&num);
  195.    g_book[g_BookCount - 1]->count = num;
  196.    printf("\t\t增加成功,該書現(xiàn)在有%d本\n",num);
  197.    return;
  198. }
  199. /*
  200. 函數(shù)描述:刪除輸入的書目
  201. 函數(shù)參數(shù):無
  202. 函數(shù)返回值:無
  203. */
  204. void DeleteBook()
  205. {  
  206.   int i,result=0;
  207.   printf("\t\t請輸入要刪除的書目:\n");
  208.   char name[32]={0};
  209.   scanf ("%s",name);
  210.   result = TraverseBook(name);
  211.   if (-1 == result)
  212.     {
  213.       printf("\t\t書本不存在,刪除失!\n");
  214.       return ;
  215.     }
  216.     else
  217.     {
  218.       for (i = result;i < g_BookCount;i++)
  219.       {
  220.        g_book[i] = g_book[i+1];
  221.       }
  222.      g_BookCount--;
  223.      printf("\t\t刪除成功!\n");
  224.     }

  225. }
  226. /*
  227. 函數(shù)描述:借書部分
  228. 函數(shù)參數(shù):無
  229. 函數(shù)返回值:無
  230. */
  231. void BorrowBook()
  232. {
  233.         int id = 0;

  234.         printf("\t\t請輸入學(xué)號:\n");
  235.         scanf("%d", &id);

  236.         //遍歷g_student數(shù)組,判斷學(xué)生是否借過書
  237.         int result = TraverseStudent(id);  //判斷id是否存在
  238.         int sturesult = result;
  239.         if (result == -1)    //不存在,給新的學(xué)生申請內(nèi)存
  240.         {
  241.                 g_StudentCount ++;
  242.                  g_student[g_StudentCount - 1] = (stu *)malloc(sizeof(stu) * 1);
  243.                 if (NULL == g_student[g_StudentCount - 1])
  244.                 {
  245.                         printf("malloc fialure!\n");
  246.                         return;
  247.                 }

  248.                 printf("\t\t要借哪本書:\n");
  249.                 char name[32] = {0};

  250.                 scanf("%s", name);

  251.                 //判斷書在不在
  252.                 result = TraverseBook(name);
  253.                
  254.                 if (-1 == result)   //書本不存在
  255.                 {
  256.                         printf("\t\t書本不存在,借書失敗\n");
  257.                         return;
  258.                 }
  259.                 else          //書本存在
  260.                 {  
  261.                    if (g_book[result]->flag == 0)
  262.                    {
  263.                    printf("\t\t該書已經(jīng)被借完!\n");
  264.                   
  265.                    }
  266.                     else
  267.                         {
  268.                         g_book[result]->count--;
  269.                         if (0 == g_book[result]->count)
  270.                         {
  271.                                 g_book[result]->flag = 0;    //書本不可借
  272.                         }
  273.                         g_student[g_StudentCount - 1]->id = id;    //記錄學(xué)生的學(xué)號
  274.                         g_student[g_StudentCount - 1]->book[g_student[g_StudentCount - 1]->count] = (char *)malloc(sizeof(char) * 128);
  275.                         if (NULL == g_student[g_StudentCount - 1]->book)
  276.                         {
  277.                                 printf("malloc failure!\n");
  278.                                 return;
  279.                         }
  280.                         strcpy(g_student[g_StudentCount - 1]->book[g_student[g_StudentCount - 1]->count], name);   //保存書名
  281.                         g_student[g_StudentCount - 1]->count++;    //借書的本數(shù)加一
  282.                         printf("\t\t借書成功\n");
  283.                 }
  284.         }
  285. }
  286.                //存在 判斷借書是否達(dá)到上限
  287.               else
  288.               {
  289.                
  290.                 if (g_student[sturesult]->count == 5 )
  291.                 {
  292.                   printf("借書已達(dá)上限!\n");
  293.                   return;
  294.                 }
  295.                   else
  296.                   {
  297.                      printf("\t\t要借哪本書?\n");
  298.                         
  299.                          char name1[32]={0};
  300.                          scanf("%s",name1);
  301.                          //判斷書是否存在
  302.                          int result1 = TraverseBook(name1);
  303.                          {
  304.                             if (-1==result1)//書本不存在
  305.                                 {
  306.                                    printf("\t\t書本不存在,借書失敗!\n");
  307.                                    return;
  308.                                 }
  309.                              else
  310.                                      {    if (g_book[result1]->flag == 0  )
  311.                                                       
  312.                                                  {
  313.                                                     printf("\t\t該書已經(jīng)被借完!\n");                  
  314.                                                   }
  315.                                             else
  316.                                             
  317.                                             {              //書本存在
  318.                                       g_book[result1]->count--;
  319.                                           if (0 == g_book[result1]->count)
  320.                                           {
  321.                                            g_book[result]->flag = 0;  //書本不可借
  322.                                           }
  323.            // printf("\t\t測試段錯誤用\n");
  324.      //給這個學(xué)生的書名分配內(nèi)存                     
  325.     g_student[sturesult]->book[g_student[sturesult]->count] = (char*)malloc(sizeof(char)*128);
  326.     if (NULL == g_student[sturesult]->book)
  327.     {
  328.       printf("malloc failure!\n");
  329.       return;
  330.     }
  331.     strcpy(g_student[sturesult]->book[g_student[sturesult]->count],name1);  
  332.                                         /*sturesult: 由于前部分result一開始代表的是學(xué)號下標(biāo),后來改成了書名下標(biāo),這里取學(xué)號下標(biāo)保存   */
  333.                                         g_student[sturesult]->count++;
  334.                                         printf("\t\t借書成功!\n");
  335.                                      }
  336.                 }
  337.                          }
  338.                   
  339.                   
  340.                   }
  341.         }
  342.      
  343. }

  344. /*
  345. 函數(shù)描述:根據(jù)學(xué)號以及書名確定還書情況
  346. 函數(shù)參數(shù):無
  347. 函數(shù)返回值:無

  348. */

  349. void Payback()
  350. {
  351.    int id,sturesult,bookresult=-1,i;
  352.    char name[32] = {0};
  353.   

  354.      printf("\t\t請輸入學(xué)號:\n");
  355.      scanf("%d",&id);
  356.       
  357.       //判斷學(xué)號是否存在
  358.       sturesult = TraverseStudent(id);
  359.       if (sturesult == -1)
  360.       {
  361.         printf("\t\t學(xué)號有誤!\n");
  362.          return;
  363.       }
  364.       else
  365.       {
  366.        printf("\t\t你要還哪本書?\n");
  367.        scanf ("%s",name);
  368.        //判斷書是否存在
  369.        for(i = 0;i < g_student[sturesult]->count;i++)
  370.         {  //判斷書名與所借書名是否相同
  371.          if (strcmp(name,g_student[sturesult]->book[i]) == 0)
  372.          {
  373.            bookresult = i;
  374.            break;
  375.          }

  376.         }

  377.          if ( bookresult == -1)
  378.          {
  379.           printf("\t\t你沒有借過這本書!\n");
  380.          }
  381.          else
  382.          {
  383.          
  384.           //學(xué)生書名刪除
  385.           for (i = bookresult;i<g_student[sturesult]->count;i++)
  386.           {
  387.             g_student[sturesult]->book[i]=g_student[sturesult]->book[i+1];
  388.          
  389.           }
  390.       
  391.           g_student[sturesult]->count--;//學(xué)生借書冊數(shù)減一

  392.          int x = TraverseBook(name);
  393.           g_book[x]->count++;           //圖書館書加一


  394.           printf("\t\t還書成功!\n");
  395.          }
  396.       
  397.       }


  398. }

  399. /*
  400. 函數(shù)描述:根據(jù)輸入學(xué)號查看該學(xué)生的借書情況
  401. 函數(shù)參數(shù):無
  402. 函數(shù)返回值:無
  403. */

  404. void Status()
  405. {
  406.   int id,result,i;
  407.   printf("\t\t請輸入你的學(xué)號:\n");
  408.   scanf("%d",&id);
  409.   result= TraverseStudent(id);
  410.   if (-1 == result)
  411.   {
  412.      printf("\t\t該學(xué)號不存在!\n");
  413.   }
  414.   else
  415.   {
  416.    printf("\t\t借書數(shù)量:%d\n",g_student[result]->count);
  417.    for(i = 0;i < g_student[result]->count;i++)
  418.    {
  419.      printf("\t\t書名:%s\n",g_student[result]->book[i]);
  420.    }


  421.   }


  422. }

  423. int main()
  424. {
  425.         char choice[2] = {0};
  426.         init();

  427.         while (1)
  428.         {
  429.                 menu();        
  430.                 scanf("%s", choice);

  431.                 switch(choice[0])
  432.                 {
  433.                         case '1':
  434.                               AddBook();
  435.                               sleep(4);
  436.                                 break;
  437.                         case '2':
  438.                                DeleteBook();
  439.                                sleep(4);
  440.                                 break;
  441.                         case '3':
  442.                                 ShowBook();
  443.                                 break;
  444.                         case '4':
  445.                             SearchBook();
  446.                                 sleep(4);
  447.                                 break;
  448.                         case '5':
  449.                                 BorrowBook();
  450.                                 sleep(4);
  451.                                 break;
  452.                         case '6':
  453.                                 Payback();
  454.                                 sleep(4);
  455.                                 break;
  456.                         case '7':
  457.                                 Status();
  458.                                 sleep(4);
  459.                                 break;
  460.                         case '8':
  461.                                 exit(0);
  462.                                 break;
  463.                 }
  464.         }

  465.         return 0;
  466. }
復(fù)制代碼

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:692345 發(fā)表于 2020-6-2 15:01 來自手機 | 只看該作者
能發(fā)源碼嘛
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表