-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.cpp
More file actions
182 lines (180 loc) · 3.98 KB
/
date.cpp
File metadata and controls
182 lines (180 loc) · 3.98 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
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
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cctype>
#include <ctime>
using namespace std;
#define EPS 1e-9
#define N 100000 + 5
#define M 10000 + 5
#define MM(X) memset(X , 0 , sizeof(X))
#define MZ(X) memset(X , 0xff , sizeof(X))
#define MI(X) memset(X , 0x3f , sizeof(X))
class Date{
private:
int year,month,day;
const int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
public:
Date(int _year=1980,int _month=1,int _day=1):year(_year),month(_month),day(_day)
{
if(_year<0) year=1980;
if(_month<0) month=1;
if(_day<0) day=1;
}
~Date(){;}
const bool inPeriod(Date & d1,Date & d2) const
{
if((*this >= d1 && *this <=d2)||(*this >= d2 && *this <=d1))
{
return true;
}
return false;
}
const bool isLeap() const
{
if(year%400==0||(year%4==0&&year%100!=0))
{
return true;
}
return false;
}
friend ostream & operator << (ostream & out,const Date & d1)
{
cout<<d1.year<<"/"<<d1.month<<"/"<<d1.day;
//printf("%4d/%2d/%2d",els.year,els.month,els.day);
}
friend istream & operator >> (istream & in , Date & els)
{
return in >> els.year >> els.month >> els.day;
}
Date operator + (int _day)
{
Date td(year,month,day);
while(_day)
{
//cout<<td.year<<'/'<<td.month<<'/'<<td.day<<"dd:"<<_day<<endl;
int px=((td.isLeap()&&td.month==2)?(days[td.month]+1):days[td.month]);
if((td.day+_day)>px)
{
td.month++;
_day=_day - (px - td.day + 1);
td.day=1;
}
else{
td.day+=_day;
_day=0;
}
if(td.month>12)
{
td.year++;
td.month=td.month-12;
}
}
return td;
}
int operator - (Date & d1)
{
Date t1 = (*this>=d1)?*this:d1,
t2 = (*this<=d1)?*this:d1;
int i=0;
while(!(t1==(t2+i)))
{
i++;
}
return i;
}
void operator =(Date & d1)
{
year=d1.year;
month = d1.month;
day = d1.day;
}
bool operator < (const Date & d1) const
{
if(year != d1.year)
{
return year < d1.year;
}
if(month != d1.month)
{
return month < d1.month;
}
return day< d1.day;
}
bool operator >= (const Date & d1) const{
return !(*this < d1);
}
bool operator > (const Date & d1) const
{
return d1 < *this;
}
bool operator <= (const Date & d1) const
{
return !(*this > d1);
}
bool operator == (const Date & d1) const
{
return (!(*this > d1) )&&( !(*this < d1));
}
bool operator != (const Date & d1) const
{
return !(*this == d1);
}
const int getyear() const
{
return year;
}
const int getmonth() const
{
return month;
}
const int getday() const
{
return day;
}
bool setyear(int _year)
{
if(_year>0)
{
year=_year;
return true;
}
return false;
}
bool setmonth(int _month)
{
if(_month>=1&&_month<=12)
{
month=_month;
return true;
}
return false;
}
bool setday(int _day)
{
if(_day<=((this->isLeap()&&month==2)?days[month]+1:days[month])&&_day>=1)
{
day=_day;
return true;
}
return false;
}
};
int main(){
Date d1(2000,4,22),d2(2018,11,26),d3,d4;
cin>>d3>>d4;
cout<<d1<<endl<<d2<<endl<<d3<<endl<<d4<<endl;
cout<<(d3 + 100)<<endl;
return 0;
}