#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<Windows.h>
using namespace std;
class Date
{
public:
Date(int _year, int _month, int _day); //构造函数
void ShowDate()
{
cout << year << "-" << month << "-" << day << endl;
}
bool operator<(const Date& d); //小于运算符的重载
bool operator==(const Date& d); //等于运算符的重载
bool operator>(const Date& d); //大于运算符的重载
bool operator<=(const Date& d); //小于等于运算符的重载
bool operator>=(const Date& d); //大于等于运算符的重载
Date operator+(int day); //某个日期+天数
Date &operator+=(int day); //某个日期+=天数
Date operator-(int day); //某个日期-天数
Date &operator-=(int day); //某个日期-=天数
Date& operator++(); //某个日期++(前置)
Date operator++(int); //某个日期++(后置)
Date& operator--(); //某个日期--(前置)
Date operator--(int); //某个日期--(后置)
int operator-( Date& d); //两个日期相减
friend bool IsTrueDate(Date &d); //友元函数,判断某个日期是否是合法日期
private:
int year;
int month;
int day;
};
Date::Date(int _year, int _month, int _day) : // 用初始化列表初始化构造函数
year(_year),
month(_month),
day(_day)
{
}
bool Date::operator<(const Date& d) //小于运算符的重载
{
return this->year < d.year || this->year == d.year && this->month<d.month ||
this->year == d.year && this->month == d.month && this->day<d.day;
}
bool Date::operator==(const Date& d) //等于运算符的重载
{
return this->year == d.year && this->month == d.month && this->day == d.day;
}
bool Date::operator>(const Date& d) //大于运算符的重载
{
return this->year > d.year || this->year == d.year && this->month>d.month ||
this->year == d.year && this->month == d.month && this->day>d.day;
}
bool Date::operator<=(const Date& d) //小于等于运算符的重载
{
return !(*this > d);
}
//bool Date::operator<=(const Date& d) //小于等于运算符的重载
//{
// return *this < d && *this == d;
//}
bool Date::operator>=(const Date& d) //大于等于运算符的重载
{
return !(*this < d);
}
//bool Date::operator>=(const Date& d) //大于等于运算符的重载
//{
// return *this > d && *this == d;
//}
bool IsLeapYear(int year) //判断某年是否是闰年
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 1001 != 0)))
{
return true;
}
else
return false;
}
int GetMonthDay(int year, int month) //返回某个月的天数
{
int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = monthArray[month];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}
//日期计算器
Date& Date:: operator += (int day)
{
*this=*this + day;
return *this;
}
Date Date::operator+ (int day)
{
if (day<0)
{
*this -(-day);
}
Date tmp(*this);
if (IsTrueDate(*this))
{
tmp.day += day;
while (tmp.day > GetMonthDay(tmp.year, tmp.month))
{
tmp.day -= GetMonthDay(tmp.year, tmp.month);
if (tmp.month == 12)
{
tmp.year++;
tmp.month = 1;
}
else
{
tmp.month++;
}
}
}
else
{
cout << "The date is eror!" << endl;
tmp.year = 1900;
tmp.month = 1;
tmp.day = 1;
}
return tmp;
}
Date &Date::operator -= (int day)
{
*this = *this - day;
return *this;
}
Date Date::operator - (int day)
{
if (day<0)
{
*this + (-day);
}
Date tmp(*this);
if (IsTrueDate(*this))
{
tmp.day -= day;
while (tmp.day <= 0)
{
if (tmp.month == 1)
{
tmp.month = 12;
tmp.year--;
}
else
{
tmp.month--;
}
tmp.day += GetMonthDay(tmp.year, tmp.month);
}
}
return tmp;
}
Date Date::operator--(int) //后置--
{
Date tmp(*this);
*this -= 1;
return tmp;
}
Date Date::operator++(int) //后置++
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& Date::operator--() //前置--
{
*this -= 1;
return *this;
}
Date& Date::operator++() //前置++
{
*this += 1;
return *this;
}
bool IsTrueDate(Date &d)
{
if (d.year > 1900 && d.month > 0 && d.month<13 && d.day>0 &&
d.day <= GetMonthDay(d.year, d.month))
{
return true;
}
else
{
cout << "The date is eror!" << endl;
d.year = 1900;
d.month = 1;
d.day = 1;
return false;
}
}
int Date::operator - ( Date& d)
{
int days=0;
int flag = 1;
if (IsTrueDate(*this) && IsTrueDate(d))
{
while (!(*this == d))
{
if (*this > d)
{
d.day++;
days++;
}
else
{
this->day++;
days++;
flag = -1;
}
}
}
return flag * days;
}
本文出自 “零点时光” 博客,请务必保留此出处http://10741764.blog.51cto.com/10731764/1743323
原文地址:http://10741764.blog.51cto.com/10731764/1743323