C++ 实现 Data类(简单的日期计算器)

简介:

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include <assert.h>
using  namespace  std;
 
//int Month1[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//int Month2[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
////闰年判断函数
//    int Data::Leep_year(int year)
//    {
//        if ((year % 400) || ((year % 4) && !(year % 100)))
//        {
//            return 1;
//        }
//        else
//        {
//            return -1;
//        }
//    }
 
//返回x月份的天数
int  GetMonthDay( int  year,  int  month)
{
     assert (month > 0 && month < 13);
     static  int  monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     int  day = monthArray[month];
     if  ((month == 2) && (year % 400) || (year % 4 && year % 100))
     {
         day += 1;
     }
     return  day;
}
 
class  Date
{
public :
//声明display函数
     void  display();
 
//Data 构造函数
     Date( int  year = 1900,  int  month = 1,  int  day = 1)
         :_year(year)
         ,_month(month)
         ,_day(day)
     {}
 
 
//重载 - 运算符
Date operator-( int  n)
{
     Date tmp(* this );
     tmp._day = tmp._day - n;
     if  (n < 0)
     {
         return  * this  + (-n);
     }
     while  (tmp._day <= 0)
     {
         if  (tmp._month == 1)
         {
             tmp._month = 12;
             tmp._year = tmp._year - 1;
         }
         else
         {
             tmp._month = tmp._month - 1;
         }
         tmp._day = tmp._day + GetMonthDay(tmp._year, tmp._month);
     }
     return  tmp;
}
//重载+运算符
Date operator+( int  n)
{
     Date tmp(* this );
     if  (n >= 0)                     //n>=0的情况
     {
         tmp._day = tmp._day + n;
 
         while  (tmp._day > GetMonthDay(tmp._year,tmp._month))
         {
             tmp._day = tmp._day - GetMonthDay(tmp._year, tmp._month);
             if  (tmp._month == 12)             //tmp.month=12,则tmp.month+1为1
             {
                 tmp._month = 1;
                 tmp._year = tmp._year + 1;
             }
             else
             {
                 tmp._month = tmp._month + 1;
             }
         }
     }
     else                     //n<0的情况
     {
         return  (* this  - (-n));
     }
     return  tmp;
}
 
//重载+=运算符
Date& operator+=( int  n)
{
     * this  = * this  + n;
     return  * this ;
}
 
//重载-=运算符
Date& operator-=( int  day)
{
     * this  = * this  - day;
     return  * this ;
}
 
//重载前置++运算符
Date& operator++()
{
     * this  = * this  + 1;
     return  * this ;
}
 
//重载后置++运算符
Date operator++( int )
{
     Date tmp(* this );
     * this  = * this  + 1;
     return  tmp;
}
 
//Date operator++(int)
//{
//    Date tmp(*this);
//    ++(*this);
//    return tmp;
//}
 
//重载前置--运算符
Date& operator--()
{
     * this  = * this  - 1;
     return  * this ;
}
 
//重载后置--运算符
Date operator--( int )
{
     Date tmp(* this );
     * this  = * this  - 1;
     return  tmp;
}
 
//Date operator--(int)
//{
//    Date tmp(*this);
//    --(*this);
//    return tmp;
//}
 
//重载>运算符
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)
//{
//    if (_year > d._year)
//    {
//        return true;
//    }
//    else if (_year == d._year)
//    {
//        if (_month > d._month)
//        {
//            return true;
//        }
//        else if(_month == d._month)
//        {
//            if (_day > d._day)
//            {
//                return true;
//            }
//        }
//    }
//    return false;
//}
 
//重载==运算符
bool  operator==( const  Date& d)
{
     return  _year==d._year
     &&_month==d._month
     &&_day==d._day;
}
 
//重载<运算符
bool  operator<( const  Date& d)
{
     return  !(* this >d || * this  == d);
}
 
//重载!=运算符
bool  operator!=(Date& d)
{
     return  !(* this  == d);

本文转自 七十七快 51CTO博客,原文链接:http://blog.51cto.com/10324228/1722483


相关文章
|
4天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
14 0
|
2天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
7 1
|
2天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
9 0
|
2天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
10 0
【C++】string学习 — 手搓string类项目
|
3天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)
|
3天前
|
编译器 C++
【C++从练气到飞升】06---重识类和对象(一)
【C++从练气到飞升】06---重识类和对象(一)
|
3天前
|
存储 编译器 C语言
【C++从练气到飞升】02---初识类与对象
【C++从练气到飞升】02---初识类与对象
|
4天前
|
设计模式 算法 编译器
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
9 0
|
4天前
|
算法 安全 调度
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
13 1
|
4天前
|
算法 编译器 C++
【C++入门到精通】新的类功能 | 可变参数模板 C++11 [ C++入门 ]
【C++入门到精通】新的类功能 | 可变参数模板 C++11 [ C++入门 ]
20 1