改法一#include <stdio.h>
#include <stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char name[128];
char author[40];
float price;
struct Date date;
};//书籍结构体
void getbook(struct Book *);
void printbook(struct Book *);
void getbook(struct Book *book)
{
printf("请输入书名:\n");
scanf("%s",book->name);
printf("请输入作者:\n");
scanf("%s",book->author);
printf("请输入价格:\n");
scanf("%f",&book->price);
printf("请输入日期:\n");
scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
}//输入书籍信息
void printbook(struct Book *book)
{
printf("书名为:%s\n",book->name);
printf("作者为:%s\n",book->author);
printf("价格为:%f\n",book->price);
printf("日期为:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
}//输出书籍信息
int main()
{
struct Book *pb = malloc(sizeof(*pb));
getbook(pb);
printbook(pb);
free(pb);
return 0;
}
改法二#include <stdio.h>
#include <stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char name[128];
char author[40];
float price;
struct Date date;
};//书籍结构体
void getbook(struct Book *);
void printbook(struct Book *);
void getbook(struct Book *book)
{
printf("请输入书名:\n");
scanf("%s",book->name);
printf("请输入作者:\n");
scanf("%s",book->author);
printf("请输入价格:\n");
scanf("%f",&book->price);
printf("请输入日期:\n");
scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
}//输入书籍信息
void printbook(struct Book *book)
{
printf("书名为:%s\n",book->name);
printf("作者为:%s\n",book->author);
printf("价格为:%f\n",book->price);
printf("日期为:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
}//输出书籍信息
int main()
{
struct Book pb;
getbook(&pb);
printbook(&pb);
return 0;
}
|