复制代码
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#include<iostream> #include<string> using namespace std; class Time { public: void display(){cout<<hour<<":"<<minute<<":"<<sec<<endl;} protected: int hour; int minute; int sec; }; class Date { public: void display1(){cout<<year<<":"<<month<<":"<<day<<endl;} protected: int year; int month; int day; }; class DateTime:public Time,public Date { public: friend istream& operator >>(istream &input,DateTime &t1); friend ostream& operator <<(ostream &output,DateTime &t1); friend DateTime operator +(DateTime &t1,DateTime &t2); friend DateTime operator -(DateTime &t1,DateTime &t2); private: string place; }; istream& operator >>(istream &input,DateTime &t1) { input>>t1.place>>t1.year>>t1.month>>t1.day>>t1.hour>>t1.minute>>t1.sec; return input; } ostream& operator<<(ostream &output,DateTime &t1) { output<<t1.place<<":"<<t1.year<<"/"<<t1.month<<"/"<<t1.day<<"/"<<t1.hour<<":"<<t1.minute<<":"<<t1.sec; return output; } DateTime operator +(DateTime &t1,DateTime &t2) { DateTime t3; t3.sec=t1.sec+t2.sec; t3.minute=t1.minute+t2.minute; t3.hour=t1.hour+t2.hour; t3.day=t1.day+t2.day; t3.month=t1.month+t2.month; t3.year=t1.year+t2.year; t3.place=t1.place; if(t3.sec>60) { t3.sec-=60; t3.minute+=1; } if(t3.minute>60) { t3.minute-=60; t3.hour+=1; } if(t3.hour>24) { t3.hour-=24; t3.day+=1; } if(t3.month==1||3||5||7||8||10||12) { if(t3.day>31) { t3.day-=31; t3.month+=1; } } if(t3.month==4||6||8||10||11) { if(t3.day>30) { t3.day-=30; t3.month+=1; } } if(t3.month==2) { if(t3.day>28) { t3.day-=28; t3.month+=1; } } if(t3.month>12) { t3.month-=12; t3.year+=1; } return t3; } DateTime operator -(DateTime &t1,DateTime &t2) { DateTime t3; t3.sec=t1.sec-t2.sec; t3.minute=t1.minute-t2.minute; t3.hour=t1.hour-t2.hour; t3.day=t1.day-t2.day; t3.month=t1.month-t2.month; t3.year=t1.year-t2.year; t3.place=t1.place; if(t3.sec<0) { t3.sec+=60; t3.minute-=1; } if(t3.minute<0) { t3.minute+=60; t3.hour-=1; } if(t3.hour<0) { t3.hour+=24; t3.day-=1; } if(t3.month==1||3||5||7||8||10||12) { if(t3.day<0) { t3.day+=31; t3.month-=1; } } if(t3.month==4||6||8||10||11) { if(t3.day<0) { t3.day+=30; t3.month-=1; } } if(t3.month==2) { if(t3.day<0) { t3.day+=28; t3.month-=1; } } if(t3.month<0) { t3.month+=12; t3.year-=1; } return t3; } int main() { DateTime d1,d2,d3,d4; cout<<"地点:"<<'t'<<"年:"<<'t'<<"月"<<'t'<<"日"<<'t'<<"时"<<'t'<<"分"<<'t'<<"秒"<<endl; cin>>d1; cin>>d2; cout<<"两时间相加为:"<<endl; d3=d1+d2; d4=d1-d2; cout<<d3<<endl; cout<<"两时间相减为:"<<endl; cout<<d4<<endl; }
最后
以上就是着急枕头最近收集整理的关于用重载时间相加的全部内容,更多相关用重载时间相加内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复