我帮你改了一下
#include<stdio.h>
void ReadBook(struct Book *);
void PrintBook(struct Book *);
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[128];
};
void ReadBook(struct Book *book)
{
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
printf("请输入售价:");
scanf("%f", &book->price);
printf("请输入出版日期:");
scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
printf("请输入出版社:");
scanf("%s", book->publisher);
}
void PrintBook(struct Book book)
{
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);
}
int main()
{
int N, i;
printf("欢迎光临图书馆!\n");
printf("请输入需录入图书数:\n");
scanf("%d", &N);
putchar('\n');
struct Book *pbook = (struct Book*)malloc(N * sizeof(struct Book)); //在堆上动态分配内存
for (i = 0; i<N; i++)
{
printf("请录入第%d本图书信息....\n", i );
ReadBook(&pbook[i]);
}
putchar('\n');
printf("图书信息录入完毕,现在开始打印验证....\n");
for (i = 0; i<N; i++)
{
printf("打印第%d本图书信息\n", i );
PrintBook(pbook[i]);
}
putchar('\n');
printf("===打印完毕===\n");
return 0;
}
|