1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include "Date.h"
using namespace std;
const int Date::month_nb = 12;
const unsigned char Date::month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
const unsigned char Date::month_daysLeap[] = {31,29,31,30,31,30,31,31,30,31,30,31};
Date::Date(const int month_, const int day_, const int year_)
{
year = year_; month = month_; day = day_;
isLeap = testLeap();
}
bool Date::testLeap(const int year_)
{
//if the year is not a multiple of 4, this is not a leap year
if(year_ % 4 != 0) return false;
//if the year is a multiple of 100, this is not a leap year,
//except if it is a multiple of 400.
if(year_ % 100 == 0) {
return year_ % 400 == 0;
}
return true;
}
bool Date::testLeap() const {
return testLeap(year);
}
void Date::out() const
{
cout << (int)month <<"/"<< (int)day <<"/"<< year << endl;
}
bool Date::testValid() const {
return (1 <= month && month <= month_nb && 1 <= day
&& ((isLeap && day <= month_daysLeap[month-1])
|| (!isLeap && day <= month_days[month-1])));
}
ostream& operator<<(ostream& os,const Date& d) {
return os<<d.get_month()<<'/'<<d.get_day()<<'/'<<d.get_year();
}
|