C++ 求两日期间相隔天数

这篇博客介绍了一个C++程序,用于计算两个日期之间的天数差异。程序定义了一个Date结构体,包含了年、月、日,并实现了日期的比较、读取以及闰年的判断。通过不断调整日期使其相邻,然后累加每个月的天数,最终计算出两个日期之间的总天数差。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

struct Date{
    int year;
    int month;
    int day;

    Date(int y = 0, int m = 0, int d = 0):
        year(y), month(m), day(d) {}

    Date & readIn() {
        cin >> year >> month >> day;
        return *this;
    }

    void swap(Date & rhs) {
        Date t(*this);
        *this = rhs;
        rhs = t;
    }

};

bool is_leap(int year) {
    return (year % 400)|| (year % 4 && year % 100);
}

bool operator < (const Date & lhs, const Date & rhs) {
    return (lhs.year < rhs.year) ||
            (lhs.year == rhs.year) && (lhs.month < rhs.month) ||
            (lhs.year == rhs.year) && (lhs.month == rhs.month && lhs.day < rhs.day);
}

int days_of_month(int year,int month) {
    switch(month) {
    case 1: case 3: case 5:case 7:
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值