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


    #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;

    }

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

  • 相关阅读:
    #writeup#pwnable unlink
    #pwnable#cmd2
    #writeup# 深交所CTF内训T2-上传漏洞
    #writeup# 深交所CTF内训T1-SQL注入
    HAProxy 基本翻译
    概况
    Day 2 @ RSA Conference Asia Pacific & Japan 2016
    Burp suite安装使用教程
    Windows server 2012 iis 网页HTTP 404.17
    SQL Server、MySQL、Oracle查询前n条记录
  • 原文地址:https://www.cnblogs.com/TonyJia/p/12788242.html
Copyright © 2020-2023  润新知