鱼C论坛

 找回密码
 立即注册
查看: 2624|回复: 0

[技术交流] 一个简单的图书管理

[复制链接]
发表于 2021-3-4 19:22:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
首先是头文件

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


  4. void input(struct Book *book);        //录入书籍
  5. void inquire(struct Book *book, int book_num);        //查询书籍名称
  6. void print(struct Book *book);                //打印书籍
  7. void menu(struct Book *book);                //目录

  8. struct Time
  9. {
  10.         int year;
  11.         int month;
  12.         int day;
  13. };

  14. struct Book
  15. {
  16.         char name[200];
  17.         struct Time time;
  18.         char chubanshe[200];
  19. };
复制代码



其次是代码部分

  1. #include <test1.h>


  2. void input(struct Book *book)        //录入书籍
  3. {
  4.        

  5.         printf("请输入书名:");
  6.         scanf("%s",book->name);
  7.         printf("请输入年月日:");
  8.         scanf("%d%d%d",&(book->time.year),&(book->time.month),&(book->time.day));
  9.         printf("请输入出版社:");
  10.         scanf("%s",book->chubanshe);
  11.         printf("\n======书籍录入成功======\n\n");       
  12.                
  13. }

  14. void inquire(struct Book *book, int book_num)        //查询书籍名称
  15. {
  16.         char name[200];
  17.         int i,flag = 0;
  18.         printf("\n请输入书籍名称:\n\n");
  19.         scanf("%s",name);

  20.         for(i = 0;i < book_num; i++)
  21.         {

  22.                 if(strcmp(book[i].name, name) == 0)
  23.                 {
  24.                         printf("这是第%d本书\n",i + 1);
  25.                         print(&book[i]);
  26.                         flag = 1;
  27.                         break;
  28.                 }
  29.                
  30.         }
  31.         if(flag == 0)       
  32.         {
  33.                 printf("\n===请重新输入书籍名称===\n\n");
  34.                 inquire(book,book_num);
  35.         }

  36. }

  37. void print(struct Book *book)        //输出查询到的书籍
  38. {
  39.         printf("\n======书籍查询成功======\n\n");
  40.         printf("书  名:%s\n",book -> name);
  41.         printf("日  期:%d-%d-%d\n", book -> time.year, book -> time.month, book -> time.day);
  42.         printf("出版社:%s\n\n",book -> chubanshe);
  43. }

  44. void menu(int *flag)        //目录
  45. {
  46.         int mode;
  47.         printf("请选择模式:\n");
  48.         printf("1.输入1录入书籍;\n");
  49.         printf("2.输入2查询书籍;\n");
  50.         printf("3.输入3删除书籍;\n");                 //未做
  51.         printf("4.输入4退出系统;\n");
  52.         scanf("%d",flag);
  53.        
  54. }

  55. int main(void)
  56. {
  57.         struct Book *book = NULL;
  58.         int book_num = 0;
  59.         int flag;
  60.         while(1)
  61.         {
  62.                 menu(&flag);
  63.                 switch(flag)
  64.                 {
  65.                         case 1:
  66.                                 {
  67.                                         book_num++;
  68.                                         printf("这是第%d本书\n",book_num);
  69.                                         book = (struct Book *)realloc(book,(book_num + 1) * sizeof(struct Book *));
  70.                                         input(&book[book_num - 1]); break;
  71.                                 }
  72.                         case 2: inquire(book,book_num); break;
  73.                 //        case 3: delet(book); break;
  74.                         case 4: free(book);exit(1);
  75.                         default:printf("\n请重新输入\n\n");break;
  76.                 }
  77.         }


  78.         return 0;
  79. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 07:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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