-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.cpp
More file actions
156 lines (134 loc) · 2.99 KB
/
Date.cpp
File metadata and controls
156 lines (134 loc) · 2.99 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
#include <iostream>
#include <stdlib.h>
using namespace std;
class Date
{
private:
// int month_[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
// int day_ [13] = {31,28,31,30,31,30,31,31,30,31,30,31};
int year;
int month;
int day;
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(){;};
friend ostream & operator << (ostream & out,const Date & d1)
{
cout<<d1.year<<"/"<<d1.month<<"/"<<d1.day;
}
friend istream & operator >> (istream & in , Date & els)
{
return in >> els.year >> els.month >> els.day;
}
int Getdays(int month,int year){
int month_[13] = {31,28,31,30,31,30,31,31,30,31,30,31};
int day = month_[month];
if(month == 2 && Isleap(year)){
day += 1;
}
return day;
}
bool Islegal(int month = 1,int day = 1,int yaer = 1980){
printf("1\n");
this -> year = year;
this -> month = month;
this -> day = day;
if(month <= 12 && month > 0){
if(day <= Getdays(month,year)&& day > 0){
return true;
}
else
cout << "非法日期" << endl;
}
else
cout << "非法日期" << endl;
}
bool Isleap(int year){
if(year % 400 == 0 ||( year % 4 == 0 && year % 400 != 0))
return true;
else
return false;
}
Date operator = (const Date & C){
this -> year = C.year;
this -> month = C.month;
this -> day = C.day;
return *this;
}
bool operator == (Date & C){
return ( !(*this > C) && !(*this < C));
}
Date operator + (int n){
Date temp(*this);
temp.day += n;
while(temp.day > Getdays(temp.month,temp.year)){
temp.day -= Getdays(temp.month,temp.year);
temp.month += 1;
if(temp.month > 12){
temp.year += 1;
temp.month = 1;
}
}
return temp;
}
Date operator - (Date & C){
Date t1 = (*this >= C ) ? *this : C,
t2 = (*this <= C ) ? *this : C;
int day_s = 0;
while(!(t1 == (t2 + day_s))){
day_s++;
}
return day_s;
}
bool operator != (Date & C){
return !(*this == C);
}
bool operator <(Date & C){
if(year != C.year){
return year < C.year;
}
if(month != C.month){
return month < C.month;
}
else
return day < C.day;
}
bool operator >= (Date & C){
return !(*this < C);
}
bool operator >(const Date & C) const{
if(year != C.year){
return year > C.year;
}
if(month != C.month){
return month > C.month;
}
else
return day > C.day;
}
bool operator <= (Date & C){
return !(*this > C);
}
const int getyear() const{
return year;
}
const int getmonth()const{
return month;
}
const int getday() const{
return day;
}
};
int main(int argc, char const *argv[])
{
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;
}