日历计算(C++类实现)

type1:实现某年某月某日往前或往后N天后日历的输出。
type2:实现两个日期之间相差天数的计算。

#include <iostream>
using namespace std;

int monthDays[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

class Cdata{
public:
    int year, month, day;

public:
    inline bool isrun(const int year) {
        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }

    int calculate1(int iyear, int imonth, int iday, int distance = 0) {
        year = 1840;
        for(int y = 1840; y < iyear; ++y) {
            if(isrun(y)) {
                distance += 366;
            } else {
                distance += 365;
            }
        }
        if(isrun(iyear)) {
            monthDays[2] = 29;
        } else {
            monthDays[2] = 28;
        }
        for(int m = 1; m < imonth; ++m) {
            distance += monthDays[m];
        }
        distance += iday;
        int res = distance;

        while(true) {
            if(isrun(year)) {   // calculate year
                if(distance <= 366) {
                    break;
                } else {
                    distance -= 366;
                    ++year;
                }
            } else {
                if(distance <= 365) {
                    break;
                } else {
                    distance -= 365;
                    ++year;
                }
            }
        }

        for(int m = 1; m < 13; ++m) {       // calculate month
            if(m ==  1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
                if(distance <= 31) {
                    break;
                }
            }else if(m == 4 || m == 6 || m == 9 || m == 11) {
                if(distance <= 30) {
                    break;
                }
            } else if(m == 2) {
                if(isrun(year)) {
                    monthDays[2] = 29;
                } else {
                    monthDays[2] = 28;
                }
                if(distance <= monthDays[2]) {
                    break;
                }
            }
            distance -= monthDays[m];
            ++month;
        }

        day = distance;

        return res;
    }

    int calculate2(int& year1, int& month1, int& day1, int& year2, int& month2, int& day2) {
        int res1 = calculate1(year1, month1, day1);
        int res2 = calculate1(year2, month2, day2);
        return res2 - res1;
    }
};

int main() {
    int type, y1, m1, d1, y2, m2, d2, dis;
    Cdata a;
    cin >> type;
    while(1) {
        if(type == 1) {
            cin >> y1 >> m1 >> d1 >> dis;
            a.calculate1(y1, m1, d1, dis);
            cout << "year:  " << a.year << endl
                 << "month: " << a.month << endl
                 << "day:   " << a.day << endl;
        } else if(type == 2) {
            cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
            cout << a.calculate2(y1, m1, d1, y2, m2, d2) << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值