有戏

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

图书信息管理系统代码

[复制链接]

1000

主题

1002

帖子

25万

积分

论坛元老

Rank: 8Rank: 8

积分
251951
发表于 2015-1-17 09:50:59 | 显示全部楼层 |阅读模式


  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <stdlib.h>
  6. typedef struct
  7. {
  8.     int year;
  9.     int month;
  10.     int day;
  11. } Date;

  12. typedef struct node
  13. {
  14.     char book_id[20];
  15.     char book_name[40];
  16.     char author[40];
  17.     Date publish_day;
  18.     char press[40];
  19.     struct node *next;
  20. } book;

  21. int current = 0;
  22. int total = 100;
  23. book *head;
  24. book *tail;

  25. int insert()   // 录入图书信息
  26. {

  27.     while(1)
  28.         {
  29.             book *node=(book*)malloc(sizeof(book));
  30.             printf("    请输入图书的书号:");
  31.             scanf("%s", node->book_id );
  32.             printf("    请输入图书的书名:");
  33.             scanf("%s", node->book_name );
  34.             printf("    请输入图书的出版日期:");
  35.             scanf("%d-%d-%d", &node->publish_day.year, &node->publish_day.month,&node->publish_day.day );
  36.             printf("    请输入图书的作者的名字:");
  37.             scanf("%s", node->author);
  38.             printf("    请输入图书的出版社:");
  39.             scanf("%s", node->press );
  40.             node->next=NULL;
  41.             tail->next=node;
  42.             tail=node;
  43.             current++;
  44.             return 0;
  45.         }
  46. }

  47. int save()
  48. {
  49.     FILE *fs;
  50.     int current;
  51.     book *p;

  52.     p=head->next;
  53.     fs = fopen("Book.dat","wb");
  54.     if(fs == NULL)
  55.     {
  56.         printf("    打开文件失败");
  57.         return 0;
  58.     }

  59.     while(p!=NULL)
  60.     {
  61.         current=fwrite(p,sizeof(book),1,fs);
  62.         if(current!=1)
  63.         {
  64.             printf("       写文件失败\n");
  65.             return 1;
  66.         }
  67.         p=p->next;
  68.     }
  69.     fclose(fs);
  70.     printf("    写文件成功\n");
  71.     return 0;
  72. }
  73. int load()
  74. {
  75.     FILE *fs;
  76.     char k;
  77.     book *temp;
  78.     head->next=NULL;
  79.     tail=head;
  80.     int count,i;

  81.     fs = fopen("Book.dat","rb");
  82.     if(fs == NULL)
  83.     {
  84.         printf("    打开文件失败");
  85.         return 0;
  86.     }

  87.     fseek(fs,0,SEEK_END);
  88.     count = ftell(fs);
  89.     current = count/sizeof(book);


  90.     fseek(fs,0,SEEK_SET);
  91.     for(i=0;i<current;i++)
  92.     {
  93.         book *temp=(book*)malloc(sizeof(book));
  94.         count = fread(temp,sizeof(book),1,fs);
  95.         if(count!=1)
  96.         {
  97.             printf("    读文件失败\n");
  98.             head->next=NULL;
  99.             tail=head;
  100.                 return 1;
  101.         }
  102.         temp->next=NULL;
  103.         tail->next=temp;
  104.         tail=temp;

  105.     }
  106.     printf("    加载成功!\n");
  107. }
  108. int query()
  109. {
  110.     char choice;
  111.     while(1)
  112.     {
  113.         printf("     请输入查找方式\n");
  114.         printf("        1:按书名查找\n");
  115.         printf("        2:按书号查找\n");
  116.         printf("        3:按作者查找\n");
  117.         printf("        0:返回\n");
  118.         choice = getch();
  119.         printf("\n\n");
  120.         switch(choice)
  121.         {
  122.             case '1':
  123.                 query_bookname();
  124.                 break;
  125.             case '2':
  126.                 query_id();
  127.                 break;
  128.             case '3':
  129.                 query_author();
  130.                 break;
  131.             case '0':
  132.                 return 0;
  133.             default:
  134.                 printf("    非法选择,请重新选择\n");
  135.         }
  136.     return 0;
  137.     }
  138. }

  139. int query_bookname()
  140. {
  141.     int j=0;
  142.     book *p;
  143.     char book_name[40];
  144.     printf("    请输入需要查询的图书名 : ");
  145.     scanf("%s", book_name);

  146.     p=head->next;
  147.     while(p!=NULL)
  148.     {
  149.         if ( strstr( p->book_name, book_name) != NULL )
  150.         {
  151.             printf("    该图书的书号 :%s \n"  , p->book_id);
  152.             printf("    该图书的书名 :%s \n" , p->book_name);
  153.             printf("    该图书的作者名字 :%s \n" , p->author);
  154.             printf("    该图书出版日期 :%d-%d-%d \n" , p->publish_day.year,p->publish_day.month,p->publish_day.day);
  155.             printf("    该图书的出版社 :%s \n"  , p->press);
  156.             printf("================================================\n");
  157.             j++;
  158.         }
  159.         p=p->next;
  160.     }

  161.     if(j==0)
  162.         printf("   没有查询到《%s》这本书 \n", book_name );
  163.     return 0;
  164. }
  165. int query_id()
  166. {
  167.     int j=0;
  168.     char book_id[20];
  169.     book *p;
  170.     printf("    请输入需要查询的图书的书号 : ");
  171.     scanf("%s", book_id);

  172.     p=head->next;
  173.     while(p!=NULL)
  174.     {
  175.         if ( strcmp( p->book_id, book_id) == 0 )
  176.         {
  177.             printf("    该图书的书名 :%s \n" , p->book_name);
  178.             printf("    该图书的书号 :%s \n"  , p->book_id);
  179.             printf("    该图书出版日期 :%d-%d-%d \n" , p->publish_day.year,p->publish_day.month,p->publish_day.day);
  180.             printf("    该图书的作者名字 :%s \n" , p->author);
  181.             printf("    该图书的出版社 :%s \n"  , p->press);
  182.             printf("================================================\n");
  183.             return 1;
  184.         }
  185.         p=p->next;
  186.     }
  187.         printf("    没有该图书");
  188.     return 0;
  189. }
  190. int query_author()
  191. {
  192.     int j=0;
  193.     char book_name[40];
  194.     book *p;
  195.     printf("    请输入需要查询的作者名字 : ");
  196.     scanf("%s", book_name);
  197.     p=head->next;
  198.     while(p!=NULL)
  199.     {
  200.         if ( strstr( p->author, book_name) != NULL )
  201.         {
  202.             printf("    该图书的书名 :%s \n" , p->book_name);
  203.             printf("    该图书的书号 :%s \n"  , p->book_id);
  204.             printf("    该图书出版日期 :%d-%d-%d \n" , p->publish_day.year,p->publish_day.month,p->publish_day.day);
  205.             printf("    该图书的作者名字 :%s \n" , p->author);
  206.             printf("    该图书的出版社 :%s \n"  , p->press);
  207.             printf("================================================\n");
  208.             j++;
  209.         }
  210.         p=p->next;
  211.     }
  212.     if(j==0)
  213.         printf("    没有查询到%s的书 \n", book_name );
  214.     return 0;
  215. }

  216. int list()
  217. {
  218.     int i=0;
  219.     printf("    当前有 %d 本图书\n",current);
  220.     book *p;
  221.     p=head->next;
  222.     while(p!=NULL)
  223.     {
  224.         printf("******************************************************\n");
  225.         printf("*    第 %d 本图书的姓名 :%s \n" , i+1 , p->book_name);
  226.         printf("*    第 %d 本的书号 :%s \n" , i+1 , p->book_id);
  227.         printf("*    第 %d 本图书的出版日期 :%d-%d-%d \n" , i+1 , p->publish_day.year,p->publish_day.month,p->publish_day.day);
  228.         printf("*    第 %d 本图书的出版社 :%s \n" , i+1 , p->press);
  229.         printf("*    第 %d 本图书的作者姓名 :%s \n" , i+1 , p->author);
  230.         i++;
  231.         p=p->next;

  232.     }
  233.      printf("******************************************************\n");
  234.     return 1;
  235. }

  236. int update()
  237. {
  238.     book *p;
  239.     char k;
  240.     char book_id[40];
  241.     printf("    请输入需要修改信息的图书的书号 : ");
  242.     scanf("%s", book_id);
  243.     p=head->next;
  244.     while(p!=NULL)
  245.     {
  246.         if ( strcmp( book_id, p->book_id) == 0 )
  247.         {
  248.              printf("    请输入图书的名字:");
  249.             scanf("%s", p->book_name );
  250.             printf("    请输入图书的出版日期:");
  251.             scanf("%d-%d-%d", &p->publish_day.year, &p->publish_day.month,&p->publish_day.day );
  252.             printf("    请输入图书作者的名字:");
  253.             scanf("%s", p->author );
  254.             printf("    请输入图书出版社的名字:");
  255.             scanf("%s", p->press );
  256.             return 1;
  257.         }
  258.         p=p->next;
  259.     }

  260.     printf("    没有此图书\n");

  261.     return 0;

  262. }
  263. int delete()
  264. {
  265.     char k;
  266.     char book_id[40];
  267.     book *q,*p;
  268.     printf("    请输入需要删除信息的图书的书号 : ");
  269.     scanf("%s", book_id);
  270.     q=head;
  271.     p=head->next;
  272.     while(p!=NULL)
  273.     {
  274.         if ( strcmp( book_id, p->book_id) == 0 )
  275.             break;
  276.         q=q->next;
  277.         p=p->next;
  278.     }
  279.     if(p!=NULL)
  280.     {
  281.         q->next=p->next;
  282.         free(p);
  283.     }
  284.     else
  285.         printf("    没有这本图书\n");

  286.     return 0;

  287. }

  288. int main()
  289. {
  290.         char choice;

  291.         printf("        图书信息管理系统\n");
  292.     head=(book*)malloc(sizeof(book));
  293.     tail=head;
  294.     head->next=NULL;
  295.         while(1)
  296.         {
  297.                 printf("\n");
  298.                 printf("                   程序功能\n");
  299.                 printf("                1:录入图书信息\n");
  300.                 printf("                2:查询图书信息\n");
  301.                 printf("                3:修改图书信息\n");
  302.                 printf("                4:删除图书信息\n");
  303.                 printf("                5:列出所有图书信息\n");
  304.                 printf("                6:保存图书信息\n");
  305.                 printf("                7:加载图书信息\n");
  306.                 printf("                0:退出程序\n");
  307.                 printf("\n");
  308.                 printf("        请选择:");
  309.                 choice = getch();
  310.                 printf("\n\n");

  311.                 switch(choice)
  312.                 {
  313.                         case '1':
  314.                                 insert();
  315.                                 break;

  316.                         case '2':
  317.                                  query();
  318.                                  break;

  319.                         case '3':
  320.                                 update();
  321.                                 break;

  322.                         case '4':
  323.                                 delete();
  324.                                 break;

  325.                         case '5':
  326.                                 list();
  327.                                 break;

  328.                         case '6':
  329.                                 save();
  330.                                 break;

  331.                         case '7':
  332.                                 load();
  333.                                 break;

  334.                         case '0':
  335.                                 return 0;
  336.                         default:
  337.                                 printf("非法选择,请重新选择\n");
  338.                 }
  339.         }

  340.         return 0;
  341. }








复制代码
大家好
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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