-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.cpp
126 lines (111 loc) · 2.16 KB
/
Date.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
#include "Date.h"
#include <iostream>
#include "ExceptionDate.h"
using namespace std;
Date::Date()
{
year=1900;
month=1;
day=1;
}
Date::Date(int y, int m, int d)
{
setDate(y, m, d);//调用set函数设置日期值
}
Date::Date(const Date &d)
{
setDate(d.year, d.month, d.day);//调用set函数设置日期值,注意参数
}
Date::~Date()
{
//dtor
}
void Date::setDate(int y, int m, int d)
{
year=y; //初始化数据成员
month=m;
day=d;
//checkDate();//检查日期是否合法,如果不合法则调整。保证设置的日期值合法
if(!checkDate())
throw ExceptionDate();
}
bool Date::checkDate()
{
bool flag=true; //日期是否合法,缺省合法
//年不做任何限制
//月应该在1~12之间
if ((month<1)||(month>12))
{
month=1; //将月改为合法日期
flag=false;
}
//日应该在特定月的日期范围之内,即在1和本月最大天数之间
if ((day<1)||(day>maxDay()))
{
day=1;
flag=false;
}
return flag;
}
int Date::maxDay() const
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(isLeapYear()) return 29;
else return 28;
default: //月错误,则返回0
return 0;
}
}
bool Date::isLeapYear() const
{
return ( ((year%4==0)&&(year%100)) || (year%400==0) );
}
void Date::nextDay()
{
day++;
if(!checkDate()) month++;
if(!checkDate()) year++;
}
/*void Date::print() const
{
cout<<year<<"-"<<month<<"-"<<day;
}*/
Date& Date::operator++()
{
nextDay();
return *this;
}
Date Date::operator++(int)
{
Date d=*this;
nextDay();
return d;
}
ostream& operator<<(ostream &output, const Date &d)
{
output<<d.year<<"-"<<d.month<<"-"<<d.day;
return output;
}
istream& operator>>(istream& input, Date &d)
{
int year, month, day;
cout<<", (格式: 年 月 日): ";
input>>year>>month>>day;
d.setDate(year,month,day);
return input;
}