有戏

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

图书管理系统【实现多重查找、删除】

[复制链接]

1000

主题

1002

帖子

25万

积分

论坛元老

Rank: 8Rank: 8

积分
251951
发表于 2015-1-17 14:19:40 | 显示全部楼层 |阅读模式
图书管理系统【实现多重查找、删除】
  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("                4:出版社\n");
  123.                   printf("\n");

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

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

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


  135.                         case '3':
  136.                                   query_author();
  137.                                   break;
  138.                          case '4':
  139.                                   query_publish();
  140.                                   break;

  141.                         default:
  142.                                   printf("非法选择,请重新选择\n");
  143.                   }
  144. }



  145. int query_id()
  146. {


  147.      char id[40];
  148.           int i;

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

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

  164.         printf("该图书不存在!\n\n");
  165.           return 1;
  166. }

  167.   int  query_bookname()
  168. {


  169.      char substr[40];
  170.           int i;

  171.         printf("请输入要查询的书名:");
  172.           scanf("%s", substr);
  173.   /*
  174.         // 也可以改成 char str[] = "http://see.xidian.edu.cn/cpp/u/xitong/";
  175.         char *str = "网络世界无限风光";
  176.         char *substr = "世界";





  177.         for (i=0; i<current; i++)
  178.           {
  179.                   if ( char *s = strstr(str, substr);)
  180.                   {
  181.                           printf("该图书信息如下\n");
  182.                           printf("书号:%s\n", book[i].book_id);
  183.                           printf("书名:%s\n", book[i].book_name);
  184.                          printf("出版日期:%s\n", book[i].publish_date);
  185.                          printf("出版社:%s\n", book[i].press);
  186.                          printf("作者:%s\n", book[i].author);
  187.                          return 0;
  188.                   }
  189.           }

  190.         printf("该图书不存在!\n\n");
  191.           return 1;


  192.           */
  193. }


  194. int  query_author()
  195. {


  196.      char author1[40];
  197.      int find=0;
  198.           int i;

  199.         printf("请输入要查询的作者:");
  200.           scanf("%s", author1);

  201.         for (i=0; i<=current; i++)
  202.           {
  203.                   if (strcmp(book[i].author , author1) == 0)
  204.                   {
  205.                         find+=1;
  206.                         printf("*******************************************\n");//    33个 星号
  207.                         printf("      %s 作者的第 %d 本书:\n",author1,find);//前面统一6个空格
  208.                         printf("      书号:%s\n", book[i].book_id);
  209.                         printf("      书名:%s\n", book[i].book_name);
  210.                          printf("出版日期:%s\n", book[i].publish_date);
  211.                         printf("      出版社:%s\n", book[i].press);
  212.                   }
  213.           }
  214.             if(i=current)
  215.              printf("*******************************************\n");
  216.             return 0;

  217.         printf("该作者不存在!\n\n");
  218.           return 1;
  219. }

  220. int  query_publish()
  221. {


  222.      char press1[40];
  223.      int find=0;
  224.           int i;

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

  227.         for (i=0; i<=current; i++)
  228.           {
  229.                   if (strcmp(book[i].press, press1) == 0)
  230.                   {
  231.                         find+=1;
  232.                         printf("*******************************************\n");//    33个 星号
  233.                         printf("      在 %s 出版社的第 %d 本书:\n",press1,find);//前面统一6个空格
  234.                         printf("      书号:%s\n", book[i].book_id);
  235.                         printf("      书名:%s\n", book[i].book_name);
  236.                         printf("出版日期:%s\n", book[i].publish_date);
  237.                         printf("      出版社:%s\n", book[i].press);
  238.                         printf("      作者:%s\n", book[i].author);
  239.                   }
  240.           }
  241.             if(i=current)
  242.              printf("*******************************************\n");
  243.             return 0;

  244.         printf("该图书不存在!\n\n");
  245.           return 1;
  246. }

  247. int delete()
  248. {
  249.                   char query_choice;
  250.                  printf("\n");
  251.                   printf("     选择你要删除的选项\n");
  252.                   printf("                1:书号\n");
  253.                   printf("                2:书名\n");
  254.                   printf("                3:作者\n");
  255.                   printf("                4:出版社\n");
  256.                   printf("\n");

  257.                 printf("        请选择:");
  258.                   query_choice = getch();
  259.                   printf("\n\n");

  260.                 switch(query_choice)
  261.                   {
  262.                           case '1':
  263.                                   delete_id();
  264.                                   break;

  265.                         case '2':
  266.                                    delete_bookname();
  267.                                    break;


  268.                         case '3':
  269.                                   delete_author();
  270.                                   break;
  271.                          case '4':
  272.                                   delete_publish();
  273.                                   break;

  274.                         default:
  275.                                   printf("非法选择,请重新选择\n");
  276.                   }
  277. }


  278. int delete_id()
  279. {

  280.           char delete_id[40];
  281.           int i;

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

  284.         for (i=0; i<current; i++)
  285.           {
  286.                   if (strcmp(book[i].book_id, delete_id) == 0)
  287.                   {
  288.                           if (i<(current-1))
  289.                                   memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
  290.                           current--;
  291.                          printf("删除成功\n");
  292.                          return 0;
  293.                   }
  294.           }
  295.           printf("该书不存在\n");

  296.         return 1;
  297. }


  298. int delete_bookname()
  299. {
  300.           char delete_bookname[40];
  301.           int i;

  302.         printf("请输入要删除的图书的书名:");
  303.           scanf("%s", delete_bookname);

  304.         for (i=0; i<current; i++)
  305.           {
  306.                   if (strcmp(book[i].book_name, delete_bookname) == 0)
  307.                   {
  308.                           if (i<(current-1))
  309.                                   memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
  310.                           current--;
  311.                          printf("删除成功\n");
  312.                          return 0;
  313.                   }
  314.           }
  315.           printf("该书不存在\n");

  316.         return 1;
  317. }


  318. int delete_publish()
  319. {
  320. //因为一个出版社有很多本书,全部查询列出来,以供删除
  321.      char press2[40];
  322.     int j;
  323.      int find=0;
  324.         printf("请输入你要删除的书所在的出版社:");
  325.           scanf("%s", press2);

  326.         for (j=0; j<=current; j++)
  327.           {
  328.                   if (strcmp(book[j].press, press2) == 0)
  329.                   {
  330.                         find+=1;
  331.                         printf("*******************************************\n");//    33个 星号
  332.                         printf("      书号:%s\n", book[j].book_id);
  333.                         printf("      书名:%s\n", book[j].book_name);
  334.                         printf("      出版日期:%s\n", book[j].publish_date);
  335.                         printf("      出版社:%s\n", book[j].press);
  336.                         printf("      作者:%s\n", book[j].author);
  337.                   }
  338.           }
  339.             if(j=current)
  340.              printf("*******************************************\n");



  341.        char delete_book_id[40];
  342.         int i;
  343.          printf("  出版社%s所有的书如上。\n", book[i].press);
  344.           printf("请输入要删除的书所在的书号:");
  345.           scanf("%s", delete_book_id);

  346.         for (i=0; i<current; i++)
  347.           {
  348.                   if (strcmp(book[i].book_id, delete_book_id) == 0)
  349.                   {
  350.                           if (i<(current-1))
  351.                                   memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
  352.                           current--;
  353.                          printf("删除成功\n");
  354.                         printf(" 书号为%s的书已被成功删除\n", book[i].book_id);
  355.                          return 1;
  356.                   }
  357.           }

  358.           /*
  359.         printf("输错了,该书不存在\n");  //其实,这步是多余的

  360.         return 1;
  361.         */
  362. }



  363. int delete_author()
  364. {
  365. //因为一个作者有很多本书,全部查询列出来,以供删除
  366.      char author[40];
  367.     int j;
  368.      int find=0;
  369.         printf("请输入你要删除的书的作者:");
  370.           scanf("%s", author);

  371.         for (j=0; j<=current; j++)
  372.           {
  373.                   if (strcmp(book[j].author, author) == 0)
  374.                   {
  375.                         find+=1;
  376.                         printf("*******************************************\n");//    33个 星号
  377.                         printf("      书号:%s\n", book[j].book_id);
  378.                         printf("      书名:%s\n", book[j].book_name);
  379.                         printf("      出版日期:%s\n", book[j].publish_date);
  380.                         printf("      出版社:%s\n", book[j].press);
  381.                         printf("      作者:%s\n", book[j].author);
  382.                   }
  383.           }
  384.             if(j=current)
  385.              printf("*******************************************\n");

  386.        char delete_book_id[40];
  387.         int i;
  388.          printf("  该作者%s所有的书如上。\n", book[i].press);
  389.           printf("请输入要删除的书所在的书号:");
  390.           scanf("%s", delete_book_id);

  391.         for (i=0; i<current; i++)
  392.           {
  393.                   if (strcmp(book[i].book_id, delete_book_id) == 0)
  394.                   {
  395.                           if (i<(current-1))
  396.                                   memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
  397.                           current--;
  398.                          printf("删除成功\n");
  399.                         printf(" 书号为%s的书已被成功删除\n", book[i].book_id);
  400.                          return 1;
  401.                   }
  402.           }


  403. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
大家好
回复

使用道具 举报

2

主题

2

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
14390
发表于 2016-1-19 10:12:34 | 显示全部楼层
模糊查找还没做好
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-6 14:29 , Processed in 0.079760 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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