定义一个结构体变量(包含年月日),计算该日在本年中为第几天?要求写一个days函数.参数是此结构体类型的变量,返回值是整数

<span style="font-size:14px;">main.m
//3.定义一个结构体变量(包含年月日),计算该日在本年中为第几天?要求写一个days函数.参数是此结构体类型的变量,返回值是整数
    Date days = {2001, 12, 31};
    printf("%d\n",printHowManyDay(days)) ;

.h

typedef struct date{
    int year;
    int month;
    int day;
}Date;
//判断是否是闰年
BOOL isLeap(int year);


//输出年月日输出一年中的第几天
int printHowManyDay(Date days);

.m

//判断是否是闰年
BOOL isLeap(int year)
{
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
        return YES;
    }else{
        return NO;
    }
}
//输出年月日输出一年中的第几天
int printHowManyDay(Date days)
{
    int howManyDay = 0;
    int month= days.month;
    int year = days.year;
    int day = days.day;
    int  sum = 0;
    switch (month) {
        case 1:
            sum = 0;
            break;
        case 2:
            sum = 31;
            break;
        case 3:
            sum = 59;
            break;
        case 4:
            sum = 90;
            break;
        case 5:
            sum = 120;
            break;
        case 6:
            sum = 151;
            break;
        case 7:
            sum = 181;
            break;
        case 8:
            sum = 212;
            break;
        case 9:
            sum = 243;
            break;
        case 10:
            sum = 273;
            break;
        case 11:
            sum = 304;
            break;
        case 12:
            sum = 334;
            break;
        default:
            break;
    }
    if (isLeap(year) && month > 2) {
        howManyDay = sum + 1 + day;
    }else{
        howManyDay = sum + day;
    }
    return howManyDay;
}</span>

你可能感兴趣的:(结构)