#编程珠玑#第三章 日期N天前后

#include <iostream>

using namespace std;

int normal_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap_month[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


struct MyDate {
    int year;
    int month;
    int day;
};



bool is_leap_year(int temp_year) {

    if ((temp_year % 4 == 0) && (temp_year % 100 != 0)) {
        return true;
    }
    if (temp_year % 400 == 0) {
        return true;
    }
    return false;
}


int count_index(MyDate & temp_date) {
    int total_days = 0;
    if (is_leap_year(temp_date.year))
    {
        for (int i = 0; i < temp_date.month - 1; i++) {
            total_days += leap_month[i];
        }
    } else {
        for (int i = 0; i < temp_date.month - 1; i++) {
            total_days += normal_month[i];
        }
    }
    total_days += temp_date.day;
    return total_days;
}



int count_pass_days(MyDate & start, MyDate & end) {

    int start_index, end_index;
    start_index = count_index(start);
    end_index = count_index(end);
    int pass_days = end_index - start_index;
    for (int i = start.year; i < end.year; i++) {
        if (is_leap_year(i)) {
            pass_days += 366;
        }else {
            pass_days += 365;
        }
    }
    return pass_days;
}

MyDate count_date(MyDate & start, int pass_days) {



    int start_index = count_index(start);
    start_index += pass_days;
    int temp_year = start.year;
    int temp_month = 1;
    int temp_day = 0;

    int year_day;
    if (is_leap_year(temp_year)){
        year_day = 366;
    } else {
        year_day = 365;
    }

    while(start_index > year_day) {
        start_index -= year_day;
        temp_year++;
        if (is_leap_year(temp_year)){
            year_day = 366;
        } else {
            year_day = 365;
        }
    }

    int month_index = 0;
    if (is_leap_year(temp_year))
    {
        while(start_index > leap_month[month_index]) {
            temp_month++;
            start_index -= leap_month[month_index];
            month_index++;
        }
    } else {
        while(start_index > normal_month[month_index]) {
            temp_month++;
            start_index -= normal_month[month_index];
            month_index++;
        }
    }
    
    temp_day += start_index;

    MyDate result_date;
    result_date.year = temp_year;
    result_date.month = temp_month;
    result_date.day = temp_day;

    return result_date;

}

MyDate count_date_minus(MyDate & start, int pass_days) {



    int start_index = count_index(start);
    start_index -= pass_days;
    int temp_year = start.year;
    int temp_month = 1;
    int temp_day = 0;

    int year_day;
    if (is_leap_year(temp_year - 1)){
        year_day = 366;
    } else {
        year_day = 365;
    }

    while(start_index <= 0) {
        start_index += year_day;
        temp_year--;
        if (is_leap_year(temp_year)){
            year_day = 366;
        } else {
            year_day = 365;
        }
    }

    int month_index = 0;
    if (is_leap_year(temp_year))
    {
        while(start_index > leap_month[month_index]) {
            temp_month++;
            start_index -= leap_month[month_index];
            month_index++;
        }
    } else {
        while(start_index > normal_month[month_index]) {
            temp_month++;
            start_index -= normal_month[month_index];
            month_index++;
        }
    }

    temp_day += start_index;

    MyDate result_date;
    result_date.year = temp_year;
    result_date.month = temp_month;
    result_date.day = temp_day;

    return result_date;

}


int main() {

    MyDate now_date;
    int passeddays;

    cin >> now_date.year >> now_date.month >> now_date.day;
    cin >> passeddays;
    MyDate result_date = count_date_minus(now_date, passeddays);
    cout << result_date.year << " " << result_date.month << " " << result_date.day << endl;

    system("pause");
    return 0;
}

 

posted @ 2013-10-24 21:27  曹大啸  阅读(117)  评论(0)    收藏  举报