一.实现日期类
今天算是做一个小练习,尝试以下对日期类的实现
#include<iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& cout, const Date& d);
public:
Date(int year = 1990, int month = 1, int day = 1)
{
m_year = year;
m_month = month;
m_day = day;
}
Date(const Date& d)
{
m_year = d.m_year;
m_month = d.m_month;
m_day = d.m_day;
}
~Date()
{}
//在日期相加之前我们必须要获取每个月的天数才能更准确的得到结果
public:
bool IsLeap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
return false;
}
//写法一
/*int GetDayByYearMonth(int year, int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12)
return 31;
else if (month == 2)
{
if (IsLeap(year))
return 29;
return 28;
}
return 30;
}*/
//写法二
//这里定义数组直接返回下标即可
int GetDayByYearMonth(int year, int month)
{
int days[13] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2)
{
if (IsLeap(year))
month = 0;
}
return days[month];
}
//重载日期++
public:
Date operator+(int n) //日期+n天
{
int year = m_day;
int month = m_month;
int day = m_day;
int days = GetDayByYearMonth(year, month);
while (day + n > days)
{
month++;
if (month > 12)
{
year++;
month = 1;
}
n -= days;
days = GetDayByYearMonth(year, month);
}
day += n;
return Date(year, month, day);
}
//重载日期-
Date operator-(int n) //日期-n天
{
int year = m_day;
int month = m_month;
int day = m_day;
int days = GetDayByYearMonth(year, month);
while (day - n > days)
{
month--;
if (month < 12)
{
year--;
month = 1;
}
n += days;
days = GetDayByYearMonth(year, month);
}
day -= n;
return Date(year, month, day);
}
//重载+=
//写法一
/*Date& operator+=(int n)
{
int days = GetDayByYearMonth(m_year, m_month);
while (m_day + n > days)
{
m_month++;
if (m_month > 12)
{
m_year++;
m_month = 1;
}
n -= days;
days = GetDayByYearMonth(m_year, m_month);
}
m_day += n;
return *this;
}*/
//写法二
//这里我们通过调用+的方法进行计算,但是这样的算法效率会比较低,因为调动了多种成员函数方法
//但是由于这个日期类比较小这样子写也是可以的
Date& operator+=(int n)
{
Date tmp = *this + n;
*this = tmp;
return *this;
}
//重载-=
Date& operator-=(int n)
{
Date tmp = *this - n;
*this = tmp;
return *this;
}
//重载前置++
Date operator++()
{
*this = *this + 1;
return *this;
}
//重载后置++
Date operator++(int)
{
Date tmp = *this;
++ *this;
return tmp;
}
//重载前置--
Date operator--()
{
*this = *this - 1;
return *this;
}
//重载后置--
Date operator--(int)
{
Date tmp = *this;
--*this;
return tmp;
}
public:
//重载日期的大于的关系
bool operator>(const Date& d)
{
if ((m_year > d.m_year)
|| (m_year == d.m_year && m_month > d.m_month)
|| (m_year == d.m_year && m_month == d.m_month && m_day > d.m_day))
return true;
return false;
}
//重载日期<=
bool operator<=(const Date& d)
{
return !(*this > d);
}
//重载日期小于的关系
bool operator<(const Date& d)
{
if ((m_year < d.m_year)
|| (m_year == d.m_year && m_month < d.m_month)
|| (m_year == d.m_year && m_month == d.m_month && m_day < d.m_day))
return true;
return false;
}
//重载日期>=
bool operator>=(const Date& d)
{
return !(*this < d);
}
//重载日期==
bool operator==(const Date& d)
{
return (m_year == d.m_year && m_month == d.m_month && m_day == d.m_day);
}
//重载日期!=
bool operator!=(const Date& d)
{
return !(*this == d);
}
//重载两个日期之间的间隔天数
Date operator-(const Date& d)
{
Date maxdate(*this);
Date mindate(d);
if (maxdate < mindate)
{
maxdate = mindate;
mindate = *this;
}
int count = 0;
while (mindate < maxdate)
{
mindate = mindate + 1;
++count;
}
return count;
}
private:
int m_year;
int m_month;
int m_day;
};
ostream& operator<<(ostream& out, const Date& d)
{
out << d.m_year << "年" << d.m_month << "月" << d.m_day << "日";
return cout;
}
void main()
{
Date d1(1900, 2, 28);
cout << ++d1 << endl;
cout << --d1 << endl;
cout << d1 << endl;
cout << d1-- << endl;
cout << d1++ << endl;
cout << d1 << endl;
Date d2(2017, 5, 1);
cout << d2 + 100 << endl;
Date d3(1997, 11, 1);
cout << d3 - 100 << endl;
}