|
发表于 2018-12-22 12:18:05
|
显示全部楼层
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Book
- {
- char title[128];
- char author[48];
- float price;
- struct Date date;
- char publisher[48];
- struct Book*pnext;
- };
- void ReadBook(struct Book**pphead)
- {
- struct Book*book = (struct Book*)malloc(sizeof(struct Book));
- printf("---开始录人书籍---\n");
- getchar();
- printf("请输入书名:");
- gets(book->title);
- printf("请输入作者:");
- gets(book->author);
- printf("请输入售价:");
- scanf("%f", &book->price);
- printf("请输入出版日期(按“ 2018-12-12 ”的格式输入):");
- scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
- getchar(); //重要
- printf("请输入出版社:");
- //scanf("%s",book->publisher);
- gets(book->publisher);
- if(*pphead == NULL)
- {
- *pphead = book;
- book->pnext = NULL;
- }
- else
- {
- struct Book*temp = *pphead;
- while(1)
- {
- if(temp->pnext == NULL)
- {
- break;
- }
- temp = temp->pnext;
- }
- temp->pnext = book;
- book->pnext = NULL;
- }
- }
- void print_book(struct Book*phead)
- {
- struct Book *book = phead;
- printf("\n---开始打印图书信息---\n");
- if(phead == NULL)
- {
- printf("还未录入图书!\n");
- }
- else
- {
- while(1)
- {
- if(book == NULL)
- {
- break;
- }
- else
- {
- printf("书名:%s\n", book->title);
- printf("作者:%s\n", book->author);
- printf("售价:%.2f", book->price);
- printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
- printf("出版社:%s\n", book->publisher);
- putchar('\n');
- book = book->pnext;
- }
- }
- }
- printf("\n---打印完毕---\n");
- }
- struct Book*SearchBook(struct Book*book) //搜索图书并返回指向该图书信息的指针,未找到则返回NULL
- {
- char inform[100];
- printf("请输入搜索图书的的书名或作者:");
- getchar(); //没有的话会异常退出
- gets(inform);
- while(1)
- {
-
-
-
-
-
- //if(book = NULL)
- if(book == NULL)
- {
- return book;
- }
- if(!strcmp(book->title, inform) || !strcmp(book->author, inform))
- {
- return book;
- }
- book = book->pnext;
- }
- }
- void print_search(struct Book*book) //打印被搜索的图书信息
- {
- if(book == NULL)
- {
- printf("未找到图书信息!\n");
- }
- else
- {
- printf("---你所寻找的图书信息如下---\n");
- printf("书名:%s\n", book->title);
- printf("作者:%s\n", book->author);
- printf("售价:%.2f\n", book->price);
- printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
- printf("出版社:%s\n", book->publisher);
- }
- }
- void cutoff_book(struct Book*book) //删除被搜索的图书信息 ,并释放内存
- {
- char cutbook[100];
- struct Book *pervious = NULL;
- struct Book *current = book;
- printf("请输入你要删除的图书的书名或作者:");
- getchar();
- gets(cutbook);
- if(current == NULL)
- {
- printf("未找到需要删除的图书\n");
- return; //返回
- }
- while(1)
- {
- if(!strcmp(current->title, cutbook) || !strcmp(current->author, cutbook))
- {
- break;
- }
- if(current == NULL)
- {
- break;
- }
- pervious = current;
- current = current->pnext;
- }
- if(current == NULL)
- {
- printf("未找到需要删除的图书!\n");
- }
- else
- {
- pervious = pervious->pnext;
- free(current);
- }
- }
- void releasebook(struct Book*book) //释放链表空间
- {
- struct Book *temp = NULL;
- while(book != NULL)
- {
- temp = book;
- book = book->pnext;
- free(temp);
- }
- }
- int main(void)
- {
- int N;
- struct Book*phead = NULL;
- // struct Book*searchbook;
- putchar('\n');
- printf("%46c图书管理系统\n", ' ');
- printf("\n\n");
- printf("1 录入图书\n");
- printf("2 打印已录入图书信息\n");
- printf("3 搜素图书\n");
- printf("4 删除已录入图书\n");
- printf("5 退出程序\n");
- printf(" \n\n ---输入功能前的相应序号进行操作--- \n\n");
- while(1)
- {
- printf("请输入指令:");
- scanf("%d", &N);
- putchar('\n');
- switch(N)
- {
- case 1:ReadBook(&phead); break;
- case 2:print_book(phead); break;
- case 3:print_search(SearchBook(phead)); break;
- case 4:cutoff_book(phead); break;
- case 5:releasebook(phead); exit(1); break;
-
-
-
-
-
-
- //defult:printf("指令错误\n"); break; //?
- default:printf("指令错误\n"); break; //?
-
-
-
-
-
-
- }
- putchar('\n');
- }
- }
复制代码 |
|