日期类
#pragma once
#include<iostream>
using namespace std;
class Date {
public:
//将输入输出重载函数定义为友元函数,该函数才能访问该类的成员及数据
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
//全缺省的默认构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
//若日期不符合要求置为默认值1-1-1
if (year<1||month<1||month>12||day <1 || (day > getMonthday(year,month)))
{
_year = 1;
_month = 1;
_day = 1;
cout << "你输入的日期有误,已重置为默认值1-1-1" << endl;
}
else
{
_year = year;
_month = month;
_day = day;
}
}
//拷贝构造函数,利用初始化列表进行拷贝,提高效率
Date(const Date& d)
:_year(d._year)
, _month(d._month)
, _day(d._day)
{
}
//赋值操作符重载
Date& operator=(const Date& d) {
if (this != &d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//+=操作符重载
Date operator+=(int days)
{
//如果days为负,则加一个负数等于减去一个正数,
//直接调用重载过的-=
if (days < 0)
return *this -= (-days);
_day += days;
while (_day> getMonthday(_year, _month))
{
_day = _day - getMonthday(_year, _month);
_month += 1;
if (_month > 12)
{
_year += 1;
_month = 1;
}
}
return *this;
}
//-=操作符重载
Date operator-=(int days)
{
if (days < 0)
return *this += (-days);
_day -= days;
while(_day <= 0)
{
_month -= 1;
if (_month <= 0)
{
_year -= 1;
_month = 12;
if (_year <= 0)
{
_year = 1;
_month = 1;
_day = 1;
}
}
_day = getMonthday(_year,_month)+ _day;
}
return *this;
}
//计算当前对象(this*)与d对象之间相差的天数
int operator-(const Date& d)
{
Date min, max;
int flag=0;
int count = 0;
if (*this > d)
{
min = d;
max = *this;
flag = 1;
}
else if (*this < d)
{
min = *this;
max = d;
flag = -1;
}
else
{
return 0;
}
while (max > min)
{
--max;
count++;
}
return count*flag;
}
//++x
Date& operator++()
{
*this += 1;
return *this;
}
//x++
Date operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
//--x
Date& operator--()
{
*this -= 1;
return *this;
}
//x--
Date operator--(int) {
Date tmp = *this;
*this -= 1;
return tmp;
}
bool operator>(const Date& d)const
{
if (_year > d._year)
return true;
if (_year < d._year)
return false;
if (_month > d._month)
return true;
if (_month < d._month)
return false;
if (_day > d._day)
return true;
if (_day < d._day)
return false;
return false;
}
bool operator>=(const Date& d)const
{
return (*this > d) || *this == d;
}
bool operator<(const Date& d)const
{
return !(*this >= d);
}
bool operator<=(const Date& d)const
{
return (*this < d) || *this == d;
}
bool operator==(const Date& d)const
{
return _day == d._day && _month == d._month && _day == d._day;
}
bool operator!=(const Date& d)const
{
return _day != d._day && _month != d._month && _day != d._day;
}
//获取当前月份的天数
int getMonthday(int year,int month)
{
int m = 0;
int a[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
if (month == 2) {
//若为闰年2月加一天
return a[month - 1] + 1;
}
}
return a[month - 1];
}
private:
int _year;
int _month;
int _day;
};
//输出操作符重载
ostream& operator<<(ostream& _cout, const Date& d)
{
cout << d._year << '-' << d._month << '-' << d._day << endl;
return cout;
}
//输入操作符重载
istream& operator>>(istream& _cin, Date& d)
{
cin >> d._year >> d._month >> d._day;
return cin;
}
//测试函数
void testdate() {
Date d;
cout << d;
cin >> d;
cout << d;
Date d1(2019,10, 28);
cout << d1;
cout << (d1 < d)<<endl;
d1 += 15;
cout << d1;
d1 -= 15;
cout << d1;
cout << d-d1;
}
int main() {
testdate();
return 0;
}