|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include<stdio.h>
#include<stdlib.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;
};
struct Book*AddBook(struct Book *);
void print_book(struct Book*);
int main()
{
struct Book *phead=NULL;
char ch;
printf("欢迎来到图书馆\n");
do{
printf("请问是否需要录入图书(Y/N):") ;
scanf(" %c",&ch);
if(ch=='Y')
{
AddBook(phead);
}
putchar('\n');
}while(ch!='N');
putchar('\n');
do
{
printf("请问是否需要打印图书信息(Y/N):");
scanf(" %c",&ch);
if(ch=='Y')
{
print_book(phead);
}
putchar('\n');
}while(ch!='N');
return 0;
}
struct Book*AddBook(struct Book*phead)
{
struct Book *temp=(struct Book*)malloc(sizeof(struct Book));
printf("请输入书名:");
scanf("%s",temp->title);
printf("请输入作者:");
scanf("%s",temp->author);
printf("请输入售价:");
scanf("%f",&temp->price);
printf("请输入出版日期(以“年-月-日”的格式输入):");
scanf("%d-%d-%d",&temp->date.year,&temp->date.month,&temp->date.day);
printf("请输入出版社:");
scanf(" %s",temp->publisher);
//头插法
if(phead==NULL)
{
phead=temp;
temp->pnext=NULL;
}
else
{
temp->pnext=phead;
phead=temp;
}
}
void print_book(struct Book *phead)
{
struct Book*temp=phead;
int count=1;
while(temp!=NULL)
{
printf("打印第%d本图书信息...\n",count);
printf("书名:%s\n",temp->title);
printf("作者:%s\n",temp->author);
printf("售价:%.2f\n",temp->price);
printf("出版日期:%d-%d-%d\n",temp->date.year,temp->date.month,temp->date.day);
printf("出版社:%s",temp->publisher);
count++;
temp=temp->pnext;
}
}
|
-
|