#include <iostream>
using namespace std;
// 1. 要考虑到日期的合法性
// 2. 考考虑闰年和非闰年
// 3. 需要用到其他辅助函数自己可以添加
class Date
{
public:
Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{
// 检查如果输入参数是非法时间,则初始化为1990-1-1
if (check_is_valid_date() == false)
{
year = 1990;
month = 1;
day = 1;
}
cout << "Date(int,int,int): " << endl;
}
Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{
cout << "Date(const Date& ): " << endl;
}
// 判断是否为瑞年
static bool is_leap_year(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return true;
}
return false;
}
// 判断每个月的天数
static int day_of_month(int year, int month)
{
if (year < 1 || month < 1 || month > 12)
{
return 0;
}
int month_days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//if (is_leap_year(year) == true)
//{
// month_days[2] += 1;
//}
month_days[2] += is_leap_year(year);
return month_days[month];
}
// 检查时间是否有效
bool check_is_valid_date()
{
if (_year < 1 || (_month < 1 || _month > 12) || (_day < 1 || _day > day_of_month(_year, _month)))
{
return false;
}
return true;
}
// 打印日期
void display()
{
cout << _year << "-" << _month << "-" << _day << endl << endl;
}
// 求当前日期第days天后是哪一天?
Date operator+(int days)
{
Date temp(*this); // 用this创建一个临时对象
if (days < 0)
{
return temp - (-days);
}
temp._day += days;
while (temp._day > day_of_month(_year, _month)) // 循环条件:当总天数大于一个月的天数时
{
temp._day -= day_of_month(_year, _month); // 没有 -= 之前这个时候 temp._day 是一个大于月份的数
if (temp._month == 12) // 当月份等于12时
{
temp._year++; // 年份加一
temp._month = 1; // 月份置1
}
else // 否则,月份加++
{
temp._month++;
}
}
return temp;
}
// 求当前日期第days天前是哪一天?
Date operator-(int days)
{
Date temp(*this);
if (days < 0)
{
return temp + (-days);
}
temp._day -= days;
while (temp._day <= 0)
{
if (temp._month == 1)
{
temp._year--;
temp._month = 12;
}
else
{
temp._month--;
}
temp._day += day_of_month(_year, _month); // 没有 += 之前这个时候 temp._day 是一个负值
}
return temp;
}
// 求两个日期之间的差值
int operator-(const Date& d)
{
Date maxDate(*this);
Date minDate(d);
int dayNum = 0;
int flag = 1;
if (*this < d)
{
maxDate = d;
minDate = *this;
flag = -1;
}
while (maxDate != minDate)
{
++minDate;
++dayNum;
}
return dayNum*flag;
}
/
// 赋值运算符重载
Date& operator=(const Date& d)
{
if (this == &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date& operator+= (int day)//重载+=(复用+)
{
*this = *this + day;
return *this;
}
Date& operator-=(int days)
{
return (*this = *this - days);
}
// 前置++
Date& operator++()
{
_day += 1;
return *this;
}
// 后置++
Date operator++(int) //int,区分前置,后置;后置++必须加标志,这是规定
{
Date temp(*this); //创建新的变量,不改变this的值
_day += 1;
return temp;
}
// 前置--
Date& operator--()
{
_day -= 1;
return *this;
}
// 后置--
Date operator--(int) //int,区分前置,后置;后置--必须加标志,这是规定
{
Date temp(*this); //创建新的变量,不改变this的值
_day -= 1;
return temp;
}
// 判断两个日期是否相等
bool operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
// 判断两个日期是否不等
bool operator!=(const Date& d)
{
return !(*this == d); // 如果 *this == d 为 true,取非,为 false,即 *this 相等 d
}
bool operator>(const Date& d)
{
return !(*this <= d); // 如果 *this <= d 为 true,取非,为 false,即 *this 不大于 d
}
bool operator>=(const Date& d)
{
return !(*this < d); // 如果 *this < d 为 true,取非,为 false,即 *this 不大于等于 d
}
bool operator<(const Date& d)
{
return !(*this >= d); // 如果 *this >= d 为 true,取非,为 false,即 *this 不小于 d
}
bool operator<=(const Date& d)
{
return !(*this > d); // 如果 *this > d 为 true,取非,为 false,即 *this 不小于等于 d
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2018, 4, 22);
Date d2(2018, 4, 23);
d2 = ++d1;
d2 = d1.operator++(); // 前置++
d2 = d1++; //编译器替我们传了标记
d2 = d1.operator++(1); // 后置++ (1没有意义,仅仅是区分而已)
}
运算符重载练习完成日期类
最新推荐文章于 2024-04-18 12:07:57 发布