video 44 图书馆(非指针版本,便于理解)

video 44 图书馆(非指针版本,便于理解)

video44
#include <stdio.h>
    struct Date;//声明
    struct Date{
        int year;
        int month;
        int day;
    }date;

    struct Book;//声明
    struct Book{
        char title[128];
        char author[40];
        float price;
        struct Date date;//结构体嵌套
        char publisher[40];
    }book;
    
    struct Book getInput(struct Book book);//声明
    struct Book getInput(struct Book 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 );
    return book;//得到的输入值给book结构体,赋值给形参
};
    
    void printBook(struct Book book)
{ //结构体整体做参数,就要定义出一个结构体x的形式出来
    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(void)
{
    struct Book b1, b2;//定义两个空结构体变量
    printf("请录入第一本书的书名\n");//通过输入得到getInput中的内容,然后再赋值给b1.
    b1=getInput(b1);//b1作为实参,调用getinpurt函数,对里面的形参赋值。然后,得到return book,再赋值给b1.
    putchar('\n');
   
    printf("请录入第二本书的书名\n");
    b2=getInput(b2);
    putchar('\n');
    
    printf("录入完毕,打印第一本书的信息\n");
    printBook(b1);
    putchar('\n');
    
    printf("现在,打印第二本书的信息\n");
    printBook(b2);
    putchar('\n');
     
    return 0;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值