📄 date.cpp
字号:
// Date.cpp: implementation of the Date class.
//
//////////////////////////////////////////////////////////////////////
#include "Date.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Initialize staitc member at file scope.
const unsigned Date::days[] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31, };
Date::Date():year(1),month(1),day(1)
{}
Date::Date(unsigned y,unsigned m,unsigned d)
{ setDate( y, m, d ); }
Date::Date( const Date& date )
{ setDate( date.year, date.month, date.day ); }
Date::Date( char* date )
{
char ch;
unsigned y,m,d;
istrstream tStream(date);
tStream>>y>>ch>>m>>ch>>d;
setDate( y, m, d );
}
void Date::setDate( unsigned y, unsigned m, unsigned d )
{
month = ( m>=1 && m<=12 ) ? m : 1;
year = ( y<100000 ) ? y : 2000;
if( 2 == month && leapYear( year ) )
{ day = ( d >= 1 && d <= 29 ) ? d : 1; }
else
{ day = ( d >= 1 && d <= days[ month ] ) ? d : 1; }
}
Date::~Date()
{}
bool Date::leapYear( int y ) const
/* If the year is a leap year, return true, else return false */
{
if( 0 == y % 400 || ( 0 != y % 100 && 0 == y % 4 ) )
{ return true; } // a leap year
else
{ return false; }
}
bool Date::endOfMonth( int d ) const
/* if the day is the end of month return true, else return false */
{
if( month == 2 && leapYear( year ) )
{ return 29 == d; } // last day of Feb in leap year
else
{ return days[ month ] == d; }
}
void Date::increment( )
/* increment one day. */
{
if( endOfMonth( day ) && 12 == month ){ // end year
day = 1;
month = 1;
++year;
}
else if( endOfMonth( day ) ){ // end month
day = 1;
++month;
}
else
{ ++day; }
}
Date& Date::operator ++ ()
/* increamet one day */
{
increment();
/* return non-incremented, saved, temporary object. */
return *this; // value return, not a reference return
}
const Date& Date::operator += ( unsigned d )
/* add a specific number of days to a date. */
{
for( unsigned i = 0; i < d; i++ )
{ increment(); }
return *this; // enables cascading
}
bool Date::operator==(const Date& date)
/* If two date all the same then return true, else return false.*/
{ return day==date.day && month==date.month && year==date.year; }
bool Date::operator>(const Date& date)
/* If the first date later than the follow, return true *
* else return false. */
{
return year>date.year
|| year==date.year && month>date.month
|| month==date.month && day>date.day;
}
bool Date::operator < (const Date& date)
/* If the first date earlier than the follow, return true *
* else return false. */
{
return year<date.year
|| year==date.year && month<date.month
|| month==date.month && day<date.day;
}
istream& operator>> ( istream& is, Date& date)
{
char ch;
unsigned y, m, d;
is >> y >> ch >> m >> ch >> d;
date.setDate( y, m, d );
return is;
}
ostream& operator << ( ostream& os, Date& date)
{
os << date.year << '-' << date.month << '-' << date.day;
return os;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -