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

KiKi定义电子日历类

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

//纯c写类
#include <stdio.h>
#include <stdlib.h>

typedef struct _TDate TDate;

typedef void (*fptrDisplayInfo)(TDate*) ;
typedef void (*fptrDelete)(TDate*) ;

typedef struct _TDate{
    int Month;
    int Day;
    int Year;
    fptrDisplayInfo Display;
    fptrDelete Delete;
} TDate;
TDate *newTDate(const int m,const int d,const int y);
void Display_TDate(TDate* const pTDateObj);
void delete_TDate(TDate* const pTDateObj);

TDate *newTDate(const int m,const int d,const int y){
    TDate* pObj = NULL;
    pObj = (TDate*)malloc(sizeof(TDate));
    if(pObj == NULL){
        return NULL;
    }
    pObj->Month = m;
    pObj->Day = d;
    pObj->Year = y;
    pObj->Display = Display_TDate;
    pObj->Delete = delete_TDate;
    return pObj;
}
void Display_TDate(TDate* const pTDateObj){
    printf("%d/%d/%d",pTDateObj->Day,pTDateObj->Month,pTDateObj->Year);
}
void delete_TDate(TDate* const pTDateObj){
    free(pTDateObj);
}

int main(){
    int y,m,d;
    scanf("%d",&y);
    scanf("%d",&m);
    scanf("%d",&d);
  
    TDate* pTDateObj = newTDate(m, d, y);
    pTDateObj->Display(pTDateObj);
    pTDateObj->Delete(pTDateObj);
    pTDateObj = NULL;
    return 0;
}

全部评论

相关推荐

1jian10:48h没写面评会变成这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务