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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include "std_lib_facilities.h"
#include "Chrono.h"
using namespace std;
namespace Chrono
{
Date::Date(int yy, Month mm, int dd)
:y{yy}, m{mm}, d{dd}
{
if(!is_date(yy,mm,dd))throw Invalid{};
}
const Date& default_date()
{
static Date dd {2001, Month::jan,1};
return dd;
}
Date::Date()
:y{default_date().year()},
m{default_date().month()},
d{default_date().day()}
{}
void Date::add_day(int n)
{
//...
}
void Date::add_year(int n)
{
if(m==Month::feb && d==29 && !leapyear(y+n))
{
m=Month::mar;
d=1;
}
y+=n;
}
//helper function
bool is_date(int y, Month m, int d)
{
//assume that y is valid
if(m<Month::jan || Month::dec<m)return false;
int days_in_month=31;
switch(m)
{
case Month::feb:
days_in_month=(leapyear(y))?29:28 ;//arithmetic if
break;
case Month::apr: case Month::jun: case Month::sep: case Month::nov:
days_in_month=30;
break;
}
if (days_in_month<d) return false;
return true;
}
bool leapyear(int y)
{
//se ex
}
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&& a.month()==b.month()
&& a.day()==b.day();
}
bool operator !=(const Date&a, const Date& b)
{
return !(a==b);
}
ostream& operator<<(ostream& os, const Date& d)
{
return os<<'('<<d.year()
<<','<<d.month()
<<','<<d.day()<<')';
}
istream& operator >>(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is>>ch1>>y>>ch2>>m>>ch3>>d>>ch4;
if(!is) return is;
if(ch1!='('||ch2!=','||ch3!=','||ch4!=')')
{
is.clear(ios_base::failbit);
return is;
}
dd=Date(y,Month(m),d);
return is;
}
enum class Day{sunday, monday,tusdey,wensday,thursday,friday,saturday};
Day day_of_week(const Date& d)
{
//..
}
Date next_sunday(const Date& d)
{
//..
}
Date next_weekday(const Date& d)
{
//..
}
}
int main()
{
cout << "Hello world!" << endl;
return 0;
}
|