1 #include <iostream>
2 using namespace std;
3 class tmend
4 {
5 private:
6 int s,f,m;
7 public:
8 tmend()
9 {
10 s = 0;
11 f = 0;
12 m = 0;
13 }
14 tmend(int s1,int f1,int m1)
15 {
16 s=s1; f=f1; m=m1;
17 }
18 tmend operator+ (tmend &t)
19 {
20 tmend ti;
21 ti.s = this->s + t.s;
22 ti.f = this->f + t.f;
23 ti.m = this->m + t.m;
24 if(ti.m > 60)
25 {
26 ti.f = ti.f/60 + ti.f;
27 ti.m = ti.m%60;
28 }
29 if(ti.f > 60)
30 {
31 ti.s = ti.s + ti.f/60;
32 ti.f = ti.f%60;
33 }
34 return ti;
35 }
36 tmend operator- (tmend &t)
37 {
38 tmend ti;
39 if(this->m < t.m)
40 {
41 this->m = this->m + 60;
42 this->f = this->f-1;
43 }
44 if(this->f < t.f)
45 {
46 this->f = this->f + 60;
47 this->s = this->s - 1;
48 }
49 if(this->s < t.s)
50 {
51 ti.s = 0;
52 }
53 else
54 {
55 ti.s = this->s - t.s;
56 }
57 ti.m = this->m - t.m;
58 ti.f = this->f - t.f;
59
60 return ti;
61 }
62 tmend operator+ (int n)
63 {
64 tmend ti;
65 ti.m = this->m + n;
66 if(this->m >= 60)
67 {
68 this->f = this->f + this->m/60;
69 this->m = this->m%60;
70 }
71 if(this->f >= 60)
72 {
73 this->s = this->s + this->f/60;
74 this->f = this->f%60;
75 }
76 ti.f = this->f;
77 ti.s = this->s;
78 return ti;
79 }
80 tmend operator- (int n)
81 {
82 tmend ti;
83 ti.m = this->m - n;
84 if(this->m < n)
85 {
86 this->f = this->f-1;
87 this->m = this->m+60;
88 }
89 if(this->f < 0)
90 {
91 this->s = this->s - 1;
92 this->f = this->f+60;
93 }
94 ti.m = this->m;
95 ti.f = this->f;
96 ti.s = this->s;
97 return ti;
98 }
99 int &operator[] (int a)
100 {
101 switch(a)
102 {
103 case 0:
104 return s;
105 case 1:
106 return f;
107 case 2:
108 return m;
109 default:
110 cout<<"输入错误"<<endl;
111 }
112 }
113
114 void show() { cout<<s<<":"<<f<<":"<<m<<endl; }
115
116
117 };
118 int main(int argc, const char *argv[])
119 {
120 tmend ti;
121 tmend t1(4,50,49);
122 cout<<t1[2]<<endl;
123 tmend t2(3,39,57);
124 ti = t1+t2;
125 ti.show();
126 ti = t1-t2;
127 ti.show();
128 cout<<t1[0]<<endl;
129 cout<<t1[1]<<endl;
130 t1[1] = 55;
131 cout<<t1[1]<<endl;
132 return 0;
133 }
03-16
16万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-21
3871
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-25
5405
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)