📄 date.h
字号:
#ifndef DATE_H
#define DATE_H
#include <iostream>
using namespace std ;
class Date
{
public:
Date( int m = 0, int d = 0, int y = 0 )
{
setDate( m, d, y ) ;
}
Date( const Date &d )
{
*this = d ;
}
~Date( void )
{
// nothing here ;
}
Date& operator = ( const Date &d )
{
ndays = d.ndays ;
return *this ;
}
// ----------------------------------------------- overloaded operators
Date operator + ( int n ) const
{
Date dt( *this ) ;
dt += n ;
return dt ;
}
Date operator - ( int n ) const
{
Date dt( *this ) ;
dt -= n ;
return dt ;
}
Date& operator += ( int n )
{
ndays += n ;
return *this ;
}
Date& operator -= ( int n )
{
ndays -= n ;
return * this ;
}
Date& operator ++ ( void )
{
++ndays ;
return *this ;
}
Date operator ++ ( int )
{
Date dt( *this ) ;
ndays++ ;
return dt ;
}
Date& operator -- ( void )
{
--ndays ;
return *this ;
}
Date operator -- ( int )
{
Date dt( *this ) ;
ndays-- ;
return dt ;
}
long operator - ( const Date &dt ) const
{
return ndays - dt.ndays ;
}
bool operator == ( const Date &dt ) const
{
return ndays == dt.ndays ;
}
bool operator != ( const Date &dt ) const
{
return ndays != dt.ndays ;
}
bool operator < ( const Date &dt ) const
{
return ndays < dt.ndays ;
}
bool operator > ( const Date &dt ) const
{
return ndays > dt.ndays ;
}
bool operator <= ( const Date &dt ) const
{
return ndays <= dt.ndays ;
}
bool operator >= ( const Date &dt ) const
{
return ndays >= dt.ndays ;
}
// ------------------------------------------------------ member functions
void setDate( int, int, int ) ;
void setMonth( int m )
{
*this = Date( m, getDay(), getYear() ) ;
}
void setDay( int d )
{
*this = Date( getMonth(), d, getYear() ) ;
}
void setYear( int y )
{
*this = Date( getMonth(), getDay(), y ) ;
}
void getDate( int &, int &, int & ) const ;
int getMonth( void ) const ;
int getDay( void ) const ;
int getYear( void ) const ;
virtual void display( void ) const ;
bool isLeapYear( int y ) const
{
return ( ( y % 4 ) == 0 && ( y & 1000 ) != 0 ) ;
}
bool isLeapYear( void ) const
{
return isLeapYear( getYear() ) ;
}
protected:
static const int dys[] ;
long int ndays ;
} ;
const int Date::dys[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;
void Date::setDate( int m, int d, int y )
{
if ( m < 1 || m > 12 || y < 1 ) {
ndays = 0 ;
return ;
}
ndays = ( y - 1 ) * 365 + ( y - 1 ) / 4 - ( y - 1 ) / 1000 ;
for ( int i = 0; i < m; ++i ) {
int dy = dys[i] ;
if ( i == 1 && isLeapYear( y ) )
dy++ ;
if ( i < m - 1 )
ndays += dy ;
else if ( d > dy ) {
ndays = 0 ;
return ;
}
}
ndays += d ;
}
void Date::getDate( int &mo, int &da, int &yr ) const
{
da = ndays ;
if ( ndays == 0 ) {
yr = mo = 0 ;
return ;
}
for ( yr = 1; ; ++yr ) {
int daysThisYear = isLeapYear( yr ) ? 366 : 365 ;
if ( da <= daysThisYear )
break ;
da -= daysThisYear ;
}
for ( mo = 1; mo < 13; ++mo ) {
int dy = dys[mo - 1] ;
if ( mo == 2 && isLeapYear( yr ) )
dy++ ;
if ( da <= dy )
break ;
da -= dy ;
}
}
int Date::getMonth( void ) const
{
int mo, da, yr ;
getDate( mo, da, yr ) ;
return mo ;
}
int Date::getDay( void ) const
{
int mo, da, yr ;
getDate( mo, da, yr ) ;
return da ;
}
int Date::getYear( void ) const
{
int mo, da, yr ;
getDate( mo, da, yr ) ;
return yr ;
}
void Date::display( void ) const
{
int mo, da, yr ;
getDate( mo, da, yr ) ;
cout << mo << '/' << da << '/' << yr << endl ;
}
inline Date operator + ( int n, const Date &dt )
{
return dt + n ;
}
inline ostream& operator << ( ostream &os, const Date &dt )
{
os << dt.getMonth() << '/' ;
os << dt.getDay() << '/' ;
os << dt.getYear() ;
return os ;
}
inline istream& operator >> ( istream &is, Date &dt )
{
int mo, da, yr ;
is >> mo >> da >> yr ;
dt.setDate( mo, da, yr ) ;
return is ;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -