|
|
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <malloc.h>
- #include <stdlib.h>
- typedef struct
- {
- int year;
- int month;
- int day;
- } Date;
- typedef struct node
- {
- char book_id[20];
- char book_name[40];
- char author[40];
- Date publish_day;
- char press[40];
- struct node *next;
- } book;
- int current = 0;
- int total = 100;
- book *head;
- book *tail;
- int insert() // 录入图书信息
- {
- while(1)
- {
- book *node=(book*)malloc(sizeof(book));
- printf(" 请输入图书的书号:");
- scanf("%s", node->book_id );
- printf(" 请输入图书的书名:");
- scanf("%s", node->book_name );
- printf(" 请输入图书的出版日期:");
- scanf("%d-%d-%d", &node->publish_day.year, &node->publish_day.month,&node->publish_day.day );
- printf(" 请输入图书的作者的名字:");
- scanf("%s", node->author);
- printf(" 请输入图书的出版社:");
- scanf("%s", node->press );
- node->next=NULL;
- tail->next=node;
- tail=node;
- current++;
- return 0;
- }
- }
- int save()
- {
- FILE *fs;
- int current;
- book *p;
- p=head->next;
- fs = fopen("Book.dat","wb");
- if(fs == NULL)
- {
- printf(" 打开文件失败");
- return 0;
- }
- while(p!=NULL)
- {
- current=fwrite(p,sizeof(book),1,fs);
- if(current!=1)
- {
- printf(" 写文件失败\n");
- return 1;
- }
- p=p->next;
- }
- fclose(fs);
- printf(" 写文件成功\n");
- return 0;
- }
- int load()
- {
- FILE *fs;
- char k;
- book *temp;
- head->next=NULL;
- tail=head;
- int count,i;
- fs = fopen("Book.dat","rb");
- if(fs == NULL)
- {
- printf(" 打开文件失败");
- return 0;
- }
- fseek(fs,0,SEEK_END);
- count = ftell(fs);
- current = count/sizeof(book);
- fseek(fs,0,SEEK_SET);
- for(i=0;i<current;i++)
- {
- book *temp=(book*)malloc(sizeof(book));
- count = fread(temp,sizeof(book),1,fs);
- if(count!=1)
- {
- printf(" 读文件失败\n");
- head->next=NULL;
- tail=head;
- return 1;
- }
- temp->next=NULL;
- tail->next=temp;
- tail=temp;
- }
- printf(" 加载成功!\n");
- }
- int query()
- {
- char choice;
- while(1)
- {
- printf(" 请输入查找方式\n");
- printf(" 1:按书名查找\n");
- printf(" 2:按书号查找\n");
- printf(" 3:按作者查找\n");
- printf(" 0:返回\n");
- choice = getch();
- printf("\n\n");
- switch(choice)
- {
- case '1':
- query_bookname();
- break;
- case '2':
- query_id();
- break;
- case '3':
- query_author();
- break;
- case '0':
- return 0;
- default:
- printf(" 非法选择,请重新选择\n");
- }
- return 0;
- }
- }
- int query_bookname()
- {
- int j=0;
- book *p;
- char book_name[40];
- printf(" 请输入需要查询的图书名 : ");
- scanf("%s", book_name);
- p=head->next;
- while(p!=NULL)
- {
- if ( strstr( p->book_name, book_name) != NULL )
- {
- printf(" 该图书的书号 :%s \n" , p->book_id);
- printf(" 该图书的书名 :%s \n" , p->book_name);
- printf(" 该图书的作者名字 :%s \n" , p->author);
- printf(" 该图书出版日期 :%d-%d-%d \n" , p->publish_day.year,p->publish_day.month,p->publish_day.day);
- printf(" 该图书的出版社 :%s \n" , p->press);
- printf("================================================\n");
- j++;
- }
- p=p->next;
- }
- if(j==0)
- printf(" 没有查询到《%s》这本书 \n", book_name );
- return 0;
- }
- int query_id()
- {
- int j=0;
- char book_id[20];
- book *p;
- printf(" 请输入需要查询的图书的书号 : ");
- scanf("%s", book_id);
- p=head->next;
- while(p!=NULL)
- {
- if ( strcmp( p->book_id, book_id) == 0 )
- {
- printf(" 该图书的书名 :%s \n" , p->book_name);
- printf(" 该图书的书号 :%s \n" , p->book_id);
- printf(" 该图书出版日期 :%d-%d-%d \n" , p->publish_day.year,p->publish_day.month,p->publish_day.day);
- printf(" 该图书的作者名字 :%s \n" , p->author);
- printf(" 该图书的出版社 :%s \n" , p->press);
- printf("================================================\n");
- return 1;
- }
- p=p->next;
- }
- printf(" 没有该图书");
- return 0;
- }
- int query_author()
- {
- int j=0;
- char book_name[40];
- book *p;
- printf(" 请输入需要查询的作者名字 : ");
- scanf("%s", book_name);
- p=head->next;
- while(p!=NULL)
- {
- if ( strstr( p->author, book_name) != NULL )
- {
- printf(" 该图书的书名 :%s \n" , p->book_name);
- printf(" 该图书的书号 :%s \n" , p->book_id);
- printf(" 该图书出版日期 :%d-%d-%d \n" , p->publish_day.year,p->publish_day.month,p->publish_day.day);
- printf(" 该图书的作者名字 :%s \n" , p->author);
- printf(" 该图书的出版社 :%s \n" , p->press);
- printf("================================================\n");
- j++;
- }
- p=p->next;
- }
- if(j==0)
- printf(" 没有查询到%s的书 \n", book_name );
- return 0;
- }
- int list()
- {
- int i=0;
- printf(" 当前有 %d 本图书\n",current);
- book *p;
- p=head->next;
- while(p!=NULL)
- {
- printf("******************************************************\n");
- printf("* 第 %d 本图书的姓名 :%s \n" , i+1 , p->book_name);
- printf("* 第 %d 本的书号 :%s \n" , i+1 , p->book_id);
- printf("* 第 %d 本图书的出版日期 :%d-%d-%d \n" , i+1 , p->publish_day.year,p->publish_day.month,p->publish_day.day);
- printf("* 第 %d 本图书的出版社 :%s \n" , i+1 , p->press);
- printf("* 第 %d 本图书的作者姓名 :%s \n" , i+1 , p->author);
- i++;
- p=p->next;
- }
- printf("******************************************************\n");
- return 1;
- }
- int update()
- {
- book *p;
- char k;
- char book_id[40];
- printf(" 请输入需要修改信息的图书的书号 : ");
- scanf("%s", book_id);
- p=head->next;
- while(p!=NULL)
- {
- if ( strcmp( book_id, p->book_id) == 0 )
- {
- printf(" 请输入图书的名字:");
- scanf("%s", p->book_name );
- printf(" 请输入图书的出版日期:");
- scanf("%d-%d-%d", &p->publish_day.year, &p->publish_day.month,&p->publish_day.day );
- printf(" 请输入图书作者的名字:");
- scanf("%s", p->author );
- printf(" 请输入图书出版社的名字:");
- scanf("%s", p->press );
- return 1;
- }
- p=p->next;
- }
- printf(" 没有此图书\n");
- return 0;
- }
- int delete()
- {
- char k;
- char book_id[40];
- book *q,*p;
- printf(" 请输入需要删除信息的图书的书号 : ");
- scanf("%s", book_id);
- q=head;
- p=head->next;
- while(p!=NULL)
- {
- if ( strcmp( book_id, p->book_id) == 0 )
- break;
- q=q->next;
- p=p->next;
- }
- if(p!=NULL)
- {
- q->next=p->next;
- free(p);
- }
- else
- printf(" 没有这本图书\n");
- return 0;
- }
- int main()
- {
- char choice;
- printf(" 图书信息管理系统\n");
- head=(book*)malloc(sizeof(book));
- tail=head;
- head->next=NULL;
- while(1)
- {
- printf("\n");
- printf(" 程序功能\n");
- printf(" 1:录入图书信息\n");
- printf(" 2:查询图书信息\n");
- printf(" 3:修改图书信息\n");
- printf(" 4:删除图书信息\n");
- printf(" 5:列出所有图书信息\n");
- printf(" 6:保存图书信息\n");
- printf(" 7:加载图书信息\n");
- printf(" 0:退出程序\n");
- printf("\n");
- printf(" 请选择:");
- choice = getch();
- printf("\n\n");
- switch(choice)
- {
- case '1':
- insert();
- break;
- case '2':
- query();
- break;
- case '3':
- update();
- break;
- case '4':
- delete();
- break;
- case '5':
- list();
- break;
- case '6':
- save();
- break;
- case '7':
- load();
- break;
- case '0':
- return 0;
- default:
- printf("非法选择,请重新选择\n");
- }
- }
- return 0;
- }
复制代码 |
|