|
发表于 2019-9-4 11:18:01
|
显示全部楼层
本帖最后由 superbe 于 2019-9-4 11:24 编辑
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct DATE
{
int year;
int math;
int day;
};
struct BOOK
{
char title[128];
char author[40];
float price;
struct DATE data;
char pulic[40];
struct BOOK* next;
};
void luru(struct BOOK* x)
{
static int count=0;
count++;
for(int i=1;i<count;i++)
{
x=x->next;
}
printf("请输入主题: ");
scanf("%s",x->title);
printf("请输入作者:");
scanf("%s",x->author);
printf("请输入价格: ");
scanf("%f",&x->price);
printf("请输入日期如(2017 3 14):");
scanf("%d%d%d",&x->data.year,&(x->data.math),&(x->data.day));
printf("请输入出版社:");
scanf("%s",x->pulic);
//return x;
}
void daying(struct BOOK* x)
{
while(x!=NULL)
{
printf("主题:%s\n ",x->title);
printf("作者:%s\n ",x->author);
printf("价格:%.2f\n",x->price); //去掉了%. 2f中间的空格
printf("日期:%d-%d-%d\n ",x->data.year,x->data.math,x->data.day);
printf("出版社:%s\n ",x->pulic);
x=x->next;
}
}
/*struct BOOK* addhead(struct BOOK** p)
{
struct BOOK* team;
struct BOOK* book=NULL;
book=(struct BOOK*)malloc(sizeof(struct BOOK));
if(book==NULL)
{
printf("申请内存失败");
exit(EXIT_SUCCESS);
}
else
{
team=*p;
*p=book;
book->next=team;
}
return *p;
}*/
struct BOOK* addlow(struct BOOK** p)
{
static struct BOOK* team;
struct BOOK* book=NULL;
book=(struct BOOK*)malloc(sizeof(struct BOOK));
if(book==NULL)
{
printf("申请内存失败");
exit(EXIT_SUCCESS);
}
else
{
if(*p==NULL)
{
*p=book;
book->next=NULL;
}
else
{
team->next=book;
book->next=NULL;
}
team=book;
}
return *p;
}
void rest(struct BOOK*p)
{
struct BOOK* am;
while(p!=NULL)
{
am=p;
p=p->next;
free(am);
}
}
struct BOOK* sousuo(struct BOOK *p,char *c)
{
while(1)
{
if(p==NULL) //两个if块交换了位置
{
return p;
}
if((!strcmp(c,p->title))||(!strcmp(c,p->author)))
{
return p;
}
p=p->next;
}
}
void sousuoresoult(struct BOOK *x,char *c)
{
if(x!=NULL)
{
do
{
printf("主题:%s\n ",x->title);
printf("作者:%s\n ",x->author);
printf("价格:%.2f\n",x->price); //去掉了%. 2f中间的空格
printf("日期:%d-%d-%d\n ",x->data.year,x->data.math,x->data.day);
printf("出版社:%s\n ",x->pulic);
x=x->next;
}while((x=sousuo(x,c))!=NULL); //原来是while(sousuo(x,c)!=NULL);
}
else
{
printf("搜索失败\n");
}
}
int main()
{
char ch,shu[80];
struct BOOK*p=NULL;
struct BOOK* dg;
while(1)
{
do
{
printf("是否录入书籍(Y/N):");
}while((ch=getchar())!='y'&&ch!='n');
p=addlow(&p);
luru(p);
getchar();
do
{
printf("是否录入完毕(Y/N):");
}while((ch=getchar())!='y'&&ch!='n');
if(ch=='y')
{
break;
}
getchar();
}
daying(p);
printf("请输入你想搜索的书如(作者/书名):");
scanf("%s",shu);
dg=sousuo(p,shu);
sousuoresoult(dg,shu);
rest(p);
return 0;
} |
|