Rylah's Study & Daily Life
날짜 계산 본문
씹어먹는 C++ - <4 - 1. 이 세상은 객체로 이루어져 있다>
modoocode.com
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
|
#include <iostream>
class Date {
private:
int year_;
int month_;
int day_;
public:
void SetDate(int year, int month, int date);
void AddDay(int inc);
void AddMonth(int inc);
void AddYear(int inc);
void ShowDate();
bool isLeapYear(int year);
};
void Date::SetDate(int year, int month, int date)
{
this->year_ = year;
this->month_ = month;
this->day_ = date;
}
bool Date::isLeapYear(int year)
{
if ((year_ % 4 == 0 && year_ % 100 != 0) || year_ % 400 == 0)
return true;
return false;
}
void Date::AddYear(int inc)
{
year_ += inc;
AddDay(0);
}
void Date::AddMonth(int inc)
{
month_ += inc;
while (month_ > 12)
{
year_++;
month_ -= 12;
}
AddDay(0);
}
void Date::AddDay(int inc)
{
const int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
day_ += inc;
if (isLeapYear(year_) && month_ == 2)
{
if (day_ > days[month_] + 1)
{
// Leap Year Case
day_ = day_ - days[month_] - 1;
AddMonth(1);
}
}
else
{
while (day_ > days[month_])
{
// Normal Case
day_ -= days[month_];
AddMonth(1);
}
}
}
void Date::ShowDate()
{
std::cout << this->year_ << "/" << this->month_ << "/" << this->day_ << std::endl;
}
int main(void)
{
Date d;
d.SetDate(2022, 3, 22);
d.ShowDate();
d.AddDay(3);
d.ShowDate();
d.AddDay(49997);
d.ShowDate();
d.AddMonth(5000);
d.ShowDate();
Date da;
da.SetDate(2020, 1, 29);
da.AddMonth(1);
da.ShowDate();
//da.AddDay(500);
//da.ShowDate();
//
//da.AddMonth(25);
//da.ShowDate();
//da.AddYear(1);
//da.ShowDate();
return 0;
}
|
cs |
1. Day에서 더해준다.
2. Month에서 더해준 다음 AddDay(0)을 통해 달이 초과한지 다시 점검한다.
3. Year도 동일
'Study > C++' 카테고리의 다른 글
rax eax (0) | 2022.03.24 |
---|---|
.bss .data .rodata (0) | 2022.03.23 |
Modern C++ : 03. C++ Build : Debug (0) | 2022.03.14 |
Visual Studio 단축키 (0) | 2022.03.14 |
Modern C++ : 03. C++ Build : Assembly (0) | 2022.03.14 |