C++实现日期类(Date类)的方法

2025-05-27 0 90

如下所示:

?

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<iostream>

using namespace std;

class Date

{

public:

Date(int year = 1900, int month = 1, int day = 1) //构造

:_year(year)

, _month(month)

, _day(day)

{

if (!isInvalidDate(_year, _month, _day))

{

_year = 1900;

_month = 1;

_day = 1;

}

}

Date operator+(int count)

{

Date tmp(*this);

tmp._day += count;

ToCorrect(tmp);

return tmp;

}

Date operator-(int count)

{

Date tmp(*this);

tmp._day -= count;

ToCorrect(tmp);

return tmp;

}

Date& operator++() //前置++

{

(*this)++;

return *this;

}

Date operator++(int) //后置++

{

Date tmp(*this);

(*this)+=1;

return tmp;

}

Date& operator--()

{

(*this)--;

return *this;

}

Date operator--(int)

{

Date tmp(*this);

(*this)--;

return tmp;

}

int operator-(const Date &d)

{

int flag = 1;

Date max = *this;

Date min = d;

if (*this<d)

{

max = d;

min = *this;

flag = -1;

}

int count=0;

while (min != max)

{

++min;

count++;

}

return count*flag;

}

Date& operator+=(int day)

{

*this = *this + day;

return *this;

}

bool operator!=(const Date &d)

{

return !(*this == d);

}

bool operator<(const Date &d)

{

return !((*this>d)||(*this==d));

}

bool operator>=(const Date &d)

{

return !(*this<d);

}

bool operator>(const Date &d)

{

return (_year > d._year

|| (_year == d._year && _month > d._month)

|| (_year == d._year && _month == d._month && _day > d._day));

}

bool operator==(const Date &d)

{

return ((_year == d._year) && (_month == d._month) && (_day == d._day));

}

public:

bool IsLeapYear(int year)

{

if(year % 400 || (year % 4 && year % 100))

return true;

return false;

}

int YearsOfMonth(int year, int month)

{

int day;

int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

day = days[month];

if (month == 2)

day += IsLeapYear(year);

return day;

}

Date ToCorrect(Date &d)

{

if (d._day>YearsOfMonth(d._year, d._month))

{

while (d._day > YearsOfMonth(d._year, d._month))

{

d._day -= YearsOfMonth(d._year,d._month);

if (d._month == 12)

{

d._year++;

d._month = 1;

}

else

{

++d._month;

}

}

}

else

{

while (d._day <= 0)

{

if (d._month == 1)

{

d._year--;

d._month = 12;

}

else

{

--d._month;

}

d._day += YearsOfMonth(d._year, d._month);

}

}

return d;

}

bool isInvalidDate(int year, int month, int day)

{

if ((year < 1) || (month<0 || month>12) || (day<0 || day>YearsOfMonth(year,month)))

return false;

return true;

}

void Display()

{

cout << _year << "-" << _month << "-" << _day << endl;

}

friend istream& operator>>(istream& is, Date &d);

friend ostream& operator<<(ostream& os, const Date &d);

private:

int _year;

int _month;

int _day;

};

istream& operator>>(istream& is, Date& d)

{

cout << "请输入一个日期" << endl;

is >> d._year >> d._month >> d._day;

return is;

}

ostream& operator<<(ostream& os, const Date &d)

{

cout << d._year << "-" <<d. _month << "-" << d._day << endl;

return os;

}

int main()

{

/*Date d1(2016,8,18);

//d1.Display();

//d1 = d1++;

cout << d1 << endl;*/

//Date d1(2015, 12, 3);

//(d1++).Display(); //d1.operator++(&d1, 0);

//(++d1).Display(); //d1.operator++(&d1);

Date d1(2015, 12, 3);

Date d2(2015, 11, 1);

cout << (d1 - d2) << endl;

//Date d1(2015, 12, 3);

//Date ret = d1 + 40; //operator+

//ret.Display();

/*Date d1(2015, 12, 3);

Date ret = d1 + 40;

d1 = ret;

ret = d1 - 40;

ret.Display();*/

/*Date ret;

Date d2(2015, 1, 1);

ret = d2 - 1;

ret.Display();*/

return 0;

}

以上这篇C++实现日期类(Date类)的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C++实现日期类(Date类)的方法 https://www.kuaiidc.com/74771.html

相关文章

发表评论
暂无评论