题解 | #KiKi定义电子日历类#

KiKi定义电子日历类

https://www.nowcoder.com/practice/e4c67097cdb242d9a3f38b7cfe839396

#include <stdio.h>
#include<malloc.h>

typedef struct TDate TDate;
//定义3个函数指针
typedef void (*INIT)(TDate* date) ;
typedef void (*SET)(TDate* date, int y, int m, int d);
typedef void (*SHOW)(TDate* date) ;

struct Functions {
    INIT init;
    SET set;
    SHOW show;
};
typedef struct TDate {
    int Year;
    int Month;
    int Day;
    struct Functions* functions;
} TDate;

void init(TDate* date) {
    date->Day = 0;
    date->Month = 0;
    date->Year = 0;
}
void set(TDate* date, int y, int m, int d) {
    date->Day = d;
    date->Month = m;
    date->Year = y;
}
void show(TDate* date) {
    printf("%d/%d/%d", date->Day, date->Month, date->Year);
}

struct Functions functions = {
    .init = init,
    .set = set,
    .show = show
};

int main() {
    TDate* date = malloc(sizeof(TDate));
    int d, m, y;
    scanf("%d %d %d", &y, &m, &d);
    date->functions = &functions;
    functions.init(date);
    functions.set(date, y, m, d);
    functions.show(date);
    return 0;
}

#C#
0基础学C 文章被收录于专栏

0基础学C,从算法开始

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务