|
发表于 2019-8-5 18:34:57
|
显示全部楼层
本楼为最佳答案
 结构体变量的赋值方法有问题,getInput() 传入的应该是结构体指针,在函数中直接为结构体的各个成员赋值,不用再返回结构体了。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.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)
- {
- 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("售价:%f\n" , book . price) ;
- printf("出版日期:%4d-%02d-%02d\n" , book . date . year , book . date . month , book . date . day) ;
- printf("出版社:%s\n" , book . publisher) ;
- }
- int main(void)
- {
- struct Book b1 , b2 ;
- printf("请输入第一本书的信息...\n") ;
- getInput(& b1) ;
- putchar('\n') ;
- printf("请输入第二本书的信息...\n") ;
- getInput(& b2) ;
- printf("\n录入完毕,现在开始打印验证...\n\n") ;
- printf("打印第一本书的信息\n") ;
- printBook(b1) ;
- printf("打印第二本书的信息\n") ;
- printBook(b2) ;
- }
复制代码 |
|