|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么输入指令后会产生这种情况,该怎么改呢?
#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;
printf("请输入搜索图书的的书名或作者:");
getchar(); //没有的话会异常退出
gets(inform);
while(1)
{
if(!strcmp(book->title,inform)||!strcmp(book->author,inform))
{
return book;
}
if(book=NULL)
{
break;
}
book=book->pnext;
}
return book;
}
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=NULL;
struct Book *perious=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))
{
perious=current;
current=current->pnext;
break;
}
if(current=NULL)
{
break;
}
}
if(current==NULL)
{
printf("未找到需要删除的图书\n");
}
else
{
perious=current->pnext;
free(current);
}
}
int main()
{
int N;
struct Book*phead=NULL;
// struct Book*searchbook;
// printf("%43s",图书管理系统\n);
putchar('\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:exit(1);break;
defult:printf("指令错误\n");break; //?
}
putchar('\n');
}
}
问题真的很多:
case 4:cutoff_book(&phead);break; //删除请用指向指针的指针
void cutoff_book(struct Book **book) //删除被搜索的图书信息 ,并释放内存
{
char c[100]={'\0'}; // 要使用请先分配空间
struct Book *perious=NULL;
struct Book *current=NULL;
current = *book;
perious = *book;
printf("请输入你要删除的图书的书名或作者:");
while(getchar()!='\n')continue;
gets(c);
if(current==NULL)
{
printf("未找到需要删除的图书\n");
return; //返回
}
while(!(!strcmp(current->title,c)||!strcmp(current->author,c)))
{
perious=current;
current=current->pnext;
if(current==NULL) // 别把== 当=用。。
{
break;
}
}
if(current==NULL) // 尾部
{
printf("未找到需要删除的图书\n");
}
else
{
if(current == *book) // 是否指向头
{
*book=current->pnext;
}
else // 中间
{
perious->pnext = current->pnext;
}
free(current);
}
}
|
-
|