天天看點

實作 日期 的運算---C++

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

#include<assert.h>

using namespace std;

class Date

{

public:

void display()

cout << _year << "-" << _month << "-" << _day << endl;

}

bool IsLeapyear(int year)

return ((year / 400 == 0) || (year / 4 == 0 && year / 100 != 0));

int GetMonthDay(int year, int month)

if (month > 12 || month < 1)

printf("無此月\n");

return -1;

else

int dayarr[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if (IsLeapyear(year) && month == 2)

return 29;

return dayarr[month - 1];

Date& operator+(const int day)

if (day < 0)

return *this - (-day);

_day += day;

while (_day > GetMonthDay(_year, _month))

_day -= GetMonthDay(_year, _month);

if (_month == 12)

_year += 1;

_month = 1;

_month += 1;

return *this;

Date& operator-(int day)

return *this + (-day);

_day -= day;

while (_day <= 0)//注意   等于    的時候

if (_month == 1)

_year -= 1;

_month = 12;

_month -= 1;

_day += GetMonthDay(_year, _month);

Date operator++(int day)//後置加加

Date tem = *this;

*this = *this + 1;

return tem;

Date& operator++()//前置加加

Date& operator--(int day)//後置減減

*this = *this - 1;

Date& operator--()//前置減減

Date& operator+=(const int day)

*this = *this + day;

Date& operator-=(const int day)

*this = *this - day;

bool operator>(const Date& cur)

if (_year > cur._year)

return 1;

else if (_year == cur._year)

if (_month > cur._month)

else if (_month == cur._month)

if (_day > cur._day)

return 0;

bool operator==(const Date& cur)

if (_year == cur._year

&& _month == cur._month

&& _day == cur._day)

bool operator<(const Date& cur)

if (!(*this > cur || *this == cur))

bool operator>=(const Date& cur)

if (*this > cur || *this == cur)

bool operator<=(const Date& cur)

if (*this < cur || *this == cur)

int operator-(const Date& cur)

int count = 0;

Date max;

Date min;

if (*this > cur)

max = *this;

min = cur;

else if (*this < cur)

max = cur;

min = *this;

while (1)

if (min == max)

return count;

min = min + 1;

count++;

Date(int year = 1990, int month = 1, int day = 1)

:_year(year)

, _month(month)

, _day(day)

{}

Date(const Date& a)

:_year(a._year)

,_month(a._month)

, _day(a._day)

~Date()

Date& operator=(const Date& cur)

_year = cur._year;

_month = cur._month;

_day = cur._day;

private:

int _year;

int _month;

int _day;

};

//operator+ operator-

//operator++ operator--

//operator+= operator-=

void test1()

Date cur(2015, 11, 11);

//(cur + 18).display();

//(cur + 19).display();

//(cur + 20).display();

//(cur + (-30)).display();

//(cur - 10).display();

//(cur - 11).display();

//(cur - 12).display();

//(cur - (-18)).display();

//operator>  <  ==  >=  <=

void test2()

Date cur1(2015, 11, 13);

Date cur2(2015, 11, 12);

cout << (cur1 > cur2) << endl;

cout << (cur1 == cur2) << endl;

//operator-(const Date& cur)

void test3()

Date date1(2015, 11, 11);

Date date2(2015, 11, 1);

Date date3(2015, 11, 22);

cout << date1 - date2 << endl;

cout << date1 - date3 << endl;

int main()

//test1();

//test2();

test3();

system("pause");

本文轉自 ye小灰灰  51CTO部落格,原文連結:http://blog.51cto.com/10704527/1719279,如需轉載請自行聯系原作者

繼續閱讀