-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChrono.cpp
More file actions
123 lines (103 loc) · 2.04 KB
/
Copy pathChrono.cpp
File metadata and controls
123 lines (103 loc) · 2.04 KB
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
116
117
118
119
120
121
122
123
//Chrono.cpp
#include "Chrono.h"
namespace Chrono {
//member function definitions;
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}; //start of 21st century
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_month(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 functions
bool is_date(int y, Month m, int d)
{
//assume that y is valid
if(d<=0) return false;
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; //any traspás 29, sinó 28
break;
case Month::apr: case Month::jun: case Month::sep: case Month::nov:
º days_in_month = 30; //the rest have 30 days
break;
}
if(days_in_month < d) return false;
return true;
}
bool leapyear(int y)
{
//see exercise 10
}
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!=')'){ //format error
is.clear(ios_base::failbit);
return is;
}
dd = Date(y, Month(m), d);
return is;
}
enum class Day{
sunday, monday, tuesday, wednesday, thursday, friday, saturday
};
Day day_of_week(const Date& d)
{
//...
}
Date next_Sunday(const Date& d)
{
//...
}
Date next_weekday(const Date& d)
{
//...
}
}//Chrono