Date.h
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
void Print() const;
static int GetMonthDay(int year, int month) ;
Date(int year = 1900, int month = 1, int day = 1);
~Date();
Date(const Date& d);
Date& operator=(const Date& d);
Date& operator+=(int day);
Date operator+(int day) const;
Date operator-(int day)const;
Date& operator-=(int day);
Date& operator++();
Date operator++(int);
Date operator--(int);
Date& operator--();
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;
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "date.h"
void Date::Print() const
{
cout << _year << '-' << _month << '-' << _day << endl;
}
int Date::GetMonthDay(int year, int month)
{
int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
a[2] = 29;
}
return a[month];
}
Date::Date(int year, int month, int day)
{
if (month >= 1 && month <= 12
&& day >= 1 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期错误"<<endl;
assert(false);
}
}
Date::~Date()
{
_year = 1;
_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)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= (-day);
}
else
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_month += 1;
if (_month > 12)
{
_year += 1;
_month = 1;
}
_day -= GetMonthDay(_year, _month);
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date tmp(*this);
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
*this += -day;
}
else
{
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
}
return *this;
}
Date Date::operator-(int day) const
{
Date tmp(*this);
tmp -= day;
return tmp;
}
Date& Date::operator++()
{
return *this += 1;
}
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& Date::operator--()
{
return *this -= 1;
}
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
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 _year == d._year && _month == d._month && _day == d._day;
}
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);
}
bool Date::operator != (const Date& d) const
{
return !(*this == d);
}
int Date::operator-(const Date& d)
{
int flag = -1;
Date max = d;
Date min = *this;
if (*this > d)
{
max = *this;
min = d;
flag = 1;
}
int sum = 0;
while (max > min)
{
min++;
sum++;
}
return sum * flag;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
int year=0, month=0, day=0;
in >> year >> month >>day;
if (month >= 1 && month <= 12
&& day >= 1 && day <= Date::GetMonthDay(year, month))
{
d._year = year;
d._month = month;
d._day = day;
}
else
{
cout << "日期非法";
assert(false);
}
return in;
}
test.cpp
#include "date.h"
int main()
{
Date d1(2102, 5, 12);
Date d2(2052, 12, 1);
Date d3(2012, 8, 1);
d1.Print();
d1.operator+=(100);
d1.Print();
d1.operator+=(-50);
d1.Print();
cout << d1;
cout << d1 << d2 << d3;
cin >> d1 >> d2;
cout << d1 << d2 << endl;
}
}
