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

#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:
    case 8: case 10: case 12:
        return 31;
    case 2:
        return is_leap(year) ? 29: 28;

    case 4: case 6: case 9: case 11:
        return 30;
    default:
        return -1;
    }
}

int day_diff(Date lhs, Date rhs) {
<span style="white-space:pre">	</span>//通过不断用其中一个向另一个逼近来求相差天数
    int flag = 1;
    if(rhs < lhs) {
        swap(lhs, rhs);
        flag = -1;
    }

    int day_shf = 0;
    if(lhs.day < rhs.day) {
        day_shf += rhs.day - lhs.day;
        rhs.day = lhs.day;
    }
    if(lhs.day > rhs.day) {
        day_shf -= lhs.day - rhs.day;
        lhs.day = rhs.day;
    }

    while(lhs.month < rhs.month) {
        day_shf += days_of_month(lhs.year ,lhs.month);
        ++lhs.month;
    }
    while(lhs.month > rhs.month) {
        day_shf -= days_of_month(rhs.year ,rhs.month);
        ++rhs.month;
    }
<span style="white-space:pre">	</span>//两个日期始终以相互为界,故日期大小关系不可能改变
    while(lhs.year < rhs.year) {
        day_shf += is_leap(lhs.year) ? 366: 365;
        ++lhs.year;
    }

    return day_shf*flag;
}

int main()
{
    Date d1, d2;
    while(true) {
        d1.readIn(); d2.readIn();
        cout << day_diff(d1, d2) << endl;
    }
    return 0;
}
时间: 2024-08-11 03:25:33

C++ 求两日期间相隔天数的相关文章

输入一个日期,判断这个日期在一年中是哪一天,是星期几,计算两个日期间的天数,使用字符串输出日期

之前写了一个博文(http://blog.csdn.net/shiwazone/article/details/45053739)是用基本函数实现的,这次使用类的设计方法,也就是面向对象的方法改写一下,并加入了日期转换成字符串的实现.这里的程序也可以解决编程珠玑习题3.4的问题. #include"calendar.h" int main() { Time t; t.initialTime(); t.Show(); t.StrShow(); Time t1; t1.initialTim

Java 两个日期间的天数计算

在Java中计算两个日期间的天数,大致有2种方法:一是使用原生JDK进行计算,在JDK8中提供了更为直接和完善的方法:二是使用第三方库. 1.使用原生的JDK [java] view plain copy print? private static long daysBetween(Date one, Date two) { long difference =  (one.getTime()-two.getTime())/86400000; return Math.abs(difference)

计算两日期间2月29日总数的Java程序

事先声明,本人仅仅是个计算机领域的新手,不久前开始学习Java.后来我接到了一份关于计算两日期间2月29日总数的编程作业,仓促之中我便写下了这个程序.由于之前可以说毫无编程经验,Java也仅仅是只学了一小部分.所以目前该程序尚未解决输入问题. 我解决这个问题的思路是: 编写一个方法(leapYear)用于判断某一年份是否为闰年: 编写另一个方法(dateExist)用于判断某一时期是否真实存在,在这方法中会引用到上个方法: 最后编写main方法,引用方法(dateExist)分别判断起始日期和终

求两个时间的天数差 日期格式为 YYYY-MM-dd

//+---------------------------------------------------  //| 求两个时间的天数差 日期格式为 YYYY-MM-dd   //+---------------------------------------------------  function daysBetween(DateOne,DateTwo)  {       var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-

js 计算两个日期间的天数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

计算两个日期相隔天数 思路:假设1998-10-10 2010-5-5 首先获取1889-10-10这个日期在这一年中还剩多少天 再次获取2010-5-5这个日子在这一年内已经过了

class FunDemo6 { public static void main(String[] args) { //测试函数getDays //System.out.println(getDays(1992,4,20)); System.out.println(subDays(1999,1,5,2001,3,10)); } //判断是否为闰年 public static boolean isLeap (int y) { if(y%4==0&&y%100!=0||y%400==0) re

java日期间相隔年月日计算

/**     * 获取date1相距date2多少天, date2>date1     * @param date1     * @param date2     * @return     * @throws ParseException     */    public static int getDaysSpace(String date1,String date2){        Calendar cal = Calendar.getInstance();        cal.se

C/C++输入两个任意日期求相隔天数

将两个日期转换成与一个指定日期(例1970-01-01)之间的差然后计算 思路: 两个日期相隔天数的计算,首先可以将两个日期转换成time_t(从指定日期至1970年1月1日0时0分0秒相隔的秒数),然后计算两个time_t的秒数差,最后用此秒数差除以24*3600秒就可以得到相隔的天数.所以程序中需要建立两个函数,一个是将日期转换成time_t的函数,一个是计算日期相隔天数的函数. 例: 1)建立程序的主体结构: 1 #include<stdio.h> 2 #include<stdli

java计算两个日期之间相差天数和相隔天数详解

大家看到文章标题"两个日期之间相差天数和相隔天数",是否有疑惑呢!从中文字面理解,"相差"和"相隔"是有区别的,然而就是这些区别害死很多人,却没有发现,在大量新增统计时是差之毫厘谬以千里,我能都发现是因为一个偶然的机会,一个项目运行几年却没有人发现,我在其中还不到一年,一开始写这些这代码的人根本没分清楚什么情况就写了,怪不得统计的数据总是有那么细微的差别,在于日期"相差"和"相隔"有某些特定的情况下是相等的