目录
- 类的定义
- 确定某年某月有多少天
- 构造函数
- 打印日期
- 日期+=天数
- 日期+天数
- 日期-=天数
- 日期-天数
- 前置++
- 后置++
- 后置--
- 前置--
- >运算符重载
- ==运算符重载
- >=运算符重载
- <运算符重载
- <=运算符重载
- !=运算符重载
- 计算两个日期之间的间隔天数,日期减去日期
类的定义
#pragma once
#include < iostream>
using std::cout;
using std::cin;
using std::endl;
class Date
{
public:
int GetMonthDay(int year, int month);
Date(int year = 0, int month = 1, int day = 1);
void Print();
Date& operator+=(int day);
Date operator+(int day);
Date operator-(int day);
Date& operator-=(int day);
Date& operator++();
Date operator++(int);
Date operator--(int);
Date& operator--();
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);
bool operator != (const Date& d);
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
确定某年某月有多少天
inline int Date::GetMonthDay(int year, int month) {
static int arr[13] = {0,31,28,31,30,31,30,31,31,30,31, 30, 31};
int day = arr[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
day = 29;
return day;
}
构造函数
Date::Date(int year, int month, int day) {
if (year >= 0 && month > 0 && month < 13
&& day >0 && day <= GetMonthDay(year, month)) {
_year = year;
_month = month;
_day = day;
}
else {
cout << "非法日期" << endl;
cout << year << "年" << month << "月" << day << "日" << endl;
}
}
打印日期
void Date::Print() {
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
日期+=天数
Date& Date::operator+=(int day) {
if (day < 0){
*this -= (-day);
}
else {
_day += day;
while (_day > GetMonthDay(_year, _month)) {
_day -= GetMonthDay(_year, _month);
++_month;
if (_month > 12) {
++_year;
_month = 1;
}
}
}
return *this;
}
日期+天数
Date Date::operator+(int day) {
Date ret(*this);
ret += day;
return ret;
}
日期-=天数
Date& Date::operator-=(int day) {
if (day < 0) {
*this += (-day);
}
else {
_day -= day;
while (_day <= 0) {
--_month;
if (_month < 1) {
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
}
return *this;
}
日期-天数
Date Date::operator-(int day) {
Date ret = *this;
ret -= day;
return ret;
}
前置++
Date& Date::operator++() {
*this += 1;
return *this;
}
后置++
Date Date::operator++(int) {
Date ret(*this);
*this += 1;
return ret;
}
后置–
Date Date::operator--(int) {
Date ret = *this;
*this -= 1;
return ret;
}
前置–
返回--后的对象
Date& Date::operator--() {
*this -= 1;
return *this;
}
>运算符重载
d1 > d2 -> d1.operator>(&d1, d2)
bool Date::operator>(const Date& d) {
if (_year >= d._year)
{
if (_year > d._year)
return true;
else {
if (_month >= d._month) {
if (_month > d._month)
return true;
else {
if (_day > d._day)
return true;
}
}
}
}
return false;
}
==运算符重载
bool Date::operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
>=运算符重载
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);
}
!=运算符重载
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
计算两个日期之间的间隔天数,日期减去日期
int Date::operator-(const Date& d) {
Date min = *this;
Date max = d;
int flag = -1;
int daycount = 0;
if (*this > d) {
min = d;
max = *this;
flag = 1;
}
while (min != max) {
++min;
daycount++;
}
return flag * daycount;
}