【日期类】C++实现

来源:互联网 发布:红色痘印怎么消除 知乎 编辑:程序博客网 时间:2024/06/08 01:17

在日常生活中,我们经常会碰到与日期计算相关的问题,模拟实现一个简易的日期类就很方便的解决了一些类似的问题。

下面实现的日期类主要包括以下基本的几个操作:

(1):days天之后的日期

(2):days天之前的日期

(3):两个日期之间相差的天数

(4):日期的相互赋值

首先给出了日期类所包含的函数声明模块:

class Date{public:Date(int year = 2017, int month = 9, int day = 10): _year(year), _month(month), _day(day){}Date& operator=(const Date& d);// 前置++ Date& operator++();// 后置++ Date operator++(int);Date& operator--();Date operator--(int);//days天之后的日期 Date operator+(int days);// days天之前的日期 Date operator-(int days);// 两个日期之间的距离 int operator-(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);bool operator>(const Date& d);bool operator<(const Date& d);bool IsLeepyear(int year)const{return((_year % 400 == 0) || ((_year % 4 == 0)&&(_year % 100 != 0)));}int GetMonthdays(int year,int month){int months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (IsLeepyear(year)==1){months[2] = 29;}return months[month];}void display();private:int _year;int _month;int _day;};

显然,我们都知道每个月的天数都是固定的,所以在实现日期类的运算时,库函数里的加家减减运算已经不能满足我们的需要了,这就需要我们自己写出满足条件的重载运算符啦

Date& Date::operator=(const Date &d){if (*this!=d){_year = d._year;_month = d._month;_day = d._day;}return *this;}//前置加加Date&Date::operator ++(){*this = *this + 1;return *this;}//后置++Date Date::operator++(int){Date tmp;tmp = *this;*this = *this + 1;return tmp;}//前置--Date&Date::operator--(){*this = *this - 1;return *this;}//后置--Date Date::operator--(int){Date temp(*this);*this = *this - 1;return temp;}

在计算days天之后或者是之前的日期,需要对_day,_month的合法性判断,这也是日期类中比较复杂的一类计算了,具体的分析请看以下代码的实现:

//days天之后的日期 Date Date:: operator+(int days){Date tmp(*this);if (days < 0){days = -days;return tmp - days;}tmp._day += days;while (tmp._day>GetMonthdays(tmp._year, tmp._month)){tmp._day -= GetMonthdays(tmp._year, tmp._month);if (tmp._month == 12){tmp._year++;tmp._month = 1;}else{tmp._month++;}}return tmp;}// days天之前的日期 Date Date:: operator-(int days){Date tmp(*this);if (days < 0){days = -days;return tmp - days;}tmp._day -= days;while (tmp._day < 0){if (tmp._month == 1){tmp._month--;tmp._year = 12;}else{tmp._month--;}tmp._day = tmp._day + GetMonthdays(tmp._year, tmp._month);}return tmp;}int Date::operator-(const Date&d){int days = 0;Date Maxdate(*this);Date Mindate(d);if (Maxdate < Mindate){Mindate = *this;Maxdate = d;}while ((Mindate+days)<Maxdate){days++;}return days;}

最后就是对日期的大小判断了,就比较简单了

bool Date::operator==(const Date&d)//判断两个日期是否相等{return ((_year == d._year) && (_month == d._month) && (_day == d._day));}bool Date::operator!=(const Date&d)//{return (_year != d._year) || (_month != d._month) || (_day != d._day);}bool Date::operator>(const Date&d){if ((_year>d._year)||((_year==d._year)&&(_month>d._month))||((_year==d._year)&&(_month==d._month)&&(_day>d._day)))return true;else                                                                                                                     return false;}bool Date::operator<(const Date&d){if ((_year<d._year) || ((_year == d._year) && (_month<d._month))|| ((_year == d._year) && (_month == d._month) && (_day<d._day)))return true;elsereturn false;}
主函数部分的测试代码:

void FunTest(){int days = 0;Date d1(2017, 9, 20);Date d2(2011, 9, 23);d1++;d1.display();Date d3;d3 = d1 + 10;d3.display();cout << d2-d1 << endl;}int main() {FunTest();system("pause");return 0;}