-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duree.cpp
126 lines (125 loc) · 2.53 KB
/
Duree.cpp
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
#include "Duree.h"
#include <iostream>
using namespace std;
Duree::Duree(int heures, int minutes, int secondes) : m_heures(heures), m_minutes(minutes), m_secondes(secondes)
{
if (m_minutes > 59)
{
m_heures+=minutes/60;
m_minutes=minutes%60;
}
if (secondes > 59)
{
m_minutes+=secondes/60;
m_secondes=secondes%60;
}
}
bool Duree::estEgal(Duree const& b) const
{
return (m_heures==b.m_heures && m_minutes==b.m_minutes && m_secondes==b.m_secondes);
}
bool Duree::estPlusPetitQue(Duree const& b) const
{
if(m_heures<b.m_heures)
return true;
else if (m_heures==b.m_heures && m_minutes<<b.m_minutes)
return true;
else if (m_heures==b.m_heures && m_minutes==b.m_minutes && m_secondes<b.m_secondes)
return true;
else
return false;
}
void Duree::operator+=(const Duree &a)
{
m_secondes+=a.m_secondes;
m_minutes+=m_secondes/60;
m_secondes%=60;
m_minutes+=a.m_minutes;
m_heures+=m_minutes/60;
m_minutes%=60;
m_heures+=a.m_heures;
}
void Duree::operator-=(const Duree &a)
{
m_secondes-=a.m_secondes;
if(m_secondes<0)
{
m_secondes+=60;
m_minutes--;
}
m_minutes-=m_secondes/60;
m_secondes%=60;
m_minutes-=a.m_minutes;
if(m_minutes<0)
{
m_minutes+=60;
m_heures--;
}
m_heures-=m_minutes/60;
m_minutes%=60;
m_heures-=a.m_heures;
}
bool operator==(Duree const& a, Duree const& b)
{
return a.estEgal(b);
}
bool operator!=(Duree const& a, Duree const& b)
{
return !(a==b);
}
bool operator<(Duree const& a, Duree const& b)
{
return a.estPlusPetitQue(b);
}
bool operator>(Duree const& a, Duree const& b)
{
return !(a==b && a<b);
}
bool operator<=(Duree const& a, Duree const& b)
{
return a<b || a==b;
}
bool operator>=(Duree const& a, Duree const& b)
{
return a>b || a==b;
}
ostream &operator<<(ostream &flux, Duree const& duree)
{
duree.afficher(flux);
return flux;
}
void Duree::afficher(ostream &flux) const
{
flux << m_heures << "h" << m_minutes << "m" << m_secondes << "s";
}
Duree operator+(Duree const& a, Duree const& b)
{
Duree copie(a);
copie+=b;
return copie;
}
Duree operator-(Duree const& a, Duree const& b)
{
Duree copie(a);
copie-=b;
return copie;
}
int main()
{
Duree d1(0, 28, 10), d2(0, 28, 10), d3(0, 120, 184);
if (d1!=d2)
{
cout << "d1!=d2" << endl;
}
else
cout << "d1==d2" << endl;
if (d1<d3)
cout << "d1<d3" << endl;
if (d3 > d2)
cout << "d3=" << d3 << ">" << "d2=" << d2 << endl;
Duree d4(4, 32, 50);
cout << "d1+d4=" << (d1+d4) << endl;
Duree d5(3, 33, 51);
cout << "d4-d5=" << (d4-d5) << endl;
return 0;
}