题解 | #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;
}
查看6道真题和解析