有戏

 找回密码
 立即注册
简历下载
搜索
热搜: 活动 交友 discuz
查看: 1694|回复: 0

图书管理系统

[复制链接]

89

主题

102

帖子

1万

积分

网站编辑

Rank: 8Rank: 8

积分
18232
发表于 2016-1-18 17:51:28 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct
  5. {
  6.          char  book_id[20];           //书号、
  7.          char book_name[40];        //书名、
  8.          char publish_date[20];     // 出版日期、
  9.          char press[40];          //出版社
  10.          char author[40];           // 作者

  11. }Information;


  12. Information *book = NULL;
  13. int current = 0;
  14. int total = 0;

  15. int main()
  16. {
  17.           char choice;
  18.          //显示程序基本信息
  19.           printf("        图书信息管理系统\n");
  20.          while(1)
  21.           {
  22.                   printf("\n");
  23.                   printf("                   程序功能\n");
  24.                   printf("                1:录入图书信息\n");
  25.                   printf("                2:保存图书信息\n");
  26.                   printf("                3:查询图书信息\n");
  27.                   printf("                4:删除图书信息\n");
  28.                   printf("\n");

  29.                 printf("        请选择:");
  30.                   choice = getch();
  31.                   printf("\n\n");

  32.                 switch(choice)
  33.                   {
  34.                           case '1':
  35.                                   insert();
  36.                                   break;

  37.                         case '2':
  38.                                    save();
  39.                                    break;


  40.                         case '3':
  41.                                   query();
  42.                                   break;

  43.                         case '4':
  44.                                   delete();
  45.                                   break;


  46.                         case '0':
  47.                   if (book != NULL)
  48.                   {
  49.                       free(book);
  50.                   }
  51.                                   return 0;

  52.                         default:
  53.                                   printf("非法选择,请重新选择\n");
  54.                   }
  55.           }

  56.         return 0;    //程序返回
  57. }

  58. int insert()
  59. {

  60.     while(total <= 0)
  61.       {
  62.           printf("请输入要图书的学生总数:");
  63.           scanf("%d", &total);
  64.       }

  65.      if (book == NULL)
  66.           book = (Information *)malloc(total*sizeof(Information));
  67.       if (book == NULL)
  68.       {
  69.           printf("分配失败\n");
  70.           return 1;
  71.       }

  72.          if (current >= total)
  73.           {


  74.           Information *tmp = (Information *)malloc(current*sizeof(Information));

  75.             memmove(tmp,book,current*sizeof(Information));

  76.                 total += 10;
  77.                   realloc(book,total*sizeof(Information));

  78.         memmove(book,tmp,current*sizeof(Information));
  79.           free(tmp);

  80.         }

  81.          printf("请输入书号:");
  82.           scanf("%s", book[current].book_id);
  83.           printf("请输入书名:");
  84.           scanf("%s", book[current].book_name);
  85.           printf("请输入出版日期:");
  86.           scanf("%s", book[current].publish_date);
  87.           printf("请输入出版社:");
  88.           scanf("%s", book[current].press);
  89.           printf("请输入作者:");
  90.           scanf("%s", book[current].author);

  91.         current++;

  92.         return 0;



  93. }


  94. int save()
  95. {

  96.         int count;
  97.           FILE *fs;

  98.         fs = fopen("BookData.dat", "wb");

  99.         if (fs == NULL)
  100.           {
  101.                   printf("不能打开文件\n");
  102.                   return 1;
  103.           }

  104.         count = fwrite(book, sizeof(Information), current, fs);

  105.         fclose(fs);

  106.         if (count != current)
  107.           {
  108.                   printf("保存失败\n");
  109.                   return 1;
  110.           }
  111.           printf("保存成功\n");

  112.         return 0;

  113. }

  114. int query()
  115. {
  116.                   char query_choice;
  117.                  printf("\n");
  118.                   printf("     选择查询关键字(模糊)查询\n");
  119.                   printf("                1:书号\n");
  120.                   printf("                2:书名\n");
  121.                   printf("                3:出版社\n");
  122.                   printf("\n");

  123.                 printf("        请选择:");
  124.                   query_choice = getch();
  125.                   printf("\n\n");

  126.                 switch(query_choice)
  127.                   {
  128.                           case '1':
  129.                                   query_id();
  130.                                   break;

  131.                         case '2':
  132.                                    query_bookname();
  133.                                    break;


  134.                         case '3':
  135.                                   query_publish();
  136.                                   break;

  137.                         default:
  138.                                   printf("非法选择,请重新选择\n");
  139.                   }
  140. }



  141. int query_id()
  142. {


  143.      char id[40];
  144.           int i;

  145.         printf("请输入要查询的书号:");
  146.           scanf("%s", id);

  147.         for (i=0; i<current; i++)
  148.           {
  149.                   if (strcmp( book[i].book_id, id ) == 0)
  150.                   {
  151.                           printf("该图书信息如下\n");
  152.                           printf("书号:%s\n", book[i].book_id);
  153.                           printf("书名:%s\n", book[i].book_name);
  154.                           printf("出版日期:%s\n", book[i].publish_date);
  155.                          printf("出版社:%s\n", book[i].press);
  156.                          printf("作者:%s\n", book[i].author);
  157.                          return 0;
  158.                   }
  159.           }

  160.         printf("该图书不存在!\n\n");
  161.           return 1;
  162. }

  163.   int  query_bookname()
  164. {


  165.      char name[40];
  166.           int i;

  167.         printf("请输入要查询的书名:");
  168.           scanf("%s", name);

  169.         for (i=0; i<current; i++)
  170.           {
  171.                   if (strcmp(book[i].book_name, name) == 0)
  172.                   {
  173.                           printf("该图书信息如下\n");
  174.                           printf("书号:%s\n", book[i].book_id);
  175.                           printf("书名:%s\n", book[i].book_name);
  176.                           printf("出版日期:%s\n", book[i].publish_date);
  177.                          printf("出版社:%s\n", book[i].press);
  178.                          printf("作者:%s\n", book[i].author);
  179.                          return 0;
  180.                   }
  181.           }

  182.         printf("该图书不存在!\n\n");
  183.           return 1;
  184. }

  185. int  query_publish()
  186. {


  187.      char press1[40];
  188.           int i;

  189.         printf("请输入要查询的出版社:");
  190.           scanf("%s", press1);

  191.         for (i=0; i<current; i++)
  192.           {
  193.                   if (strcmp(book[i].press, press1) == 0)
  194.                   {
  195.                           printf("该图书信息如下\n");
  196.                           printf("书号:%s\n", book[i].book_id);
  197.                           printf("书名:%s\n", book[i].book_name);
  198.                           printf("出版日期:%s\n", book[i].publish_date);
  199.                          printf("出版社:%s\n", book[i].press);
  200.                          printf("作者:%s\n", book[i].author);
  201.                          return 0;
  202.                   }
  203.           }

  204.         printf("该图书不存在!\n\n");
  205.           return 1;
  206. }


  207. int delete()
  208. {
  209.           char delete_id[40];
  210.           int i;

  211.         printf("请输入要删除的图书的书号:");
  212.           scanf("%s", delete_id);

  213.         for (i=0; i<current; i++)
  214.           {
  215.                   if (strcmp(book[i].book_id, delete_id) == 0)
  216.                   {
  217.                           if (i<(current-1))
  218.                                   memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
  219.                           current--;
  220.                          printf("删除成功\n");
  221.                          return 0;
  222.                   }
  223.           }
  224.           printf("该书不存在\n");

  225.         return 1;
  226. }

复制代码
哈哈哈
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|有戏 粤ICP备2020111303号

GMT+8, 2025-12-6 12:13 , Processed in 0.080125 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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