定义
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1);
void Print() const;
int GetMonthDay(int year, int month);
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);
Date& operator+=(const int day);
Date operator+(const int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date& operator-=(int day);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
//前置++
Date& operator++();
//后置++
Date operator++(int);
// 日期-日期 返回天数
int operator-(const Date& d);
//输出该天星期几
void PrintWeekDay();
/*void operator<<(ostream& out);*/
//友元函数,函数的声明必须放在类里面
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
};
实现
#include"Date.h"
int Date::GetMonthDay(int year, int month)
{
static int MonthDayArrary[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = MonthDayArrary[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!(year >= 0
&& (month > 0 && month < 13)
&& (day > 0 && day <= GetMonthDay(year, month))))
{
cout << "非法日期" << endl;
Print();
}
}
void Date::Print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
else
{
return false;
}
}
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 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;
//return !(*this == d);
}
//+=
Date& Date::operator+=(const int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
//+
Date Date::operator+(const int day)
{
Date ret(*this);
//ret.operator+=(day);
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 day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
// 后置--
Date Date::operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
// 前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (max != min)
{
min++;
count++;
}
return count * flag;
}
void Date::PrintWeekDay()
{
const char* arr[] = { "星期一", "星期二", "星期三", "星期四", "星期五","星期六","星期天" };
Date start(1, 1, 1);
int count = *this - start;
cout << arr[count % 7] << endl;
}
//void Date::operator<<(ostream& out)
//{
// out << _year << "/" << _month << "/" << _day;
//}
ostream& operator<<(ostream& out, const Date& d)
{
cout << d._year << "/" << d._month << "/" << d._day << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cin >> d._year;
cin >> d._month;
cin >> d._day;
return in;
}