|
|
图书管理系统【实现多重查找、删除】
- #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(" 4:出版社\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_author();
- break;
- case '4':
- 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 substr[40];
- int i;
- printf("请输入要查询的书名:");
- scanf("%s", substr);
- /*
- // 也可以改成 char str[] = "http://see.xidian.edu.cn/cpp/u/xitong/";
- char *str = "网络世界无限风光";
- char *substr = "世界";
- for (i=0; i<current; i++)
- {
- if ( char *s = strstr(str, substr);)
- {
- 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_author()
- {
- char author1[40];
- int find=0;
- int i;
- printf("请输入要查询的作者:");
- scanf("%s", author1);
- for (i=0; i<=current; i++)
- {
- if (strcmp(book[i].author , author1) == 0)
- {
- find+=1;
- printf("*******************************************\n");// 33个 星号
- printf(" %s 作者的第 %d 本书:\n",author1,find);//前面统一6个空格
- 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);
- }
- }
- if(i=current)
- printf("*******************************************\n");
- return 0;
- printf("该作者不存在!\n\n");
- return 1;
- }
- int query_publish()
- {
- char press1[40];
- int find=0;
- int i;
- printf("请输入要查询的出版社:");
- scanf("%s", press1);
- for (i=0; i<=current; i++)
- {
- if (strcmp(book[i].press, press1) == 0)
- {
- find+=1;
- printf("*******************************************\n");// 33个 星号
- printf(" 在 %s 出版社的第 %d 本书:\n",press1,find);//前面统一6个空格
- 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);
- }
- }
- if(i=current)
- printf("*******************************************\n");
- return 0;
- printf("该图书不存在!\n\n");
- return 1;
- }
- int delete()
- {
- char query_choice;
- printf("\n");
- printf(" 选择你要删除的选项\n");
- printf(" 1:书号\n");
- printf(" 2:书名\n");
- printf(" 3:作者\n");
- printf(" 4:出版社\n");
- printf("\n");
- printf(" 请选择:");
- query_choice = getch();
- printf("\n\n");
- switch(query_choice)
- {
- case '1':
- delete_id();
- break;
- case '2':
- delete_bookname();
- break;
- case '3':
- delete_author();
- break;
- case '4':
- delete_publish();
- break;
- default:
- printf("非法选择,请重新选择\n");
- }
- }
- int delete_id()
- {
- 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;
- }
- int delete_bookname()
- {
- char delete_bookname[40];
- int i;
- printf("请输入要删除的图书的书名:");
- scanf("%s", delete_bookname);
- for (i=0; i<current; i++)
- {
- if (strcmp(book[i].book_name, delete_bookname) == 0)
- {
- if (i<(current-1))
- memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
- current--;
- printf("删除成功\n");
- return 0;
- }
- }
- printf("该书不存在\n");
- return 1;
- }
- int delete_publish()
- {
- //因为一个出版社有很多本书,全部查询列出来,以供删除
- char press2[40];
- int j;
- int find=0;
- printf("请输入你要删除的书所在的出版社:");
- scanf("%s", press2);
- for (j=0; j<=current; j++)
- {
- if (strcmp(book[j].press, press2) == 0)
- {
- find+=1;
- printf("*******************************************\n");// 33个 星号
- printf(" 书号:%s\n", book[j].book_id);
- printf(" 书名:%s\n", book[j].book_name);
- printf(" 出版日期:%s\n", book[j].publish_date);
- printf(" 出版社:%s\n", book[j].press);
- printf(" 作者:%s\n", book[j].author);
- }
- }
- if(j=current)
- printf("*******************************************\n");
- char delete_book_id[40];
- int i;
- printf(" 出版社%s所有的书如上。\n", book[i].press);
- printf("请输入要删除的书所在的书号:");
- scanf("%s", delete_book_id);
- for (i=0; i<current; i++)
- {
- if (strcmp(book[i].book_id, delete_book_id) == 0)
- {
- if (i<(current-1))
- memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
- current--;
- printf("删除成功\n");
- printf(" 书号为%s的书已被成功删除\n", book[i].book_id);
- return 1;
- }
- }
- /*
- printf("输错了,该书不存在\n"); //其实,这步是多余的
- return 1;
- */
- }
- int delete_author()
- {
- //因为一个作者有很多本书,全部查询列出来,以供删除
- char author[40];
- int j;
- int find=0;
- printf("请输入你要删除的书的作者:");
- scanf("%s", author);
- for (j=0; j<=current; j++)
- {
- if (strcmp(book[j].author, author) == 0)
- {
- find+=1;
- printf("*******************************************\n");// 33个 星号
- printf(" 书号:%s\n", book[j].book_id);
- printf(" 书名:%s\n", book[j].book_name);
- printf(" 出版日期:%s\n", book[j].publish_date);
- printf(" 出版社:%s\n", book[j].press);
- printf(" 作者:%s\n", book[j].author);
- }
- }
- if(j=current)
- printf("*******************************************\n");
- char delete_book_id[40];
- int i;
- printf(" 该作者%s所有的书如上。\n", book[i].press);
- printf("请输入要删除的书所在的书号:");
- scanf("%s", delete_book_id);
- for (i=0; i<current; i++)
- {
- if (strcmp(book[i].book_id, delete_book_id) == 0)
- {
- if (i<(current-1))
- memmove(&book, &book[i+1], (current-1-i)*sizeof(Information));
- current--;
- printf("删除成功\n");
- printf(" 书号为%s的书已被成功删除\n", book[i].book_id);
- return 1;
- }
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|