Date类
程序代码如下:
Date.h
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 2000, int month = 1, int day = 1);
Date(const Date& d);
//赋值运算符重载
Date& operator=(const Date& d);
//前置++
Date& operator++();
//后置++
Date operator++(int);
//前置--
Date& operator--();
//后置--
Date operator--(int);
//计算日期是那一年的第几天
int DaysInYear(int year, int month, int day);
//计算当前日期向后n天的日期
Date Date::operator+(int days);
//计算当前日期向前n天的日期
Date Date::operator-(int days);
//计算两个日期相差天数
int operator-(Date& d);
//判断两个日期是否相等
bool operator==(const Date& d)const;
bool operator!=(const Date& d)const;
bool operator>(const Date& d)const;
bool operator>=(const Date& d)const;
bool operator<(const Date& d)const;
bool operator<=(const Date& d)const;
private:
//交换函数
void Swap(int& left, int& right);
//判断是否为闰年
bool IsLeap(int year);
//获取当月天数
int GetDaysOfMonth(int year, int month);
//判断日期是否合法
bool IsDateValid();
private:
int _year;
int _month;
int _day;
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
};
Date.c
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
//日期类
Date::Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{
if (!IsDateValid())
{
cout << "日期非法!使用默认值2000.1.1" << endl;
_year = 2000;
_month = 1;
_day = 1;
}
}
Date::Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//交换函数
void Date::Swap(int& left, int& right)
{
int tmp = left;
left = right;
right = tmp;
}
//判断是否为闰年
bool Date::IsLeap(int year)
{
if (((0 == year % 4) && (0 != year % 100)) ||
(0 == year % 400))
return true;
else
return false;
}
//获取当月天数
int Date::GetDaysOfMonth(int year,int month)
{
//方法一
int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && IsLeap(year))
return days[month] + 1;
else
return days[month];
//方法二
/*switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (IsLeap(year))
return 29;
else
return 28;
}*/
}
//判断日期是否合法
bool Date::IsDateValid()
{
if (_year <1 || (_month < 1 || _month > 12) ||
(_day<1 || _day>GetDaysOfMonth(_year, _month)))
return false;
else
return true;
}
//前置++
Date& Date::operator++()
{
if (_day < GetDaysOfMonth(_year,_month))
++_day;
else
{
if (_month < 12)
{
++_month;
_day = 1;
}
else
{
++_year;
_month = 1;
_day = 1;
}
}
return *this;
}
//后置++
Date Date::operator++(int)
{
Date tmp(*this);
if (_day < GetDaysOfMonth(_year, _month))
++_day;
else
{
if (_month < 12)
{
++_month;
_day = 1;
}
else
{
++_year;
_month = 1;
_day = 1;
}
}
return tmp;
}
//前置--
Date& Date::operator--()
{
if (_day > 1)
--_day;
else
{
if (_month > 1)
{
--_month;
_day = GetDaysOfMonth(_year, _month);
}
else
{
--_year;
_month = 12;
_day = 31;
}
}
return *this;
}
//后置--
Date Date::operator--(int)
{
Date tmp = *this;
if (_day > 1)
--_day;
else
{
if (_month > 1)
{
--_month;
_day = GetDaysOfMonth(_year, _month);
}
else
{
--_year;
_month = 12;
_day = 31;
}
}
return tmp;
}
//计算日期是那一年的第几天
int Date::DaysInYear(int year, int month, int day)
{
int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeap(year))
days[2] = 29;
for (int i = 1; i < month; ++i)
{
day += days[i];
}
return day;
}
//计算当前日期向后n天的日期
Date Date::operator+(int days)
{
if (days<0)
return *this - (0 - days);
Date tmp(*this);
tmp._day += days;
int DaysOfMonth = 0;
while (tmp._day > (DaysOfMonth = GetDaysOfMonth(tmp._year, tmp._month)))
{
tmp._day -= DaysOfMonth;
++tmp._month;
if (tmp._month > 12)
{
++tmp._year;
tmp._month = 1;
}
}
return tmp;
}
//计算当前日期向前n天的日期
Date Date::operator-(int days)
{
if (days<0)
return *this + (0 - days);
Date tmp(*this);
tmp._day -= days;
while (tmp._day <= 0)
{
--tmp._month;
if (tmp._month < 1)
{
--tmp._year;
tmp._month = 12;
}
tmp._day += GetDaysOfMonth(tmp._year, tmp._month);
}
return tmp;
}
//计算两个日期相差天数
int Date::operator-(Date& d)
{
//同年同月
if (_year == d._year && _month == d._month)
return _day > d._day ? (_day - d._day) : (d._day - _day);
//同年
else if (_year == d._year)
{
int d1, d2;
d1 = DaysInYear(_year, _month, _day);
d2 = DaysInYear(d._year, d._month, d._day);
return d1 > d2 ? (d1 - d2) : (d2 - d1);
}
//年、月都不相同
else
{
int flag = 0;
//确保_year日期比d._year大
if (_year < d._year)
{
Swap(_year, d._year);
Swap(_month, d._month);
Swap(_day, d._day);
flag = 1;
}
int d1, d2, d3 = 0;
//1.求小的日期在该年剩余天数
if (IsLeap(d._year))
d1 = 366 - DaysInYear(d._year, d._month, d._day);
else
d1 = 365 - DaysInYear(d._year, d._month, d._day);
//2.求大的日期是该年的第几天
d2 = DaysInYear(_year, _month, _day);
//3.求两个日期中间相差几个整年
for (int i = d._year+1; i < _year; ++i)
{
if (IsLeap(i))
d3 += 366;
else
d3 += 365;
}
if (1 == flag)
{
Swap(_year, d._year);
Swap(_month, d._month);
Swap(_day, d._day);
}
return d1 + d2 + d3;
}
}
//判断两个日期是否相等
bool Date::operator==(const Date& d)const
{
return _year == d._year &&
_month == d._month &&
_day == d._day;
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}
bool Date::operator>(const Date& d)const
{
if (_year > d._year ||
_year == d._year && _month > d._month ||
_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}
bool Date::operator>=(const Date& d)const
{
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d)const
{
return (*this < d) || (*this == d);
}
//输入运算符重载
istream& operator>>(istream& _cin, Date& d)
{
_cin >> d._year >> d._month >> d._day;
while (!d.IsDateValid())
{
cout << "日期非法!请重新输入:";
_cin >> d._year >> d._month >> d._day;
}
return _cin;
}
//输出运算符重载
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
//测试构造、拷贝构造、前置++、后置++、关系运算符、赋值运算符、输出运算符重载
void TestFunc1()
{
Date d1(2018, 2, 27);
++d1;
d1++;
Date d2(d1);
Date d3 = d1;
cout << "d1:" << d1 << endl;
cout << "d2:" << d2 << endl;
cout << "d3:" << d3 << endl;
if (d2 == d1)
cout << "d2 = d1,厉害了我的哥!" << endl;
if (d2 >= d1)
cout << "d2 >= d1,厉害了我的哥!" << endl;
if (d2 <= d1)
cout << "d2 <= d1,厉害了我的哥!" << endl;
Date d4(2016, 1, 2);
--d4;
d4--;
cout << "d4:" << d4 << endl;
if (d4 != d1)
cout << "d4 != d1,厉害了我的哥!" << endl;
if (d1 > d4)
cout << "d1 > d4,厉害了我的哥!" << endl;
if (d4 < d1)
cout << "d4 < d1,厉害了我的哥!" << endl;
}
//测试加、减号运算符重载---计算当前日期向前、向后n天的日期
void TestFunc2()
{
Date d1(2018, 11, 20);
Date d0 = d1 - 100;
Date d2 = d1 + 100;
cout <<d1<<"的100天前的日期:"<< d0 << endl;
cout << d1 << "的100天后的日期:" << d2 << endl;
}
//测试减号运算符重载---计算两个日期相差天数
void TestFunc3()
{
Date d1(1999, 1, 23);
Date d2(2000, 1, 1);
cout << d1 << "与" << d2 << "间隔" << d1 - d2 << "天" << endl;
cout << d2 << "与" << d1 << "间隔" << d2 - d1 << "天" << endl;
}
int main()
{
TestFunc1();
TestFunc2();
TestFunc3();
return 0;
}
程序运行结果如下:
TestFunc1();
TestFunc2();
TestFunc3();