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

本文介绍了一个日期类的设计与实现,重点在于通过重载运算符来方便地进行日期加减操作。包括如何实现日期的加减、比较等基本功能,并确保日期的有效性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


(1) 测试日期成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期编写时间CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值