forked from jieniyimiao/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex14_49.cpp
220 lines (172 loc) · 4.68 KB
/
ex14_49.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "ex14_49.h"
#include <algorithm>
//! constructor taking Size as days
//! the argument must be within (0, 2^32)
Date::Date(Size days)
{
//! calculate the year
Size y400 = days/YtoD_400;
Size y100 = (days - y400*YtoD_400)/YtoD_100;
Size y4 = (days - y400*YtoD_400 - y100*YtoD_100)/YtoD_4;
Size y = (days - y400*YtoD_400 - y100*YtoD_100 - y4*YtoD_4)/365;
Size d = days - y400*YtoD_400 - y100*YtoD_100 - y4*YtoD_4 - y*365;
this->year = y400*400 + y100*100 + y4*4 + y;
//! check if leap and choose the months vector accordingly
std::vector<Size>currYear
= isLeapYear(this->year) ? monthsVec_l : monthsVec_n;
//! calculate day and month using find_if + lambda
Size D_accumu = 0, M_accumu = 0;
//! @bug fixed: the variabbles above hade been declared inside the find_if as static
//! which caused the bug. It works fine now after being move outside.
std::find_if(currYear.cbegin(), currYear.cend(), [&](Size m){
D_accumu += m;
M_accumu ++;
if(d < D_accumu)
{
this->month = M_accumu;
this->day = d + m - D_accumu ;
return true;
}
else
return false;
});
}
//! construcotr taking iostream
Date::Date(std::istream &is, std::ostream &os)
{
is >> day >> month >> year;
if(is)
{
if(check(*this)) return;
else
{
os << "Invalid input! Object is default initialized.";
*this = Date();
}
}
else
{
os << "Invalid input! Object is default initialized.";
*this = Date();
}
}
//! copy constructor
Date::Date(const Date &d) :
day(d.day), month(d.month), year(d.year)
{}
//! move constructor
Date::Date(Date&& d) noexcept :
day(d.day), month(d.month), year(d.year)
{ std::cout << "copy moving";}
//! copy operator=
Date &Date::operator= (const Date &d)
{
this->day = d.day;
this->month = d.month;
this->year = d.year;
return *this;
}
//! move operator=
Date &Date::operator =(Date&& rhs) noexcept
{
if(this != &rhs)
{
this->day = rhs.day;
this->month = rhs.month;
this->year = rhs.year;
}
std::cout << "moving =";
return *this;
}
//! conver to days
Date::Size Date::toDays() const
{
Size result = this->day;
//! check if leap and choose the months vector accordingly
std::vector<Size>currYear
= isLeapYear(this->year) ? monthsVec_l : monthsVec_n;
//! calculate result + days by months
for(auto it = currYear.cbegin(); it != currYear.cbegin() + this->month -1; ++it)
result += *it;
//! calculate result + days by years
result += (this->year/400) * YtoD_400;
result += (this->year%400/100) * YtoD_100;
result += (this->year%100/4) * YtoD_4;
result += (this->year%4) * YtoD_1;
return result;
}
//! member operators: += -=
Date &Date::operator +=(Date::Size offset)
{
*this = Date(this->toDays() + offset);
return *this;
}
Date &Date::operator -=(Date::Size offset)
{
if(this->toDays() > offset)
*this = Date(this->toDays() - offset);
else
*this = Date();
return *this;
}
//! non-member operators: << >> - == != < <= > >=
std::ostream&
operator <<(std::ostream& os, const Date& d)
{
os << d.day << " " << d.month << " " << d.year;
return os;
}
std::istream&
operator >>(std::istream& is, Date& d)
{
if(is)
{
Date input = Date(is, std::cout);
if(check(input)) d = input;
}
return is;
}
int operator -(const Date &lhs, const Date &rhs)
{
return lhs.toDays() - rhs.toDays();
}
bool operator ==(const Date &lhs, const Date &rhs)
{
return (lhs.day == rhs.day ) &&
(lhs.month == rhs.month) &&
(lhs.year == rhs.year ) ;
}
bool operator !=(const Date &lhs, const Date &rhs)
{
return !(lhs == rhs);
}
bool operator < (const Date &lhs, const Date &rhs)
{
return lhs.toDays() < rhs.toDays();
}
bool operator <=(const Date &lhs, const Date &rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
bool operator >(const Date &lhs, const Date &rhs)
{
return !(lhs <= rhs);
}
bool operator >=(const Date &lhs, const Date &rhs)
{
return !(lhs < rhs);
}
Date operator - (const Date &lhs, Date::Size rhs)
{ //! ^^^ rhs must not be larger than 2^32-1
//! copy lhs
Date result(lhs);
result -= rhs;
return result;
}
Date operator + (const Date &lhs, Date::Size rhs)
{ //! ^^^ rhs must not be larger than 2^32-1
//! copy lhs
Date result(lhs);
result += rhs;
return result;
}