|
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct
- {
- char book_id[20]; //书号、
- char book_name[40]; //书名、
- char publish_date[20]; // 出版日期、
- char press[40]; //出版社
- char author[40]; // 作者
- }Information;
- Information *book = NULL;
- int current = 0;
- int total = 0;
- int main()
- {
- char choice;
- //显示程序基本信息
- printf(" 图书信息管理系统\n");
- while(1)
- {
- printf("\n");
- printf(" 程序功能\n");
- printf(" 1:录入图书信息\n");
- printf(" 2:保存图书信息\n");
- printf(" 3:查询图书信息\n");
- printf(" 4:删除图书信息\n");
- printf("\n");
- printf(" 请选择:");
- choice = getch();
- printf("\n\n");
- switch(choice)
- {
- case '1':
- insert();
- break;
- case '2':
- save();
- break;
- case '3':
- query();
- break;
- case '4':
- delete();
- break;
- case '0':
- if (book != NULL)
- {
- free(book);
- }
- return 0;
- default:
- printf("非法选择,请重新选择\n");
- }
- }
- return 0; //程序返回
- }
- int insert()
- {
- while(total <= 0)
- {
- printf("请输入要图书的学生总数:");
- scanf("%d", &total);
- }
- if (book == NULL)
- book = (Information *)malloc(total*sizeof(Information));
- if (book == NULL)
- {
- printf("分配失败\n");
- return 1;
- }
- if (current >= total)
- {
- Information *tmp = (Information *)malloc(current*sizeof(Information));
- memmove(tmp,book,current*sizeof(Information));
- total += 10;
- realloc(book,total*sizeof(Information));
- memmove(book,tmp,current*sizeof(Information));
- free(tmp);
- }
- printf("请输入书号:");
- scanf("%s", book[current].book_id);
- printf("请输入书名:");
- scanf("%s", book[current].book_name);
- printf("请输入出版日期:");
- scanf("%s", book[current].publish_date);
- printf("请输入出版社:");
- scanf("%s", book[current].press);
- printf("请输入作者:");
- scanf("%s", book[current].author);
- current++;
- return 0;
- }
- int save()
- {
- int count;
- FILE *fs;
- fs = fopen("BookData.dat", "wb");
- if (fs == NULL)
- {
- printf("不能打开文件\n");
- return 1;
- }
- count = fwrite(book, sizeof(Information), current, fs);
- fclose(fs);
- if (count != current)
- {
- printf("保存失败\n");
- return 1;
- }
- printf("保存成功\n");
- return 0;
- }
- int query()
- {
- char query_choice;
- printf("\n");
- printf(" 选择查询关键字(模糊)查询\n");
- printf(" 1:书号\n");
- printf(" 2:书名\n");
- printf(" 3:出版社\n");
- printf("\n");
- printf(" 请选择:");
- query_choice = getch();
- printf("\n\n");
- switch(query_choice)
- {
- case '1':
- query_id();
- break;
- case '2':
- query_bookname();
- break;
- case '3':
- query_publish();
- break;
- default:
- printf("非法选择,请重新选择\n");
- }
- }
- int query_id()
- {
- char id[40];
- int i;
- printf("请输入要查询的书号:");
- scanf("%s", id);
- for (i=0; i<current; i++)
- {
- if (strcmp( book[i].book_id, id ) == 0)
- {
- printf("该图书信息如下\n");
- printf("书号:%s\n", book[i].book_id);
- printf("书名:%s\n", book[i].book_name);
- printf("出版日期:%s\n", book[i].publish_date);
- printf("出版社:%s\n", book[i].press);
- printf("作者:%s\n", book[i].author);
- return 0;
- }
- }
- printf("该图书不存在!\n\n");
- return 1;
- }
- int query_bookname()
- {
- char name[40];
- int i;
- printf("请输入要查询的书名:");
- scanf("%s", name);
- for (i=0; i<current; i++)
- {
- if (strcmp(book[i].book_name, name) == 0)
- {
- printf("该图书信息如下\n");
- printf("书号:%s\n", book[i].book_id);
- printf("书名:%s\n", book[i].book_name);
- printf("出版日期:%s\n", book[i].publish_date);
- printf("出版社:%s\n", book[i].press);
- printf("作者:%s\n", book[i].author);
- return 0;
- }
- }
- printf("该图书不存在!\n\n");
- return 1;
- }
- int query_publish()
- {
- char press1[40];
- int i;
- printf("请输入要查询的出版社:");
- scanf("%s", press1);
- for (i=0; i<current; i++)
- {
- if (strcmp(book[i].press, press1) == 0)
- {
- printf("该图书信息如下\n");
- printf("书号:%s\n", book[i].book_id);
- printf("书名:%s\n", book[i].book_name);
- printf("出版日期:%s\n", book[i].publish_date);
- printf("出版社:%s\n", book[i].press);
- printf("作者:%s\n", book[i].author);
- return 0;
- }
- }
- printf("该图书不存在!\n\n");
- return 1;
- }
- int delete()
- {
- char delete_id[40];
- int i;
- printf("请输入要删除的图书的书号:");
- scanf("%s", delete_id);
- for (i=0; i<current; i++)
- {
- if (strcmp(book[i].book_id, delete_id) == 0)
- {
- if (i<(current-1))
- memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
- current--;
- printf("删除成功\n");
- return 0;
- }
- }
- printf("该书不存在\n");
- return 1;
- }
复制代码 |
|