日期类

    xiaoxiao2021-03-25  87

    1.关于日期d1-d2所得差值的思路总结:

    方法一:重载一个自增运算符,然后使用计数器来计算两个日期之间的差值。 因为比较简单,就直接看代码!

    int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2) { int flag = 1; if (d1 > *this) { flag = -flag; } int count = 0; Date minDate = *this; Date maxDate = d1; if (minDate > maxDate) { Swap(minDate, maxDate); } while (minDate != maxDate) { minDate++; count++; } return flag * count; }

    第二种方法: ①当d1和d2是同年且同月的日期时: 结果为return d1._day - d2._day; ②当d1和d2是同年但不同月时: 则用d1当年的天数减去d2当年的天数。 ③当d1和d2不同年时(比较复杂,需仔细分析): 首先,定义几个变量

    int remainDays = 0;//小的日期在当年剩余的天数(到年底) int dayInYear = 0;//大的日期在当年的天数 int d_value = 0;//两个日期年之间相差的天数 令变量minDate表示小的日期; maxDate表示大的日期。 1)首先,求的remainDays的值(通过minDate) 当minDate日期为闰年时:remainDays = 366 - 小日期当年的天数 否则:remianDays = 365 - 小日期当年的天数 2)求的dayInYear的值(通过maxDate) dayInYear = 大的日期在当年的天数 3)求的d_value的值(通过minDate和maxDate):

    //伪代码如下:

    int days = 0; for (int year = minDate._year + 1; year < maxDate._year; year++) { if (year是闰年) { d_values += 366; } else { d_values += 365; } }

    最后:返回remainDays+dayInYear+d_value的和。

    代码如下:

    int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2) { int flag = 1; if (d1 > *this) { flag = -flag; } int remainDays = 0;//小的日期在当年剩余的天数(到年底) int dayInYear = 0;//大的日期在当年的天数 int d_value = 0;//两个日期的年之间相差的天数 Date d2(*this); assert(IsValid(d1) != 0);//判断两个日期的有效性 assert(IsValid(d2) != 0); if ((d1.m_year == d2.m_year) && (d1.m_month == d2.m_month))//当两个日期是同年且同月 { return d1.m_day - d2.m_day; } else if (d1.m_year == d2.m_year)//当两个日期是同年但不同月 { return (DayInYear(d1) - DayInYear(d2)); } else//当两个日期不同年 { Date minDate = d1; Date maxDate = d2; if (minDate > maxDate) { Swap(minDate, maxDate); } if (IsLeapYear(minDate.m_year)) { remainDays = 366 - DayInYear(minDate); } else { remainDays = 365 - DayInYear(minDate); } dayInYear = DayInYear(maxDate);//获得大的日期在当年的天数 //获取两个日期的年之间的差值 for (int year = minDate.m_year + 1; year < maxDate.m_year; year++) { if (IsValid(year)) { d_value += 366; } else { d_value += 365; } } } return flag * (d_value + remainDays + dayInYear); }

    最后,总结一下,在vs2013下,我经过测试,发现第一种方法程序的运行速度要快于第二种很多,而且也可以看出第二种方法比较复杂,考虑的情况比较多,所以建议大家最好写第一种代码,第二种了解就好。

    我将日期类中所有可能需要实现的代码贴出来,欢迎大家参考(笑脸)

    //Date.h

    #pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include<cassert> #include<ctime> class Date { public : Date(int year = 1990, int month = 1, int day = 1); Date operator+(int day); Date& operator+=(int day); Date operator-(int day); Date& operator-=(int day); int operator-(const Date& d); Date& operator++();//前置++ Date operator++(int);//后置++ Date& operator--();//前置-- Date operator--(int);//后置-- void Dispaly(); int Date::DayInYear(const Date& d); bool IsLeapYear(int year); friend void Swap(Date& d1, Date& d2); friend bool operator>(const Date& d1, const Date& d2); friend bool operator<(const Date& d1, const Date& d2); friend bool operator==(const Date&d1, const Date& d2); friend bool operator!=(const Date&d1, const Date& d2); protected: bool IsValid(const Date& d) { if (d.m_year > 0 && (d.m_month > 0 && d.m_month < 13) && (d.m_day > 0 && d.m_day <= GetMonthDays(d.m_year, d.m_month))) { return true; } return false; } int GetMonthDays(int year, int month) { static int MonthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month != 2) { return MonthDays[month]; } else if (IsLeapYear(year))//是闰年 { return 29; } else return 28; } private : int m_year; int m_month; int m_day; };

    //Date.cpp

    #include"Date1.h" Date::Date(int year, int month, int day)//构造函数 :m_year(year) , m_month(month) , m_day(day) { assert(IsValid(*this) != 0); } Date Date::operator+(int day)//某个日期加上一个天数(形如:d1+100) { Date tmp(*this); tmp.m_day += day; while (tmp.m_day > GetMonthDays(tmp.m_year, tmp.m_month)) { tmp.m_day -= GetMonthDays(tmp.m_year, tmp.m_month); if (tmp.m_month == 12) { tmp.m_year++; tmp.m_month = 1; } else { tmp.m_month++; } } return tmp; } Date& Date::operator+=(int day)//注意与operator+之间的区别(一个返回引用,一个返回值) { *this = *this + day; return *this; } Date& Date::operator++()//前置++ { *this = *this + 1; return *this; } Date Date::operator++(int)//后置++ { Date tmp(*this); *this = *this + 1; return tmp; } Date Date::operator-(int day)//某个日期减去某个天数(形如:d1-100) { Date tmp(*this); tmp.m_day -= day; while (!IsValid(tmp)) { if (tmp.m_month == 1) { tmp.m_year--; tmp.m_month = 12; } else { tmp.m_month--; } tmp.m_day += GetMonthDays(tmp.m_year, tmp.m_month); } return tmp; } Date& Date::operator-=(int day) { *this = *this - day; return *this; } bool operator>(const Date& d1, const Date& d2)//重载> { if ((d1.m_year > d2.m_year) \ || (d1.m_year == d2.m_year) && ((d1.m_month > d2.m_month) || ((d1.m_month == d2.m_month) && (d1.m_day > d2.m_day)))) { return true; } return false; } bool operator<(const Date& d1, const Date& d2)//重载< { return !((d1 > d2) || (d1 == d2)); } bool operator!=(const Date&d1, const Date& d2)//重载!= { return !(d1 == d2); } bool operator==(const Date&d1, const Date& d2)//重载== { if ((d1.m_year == d2.m_year) && (d1.m_month == d2.m_month) && (d1.m_day == d2.m_day)) { return true; } return false; } // //int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2) //{ // int flag = 1; // if (d1 > *this) // { // flag = -flag; // } // int count = 0; // Date minDate = *this; // Date maxDate = d1; // if (minDate > maxDate) // { // Swap(minDate, maxDate); // } // while (minDate != maxDate) // { // minDate++; // count++; // } // return flag * count; // //} int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2) { int flag = 1; if (d1 > *this) { flag = -flag; } int remainDays = 0;//小的日期在当年剩余的天数(到年底) int dayInYear = 0;//大的日期在当年的天数 int d_value = 0;//两个日期的年之间相差的天数 Date d2(*this); assert(IsValid(d1) != 0);//判断两个日期的有效性 assert(IsValid(d2) != 0); if ((d1.m_year == d2.m_year) && (d1.m_month == d2.m_month))//当两个日期是同年且同月 { return d1.m_day - d2.m_day; } else if (d1.m_year == d2.m_year)//当两个日期是同年但不同月 { return (DayInYear(d1) - DayInYear(d2)); } else//当两个日期不同年 { Date minDate = d1; Date maxDate = d2; if (minDate > maxDate) { Swap(minDate, maxDate); } if (IsLeapYear(minDate.m_year)) { remainDays = 366 - DayInYear(minDate); } else { remainDays = 365 - DayInYear(minDate); } dayInYear = DayInYear(maxDate);//获得大的日期在当年的天数 //获取两个日期的年之间的差值 for (int year = minDate.m_year + 1; year < maxDate.m_year; year++) { if (IsValid(year)) { d_value += 366; } else { d_value += 365; } } } return flag * (d_value + remainDays + dayInYear); } Date& Date::operator--()//前置-- { *this = *this - 1; return *this; } Date Date::operator--(int)//后置-- { Date tmp(*this); *this = *this - 1; return tmp; } void Swap(Date& d1, Date& d2)//交换两个日期 { int tmp = d1.m_year; d1.m_year = d2.m_year; d2.m_year = tmp; tmp = d1.m_month; d1.m_month = d2.m_month; d2.m_month = tmp; tmp = d1.m_day; d1.m_day = d2.m_day; d2.m_day = tmp; } int Date::DayInYear(const Date& d)//获取当年的天数 { assert(IsValid(d) != 0); int days = d.m_day; for (int month = 1; month < d.m_month; month++) { days += GetMonthDays(d.m_year, month); } return days; } bool Date::IsLeapYear(int year)//判断闰年 { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 100 == 0)) { return true; } return false; } void Date::Dispaly()//输出日期 { cout << m_year << "-" << m_month << "-" << m_day << endl; } //测试 void Test() { Date d1(2017, 2, 28); Date d2; d2 = d1 + 100; d2.Dispaly(); d2 = d2 - 100; d2.Dispaly(); } void Test2() { Date d2(2017, 2, 28); Date d1(2015, 3, 8); int start = clock(); int day = d1 - d2; int end = clock(); cout << (end - start) << endl; cout << day << endl; } void Test3() { Date d2(2017, 2, 28); d2.Dispaly(); d2++.Dispaly(); Date d1(2015, 3, 8); d1.Dispaly(); ++d1; d1.Dispaly(); } void Test4() { Date d2(2017, 2, 28); d2.Dispaly(); d2--.Dispaly(); Date d1(2015, 3, 8); --d1; d1.Dispaly(); } void Test5() { Date d1(2015, 3, 8); d1 += 100; d1.Dispaly(); d1 -= 100; d1.Dispaly(); } void Test6() { Date d1(2015, 3, 8); Date d2(2017, 2, 28); cout << (d1 < d2) << endl; cout << (d1 > d2) << endl; cout << (d1 == d2) << endl; } int main() { //Test(); //Test2(); //Test3(); //Test4(); //Test5(); Test6(); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-36948.html

    最新回复(0)