1、Date.h
# pragma once
# include <iostream>
using namespace std;
class Date
{
public:
Date ( int year = 1 , int month = 1 , int day = 1 ) ;
void Print ( ) ;
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+= ( int day) ;
Date operator+ ( int day) ;
Date& operator-= ( int day) ;
Date operator- ( int day) ;
int operator- ( const Date& d) ;
Date& operator++ ( ) ;
Date operator++ ( int i) ;
Date& operator-- ( ) ;
Date operator-- ( int i) ;
private:
int _year;
int _month;
int _day;
} ;
2、Date.cpp
# define _CRT_SECURE_NO_WARNINGS 1
# include "Date.h"
Date:: Date ( int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if ( month < 1 || month> 12 || day< 1 || day> GetMonthDay ( year, month) )
{
cout << "非法日期:" ;
}
}
void Date:: Print ( )
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
int Date:: GetMonthDay ( int year, int month)
{
static int monthArray[ 13 ] = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;
if ( month == 2 && ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 ) )
{
return 29 ;
}
return monthArray[ month] ;
}
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 ( _year == d. _year && _month == d. _month && _day == d. _day) ;
}
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) ;
}
bool Date:: operator!= ( const Date& d)
{
return ! ( * this == d) ;
}
Date& Date:: operator+= ( int day)
{
if ( day < 0 )
{
return * this -= ( - day) ;
}
_day += day;
while ( _day > GetMonthDay ( _year, _month) )
{
_day -= GetMonthDay ( _year, _month) ;
++ _month;
if ( _month == 13 )
{
_year++ ;
_month = 1 ;
}
}
return * this;
}
Date Date:: operator+ ( int day)
{
Date tmp ( * this) ;
tmp += day;
return tmp;
}
Date& Date:: operator-= ( int day)
{
if ( day < 0 )
{
return * this += ( - day) ;
}
_day -= day;
while ( _day < 1 )
{
_month-- ;
if ( _month == 0 )
{
_year-- ;
_month = 12 ;
}
_day += GetMonthDay ( _year, _month) ;
}
return * this;
}
Date Date:: operator- ( int day)
{
Date tmp ( * this) ;
tmp -= day;
return tmp;
}
Date& Date:: operator++ ( )
{
* this += 1 ;
return * this;
}
Date Date:: operator++ ( int i)
{
Date tmp = * this;
* this += 1 ;
return tmp;
}
Date& Date:: operator-- ( )
{
* this -= 1 ;
return * this;
}
Date Date:: operator-- ( int i)
{
Date tmp = * this;
* this -= 1 ;
return tmp;
}
int Date:: operator- ( const Date& d)
{
Date max = * this;
Date min = d;
int flag = 1 ;
if ( max < min)
{
max = d;
min = * this;
flag = - 1 ;
}
int n = 0 ;
while ( min != max)
{
++ min;
++ n;
}
return n * flag;
}
3、Test.cpp
# define _CRT_SECURE_NO_WARNINGS 1
# include "Date.h"
void Test1 ( )
{
Date d1 ( 2023 , 7 , 23 ) ;
d1. Print ( ) ;
Date d2 ( 2023 , 8 , 23 ) ;
d2. Print ( ) ;
Date d3 ( 2023 , 13 , 0 ) ;
d3. Print ( ) ;
}
void Test2 ( )
{
Date d1 ( 2023 , 7 , 23 ) ;
d1. Print ( ) ;
Date d2 ( 2023 , 8 , 13 ) ;
d2. Print ( ) ;
Date d3 ( d2) ;
d3. Print ( ) ;
d1 = d2;
d1. Print ( ) ;
d1 = d2 = d3;
d1. Print ( ) ;
}
void Test3 ( )
{
Date d1 ( 2023 , 7 , 27 ) ;
d1 += 2000 ;
d1. Print ( ) ;
Date d2 ( 2023 , 7 , 27 ) ;
Date ret = d2;
ret. Print ( ) ;
}
void Test4 ( )
{
Date d1 ( 2023 , 7 , 27 ) ;
d1 -= 2000 ;
d1. Print ( ) ;
d1 -= - 200 ;
d1. Print ( ) ;
}
void Test5 ( )
{
Date d1 ( 2023 , 7 , 27 ) ;
Date ret1 = d1++ ;
ret1. Print ( ) ;
d1. Print ( ) ;
Date ret2 = ++ d1;
ret2. Print ( ) ;
d1. Print ( ) ;
}
void Test6 ( )
{
Date d1 ( 2002 , 12 , 14 ) ;
Date d2 ( 2023 , 7 , 27 ) ;
cout << d2 - d1 << endl;
}
int main ( )
{
cout << "Test1:" << endl;
Test1 ( ) ;
cout << endl;
cout << "Test2:" << endl;
Test2 ( ) ;
cout << endl;
cout << "Test3:" << endl;
Test3 ( ) ;
cout << endl;
cout << "Test4:" << endl;
Test4 ( ) ;
cout << endl;
cout << "Test5:" << endl;
Test5 ( ) ;
cout << endl;
cout << "Test6:" << endl;
Test6 ( ) ;
return 0 ;
}
4、运行结果