动态结构体变量的指针数组

#include <stdio.h>
#include <stdlib.h>

struct Date
{
int year;
int month;
int day;
};

struct Book
{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
};

void getInput(struct Book *book);
void printBook(struct Book *book);

void getInput(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 i , j;
int a;
struct Book *b2, *b1[50] = {0};

system("color 0a");//装B必备

printf(" ===============欢迎来到骚年学习吧!图书馆===============\n");

b2 = (struct Book *) malloc (sizeof(b1));
if ( b2 == NULL )
{
printf("骚年!关掉你的小电影可能内存就够用了!\n");
exit(1);
}

for (i = 0; i < 50; i++)
{
printf("请录入第%d本书的信息...\n", i + 1 );
getInput( b2 + i );
putchar('\n');

printf("骚年,是否继续录入(1=是,0=不是.):");
scanf("%d",&a);
if (a == 0)
{
break;
}
putchar('\n');
}

printf("\n\n录入完毕,现在开始打印验证...\n\n");

for (j = 0; j <= i; j++)
{
printf("第%d本书的信息...\n", j + 1 );
printBook( b2 + j );
putchar('\n');
}
free(b2);
return 0;

}

忘记在哪里转的,等找到再加上吧,和原作者说声抱歉!

posted @ 2020-05-16 20:16  Tony.Jia  阅读(309)  评论(0编辑  收藏  举报