質問編集履歴
1
コードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -9,3 +9,106 @@
|
|
9
9
|
C:\Users\user\AppData\Local\Temp\ccE3QDsf.o:DateTest1.cpp:(.text+0xd5): undefined reference to `operator<<(std::ostream&, Date const&)'
|
10
10
|
collect2.exe: error: ld returned 1 exit status
|
11
11
|
```
|
12
|
+
|
13
|
+
コード
|
14
|
+
```C++
|
15
|
+
#ifndef ___Class_Date
|
16
|
+
#define ___Class_Date
|
17
|
+
|
18
|
+
#include <string>
|
19
|
+
#include <iostream>
|
20
|
+
|
21
|
+
class Date{
|
22
|
+
int y;
|
23
|
+
int m;
|
24
|
+
int d;
|
25
|
+
|
26
|
+
public:
|
27
|
+
Date();
|
28
|
+
Date(int yy, int mm = 1, int dd =1);
|
29
|
+
|
30
|
+
int year() const { return y; }
|
31
|
+
int month() const { return m; }
|
32
|
+
int day() const { return d; }
|
33
|
+
|
34
|
+
Date preceding_day() const;
|
35
|
+
|
36
|
+
std::string to_string() const;
|
37
|
+
|
38
|
+
int day_of_week() const;
|
39
|
+
};
|
40
|
+
|
41
|
+
std::ostream& operator<<(std::ostream& s, const Date& x);
|
42
|
+
std::istream& operator>>(std::istream& s, Date& x);
|
43
|
+
|
44
|
+
#endif
|
45
|
+
```
|
46
|
+
```C++
|
47
|
+
#include <ctime>
|
48
|
+
#include <sstream>
|
49
|
+
#include <iostream>
|
50
|
+
#include "Date.h"
|
51
|
+
|
52
|
+
using namespace std;
|
53
|
+
|
54
|
+
Date::Date(){
|
55
|
+
time_t current = time(NULL);
|
56
|
+
struct tm* local = localtime(¤t);
|
57
|
+
|
58
|
+
y = local->tm_year + 1900;
|
59
|
+
m = local->tm_mon + 1;
|
60
|
+
d = local->tm_mday;
|
61
|
+
}
|
62
|
+
|
63
|
+
Date::Date(int yy, int mm, int dd){
|
64
|
+
y = yy;
|
65
|
+
m = mm;
|
66
|
+
d = dd;
|
67
|
+
}
|
68
|
+
|
69
|
+
Date Date::preceding_day() const{
|
70
|
+
int dmax[] = {31,28,31,30,31,30,31,31,30,31,30,31};
|
71
|
+
Date temp = *this;
|
72
|
+
|
73
|
+
if (temp.d > 1)
|
74
|
+
temp.d--;
|
75
|
+
else{
|
76
|
+
if(--temp.m < 1){
|
77
|
+
temp.y--;
|
78
|
+
temp.m = 12;
|
79
|
+
}
|
80
|
+
temp.d = dmax[temp.m - 1];
|
81
|
+
}
|
82
|
+
return temp;
|
83
|
+
}
|
84
|
+
|
85
|
+
string Date::to_string() const{
|
86
|
+
ostringstream s;
|
87
|
+
s << y << "年" << m << "月" << d << "日";
|
88
|
+
return s.str();
|
89
|
+
}
|
90
|
+
|
91
|
+
int Date::day_of_week() const{
|
92
|
+
int yy = y;
|
93
|
+
int mm = m;
|
94
|
+
if ( mm == 1 || mm == 2){
|
95
|
+
yy--;
|
96
|
+
mm += 12;
|
97
|
+
}
|
98
|
+
return (yy + yy / 4 - yy / 100 + yy / 400 + (13 * mm + 8) / 5 + d) % 7;
|
99
|
+
}
|
100
|
+
```
|
101
|
+
```C++
|
102
|
+
#include <iostream>
|
103
|
+
#include "Date.h"
|
104
|
+
|
105
|
+
using namespace std;
|
106
|
+
|
107
|
+
int main(){
|
108
|
+
Date today;
|
109
|
+
|
110
|
+
cout << "今日" << today << "\n";
|
111
|
+
cout << "昨日" << today.preceding_day() << "\n";
|
112
|
+
cout << "昨日" << today.preceding_day().preceding_day() << "\n";
|
113
|
+
}
|
114
|
+
```
|