找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3398|回复: 1
收起左侧

自己写的C语言图书管理系统程序 综合使用了数组,指针,结构体的知识

[复制链接]
ID:568565 发表于 2019-11-19 10:34 来自手机 | 显示全部楼层 |阅读模式
综合使用了数组,指针,结构体的知识

结果截图如下
Screenshot_20191119-102837.png
源代码如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /*
  5. 系统名称:图书管理系统
  6. 系统功能:增加图书 删除图书 查找图书 查看图书  借书 还书 个人借书情况等
  7. 作者:陈银虎
  8. 完成时间:2019年11月19日
  9. */
  10. struct Book
  11. {
  12.         char name[32];    //书名
  13.         int count;        //本数
  14.         int flag;         //是否可借 1表示可借
  15. };
  16. typedef struct Book book;

  17. struct Student
  18. {
  19.         char name[32];      //学生名
  20.         int id;             //学生学号
  21.         char *book[5];      //已经借的书
  22.         int count;          //借的本数
  23. };
  24. typedef struct Student stu;

  25. book *g_book[1024] = {0};     //图书馆所有书
  26. stu *g_student[1024] = {0};   //图书馆记录的所有借书的学生
  27. int g_BookCount = 0;          //图书馆书的本数
  28. int g_StudentCount = 0;       //借书的人数

  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、退出系统\n");
  37.         printf("\t\t----------------------------------------------\n");
  38. }

  39. /*
  40. 函数描述:图书馆初始化,存放一些图书
  41. 函数参数:无
  42. 函数返回值:无
  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, "数学");
  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. 函数描述:显示图书馆所有书本
  80. 函数参数:无
  81. 函数返回值:无
  82. */
  83. void ShowBook()
  84. {       printf("\t\t借阅情况:\n");
  85.         printf("\t\t图书馆总藏书数:%d\n",g_BookCount);
  86.         printf("\t\t总借阅人数:%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 本数 %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. 函数描述: 查询图书馆的所有书本
  100. 函数参数: 无
  101. 函数返回值:无
  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)                      //未查询到结果
  109.     {
  110.            printf("\t\t图书馆内无此书!\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. 函数描述:根据id查找学生是否存在
  128. 函数参数:学号
  129. 函数返回值:学生存在返回下标 学生不存在返回-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("存在!");        //调试用
  139.                         return i;
  140.                 }
  141.         }

  142.         return -1;
  143. }

  144. /*
  145. 函数描述:根据书名判断书是否存在
  146. 函数参数:书名
  147. 函数返回值:存在则返回书本的下标,不存在返回-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. 函数描述:增加书籍
  163. 函数参数:无
  164. 函数返回值:无
  165. */
  166. void AddBook()
  167. {
  168.    printf("\t\t请输入要增加的书籍名称:\n");
  169.    char name[32] = {0};
  170.    int num;
  171.    scanf ("%s",name);
  172.    //判断书籍是否已经存在
  173.    int result = TraverseBook(name);
  174.    if (result != -1)
  175.    {
  176.      printf("\t\t该书已存在,请输入你要增加的数量:\n");
  177.      scanf("%d",&num);
  178.      g_book[result]->count +=num;
  179.      printf("书籍增加成功,该书现在有%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请输入要增加的本数:\n");
  194.    scanf ("%d",&num);
  195.    g_book[g_BookCount - 1]->count = num;
  196.    printf("\t\t增加成功,该书现在有%d本\n",num);
  197.    return;
  198. }
  199. /*
  200. 函数描述:删除输入的书目
  201. 函数参数:无
  202. 函数返回值:无
  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. 函数描述:借书部分
  228. 函数参数:无
  229. 函数返回值:无
  230. */
  231. void BorrowBook()
  232. {
  233.         int id = 0;

  234.         printf("\t\t请输入学号:\n");
  235.         scanf("%d", &id);

  236.         //遍历g_student数组,判断学生是否借过书
  237.         int result = TraverseStudent(id);  //判断id是否存在
  238.         int sturesult = result;
  239.         if (result == -1)    //不存在,给新的学生申请内存
  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该书已经被借完!\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;    //记录学生的学号
  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++;    //借书的本数加一
  282.                         printf("\t\t借书成功\n");
  283.                 }
  284.         }
  285. }
  286.                //存在 判断借书是否达到上限
  287.               else
  288.               {
  289.                
  290.                 if (g_student[sturesult]->count == 5 )
  291.                 {
  292.                   printf("借书已达上限!\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该书已经被借完!\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.      //给这个学生的书名分配内存                     
  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一开始代表的是学号下标,后来改成了书名下标,这里取学号下标保存   */
  333.                                         g_student[sturesult]->count++;
  334.                                         printf("\t\t借书成功!\n");
  335.                                      }
  336.                 }
  337.                          }
  338.                   
  339.                   
  340.                   }
  341.         }
  342.      
  343. }

  344. /*
  345. 函数描述:根据学号以及书名确定还书情况
  346. 函数参数:无
  347. 函数返回值:无

  348. */

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

  354.      printf("\t\t请输入学号:\n");
  355.      scanf("%d",&id);
  356.       
  357.       //判断学号是否存在
  358.       sturesult = TraverseStudent(id);
  359.       if (sturesult == -1)
  360.       {
  361.         printf("\t\t学号有误!\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.           //学生书名删除
  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--;//学生借书册数减一

  392.          int x = TraverseBook(name);
  393.           g_book[x]->count++;           //图书馆书加一


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


  398. }

  399. /*
  400. 函数描述:根据输入学号查看该学生的借书情况
  401. 函数参数:无
  402. 函数返回值:无
  403. */

  404. void Status()
  405. {
  406.   int id,result,i;
  407.   printf("\t\t请输入你的学号:\n");
  408.   scanf("%d",&id);
  409.   result= TraverseStudent(id);
  410.   if (-1 == result)
  411.   {
  412.      printf("\t\t该学号不存在!\n");
  413.   }
  414.   else
  415.   {
  416.    printf("\t\t借书数量:%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. }
复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:692345 发表于 2020-6-2 15:01 来自手机 | 显示全部楼层
能发源码嘛
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表