zoukankan      html  css  js  c++  java
  • 动态结构体变量的指针数组

    #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 ",book -> title);
    printf("作者:%s ",book -> author);
    printf("售价:%.2f ",book -> price);
    printf("出版日期:%d-%d-%d ",book -> date.year,book -> date.month,book -> date.day);
    printf("出版社:%s ",book -> publisher);
    }

    int main()
    {

    int i , j;
    int a;
    struct Book *b2, *b1[50] = {0};

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

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

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

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

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

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

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

    }

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

  • 相关阅读:
    PHP解决跨域问题
    《高性能MySQL》笔记——MySQL建表数据类型的选择
    PHP中有关IPV4 和IPV6地址转换以及其它一些常见问题
    Axure RP Extension for Chrome安装
    DelayQueue的使用
    MySQL8.0设置远程访问权限
    Git 常用命令
    Zipkin分布式跟踪系统介绍
    什么是kibana?
    Elastic-Job-分布式调度解决方案
  • 原文地址:https://www.cnblogs.com/TonyJia/p/12788242.html
Copyright © 2011-2022 走看看