记一道C++作业题

如题:题目要求根据初始化时的日期加上一定的天数后计算得到的日期,这里用到的计算思路来自这里

image-20210412221018035

只要稍做改动,就能写出代码来了,不过要注意原题计算的是日期差+1!

这题主要考察了日期问题、++前缀与后缀重载等知识点。

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
#include <iostream>
#include <cstdio>
using namespace std;
// 判断闰年,是闰年返回true
bool isLeap(int y){
return (y%4==0 &&y%100!=0) || (y%400==0);
}
//顺序存放平年、闰年每月对应天数
int month[13][2] = {
{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},
{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}
};

class Date{
public:
Date(){
y = 2000,m = 1,d = 1;
}
Date(int _y,int _m,int _d):
y(_y),m(_m),d(_d){}
// Copy Constructor
Date(Date& t){
y = t.y,m = t.m,d = t.d;
}
void print(){
printf("Y:%d,M:%2d,D:%2d\n",y,m,d);
}
friend Date operator+ (Date& D,int n);// 重载加号
friend Date operator++ (Date& D,int);// 重载后缀++
friend Date operator++ (Date& D);// 重载++前缀
private:
int y;
int m;
int d;
};
Date operator+ (Date& D,int n){
Date temp = D;
int ans = 0;// 累加ans到n
// ans == n退出
while (ans < n){
// 优化,降低循环次数
if (n- ans > 366){

if (isLeap(temp.y)) ans += 366;
else ans += 365;
temp.y ++;
continue;
}
temp.d ++;
if (temp.d == month[temp.m][isLeap(temp.y)]+1){
temp.d = 1;temp.m ++;
}
if (temp.m == 13){
temp.m = 1;temp.y ++;
}
ans ++;
}
return temp;
}
// 重载后缀++
Date operator++ (Date& D,int){
Date temp = D;
D.d ++;
if (D.d == month[D.m][isLeap(D.y)]+1){
D.d = 1;D.m ++;
}
if (D.m == 13){
D.m = 1;D.y ++;
}
return temp;
}
// 重载++前缀
Date operator++ (Date& D){
D.d ++;
if (D.d == month[D.m][isLeap(D.y)]+1){
D.d = 1;D.m ++;
}
if (D.m == 13){
D.m = 1;D.y ++;
}
return D;
}
int main(){
Date d1(2019,12,31),d2,d3;
d2 = d1++;
d1.print();
d2.print();
d3 = ++d1;
d1.print();
d3.print();
d1 = d1 + 365;
d1.print();
return 0;
}
坚持原创技术分享,您的支持将鼓励我继续创作!