1. 程式人生 > >C語言 輸入年月日判斷是第幾天

C語言 輸入年月日判斷是第幾天

判斷輸入年份為閏年還是平年.主要用於判斷二月份的天數.

int isLeapYear(int year);
int isLeapYear(int year) {
    int february = 0;
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
        february = 29;
    } else {
        february = 28;
    }
    return february;
}

計算天數的函式
int daysOfDate(int year, int month, int day);
int daysOfDate(int year, int month, int day) {
    int days = day;
    switch (month - 1) {
        case 3:
            days += 31;
        case 2:
            days += isLeapYear(year);
        case 1:
            days += 31;
            break;
            
        default:
            break;
    }
    return days;
}

在主函式中呼叫即可,

OK~