From d01c4eb1354fea01410a085e5913c1bc8a7fcaf0 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 6 Mar 2016 17:29:02 +0100 Subject: [PATCH] Seance #5 --- README.md | 6 +++- ex5.cpp | 20 +++++++++++ ex5/Duree.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++ ex5/Duree.h | 36 ++++++++++++++++++++ exec/ex5 | Bin 0 -> 13858 bytes exec/tp2 | Bin 9205 -> 13598 bytes tp2.cpp | 10 +++--- tp2/ZFraction.cpp | 34 +++++++++++++++---- tp2/ZFraction.h | 13 ++++--- 9 files changed, 188 insertions(+), 16 deletions(-) create mode 100644 ex5.cpp create mode 100644 ex5/Duree.cpp create mode 100644 ex5/Duree.h create mode 100755 exec/ex5 diff --git a/README.md b/README.md index daa39f6..fc12e72 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,8 @@ Lab de prog. en C++. [ex4] Fichiers -[tp1] Mot mystere \ No newline at end of file +[tp1] Mot mystere + +[ex5] Surcharge d'operateurs (Duree) + +[tp2] Surcharge d'operateurs (Fraction) diff --git a/ex5.cpp b/ex5.cpp new file mode 100644 index 0000000..24857d1 --- /dev/null +++ b/ex5.cpp @@ -0,0 +1,20 @@ +#include +#include + +#include "ex5/Duree.h" + +using namespace std; + + + +int main(){ + Duree d1(0, 10, 30); + Duree d2(2, 14, 89); + + cout << d1 << " + " << d2 << " -> " << d1+d2 << endl; + cout << d1 << " == " << d2 << " -> " << (d1==d2) << endl; + cout << 2 << " * " << d2 << " -> " << (2*d2) << endl; + + + return 0; +} \ No newline at end of file diff --git a/ex5/Duree.cpp b/ex5/Duree.cpp new file mode 100644 index 0000000..2bdaa2a --- /dev/null +++ b/ex5/Duree.cpp @@ -0,0 +1,85 @@ +/* [x] Constructeur +=========================================================*/ +void Duree::fix(){ + int stack(0); + + // Gestion du debordement de secondes + stack = _s; + if( stack >= 60 ){ + _s = stack % 60; + _m += stack / 60; + } + + // Gestion du debordement de minutes + stack = _m; + if( stack >= 60 ){ + _m = stack % 60; + _h += stack / 60; + } +} + +Duree::Duree(int h, int m, int s) : _h(h), _m(m), _s(s){ + this->fix(); +} + +Duree::Duree(const Duree& d) : _h(d._h), _m(d._m), _s(d._s){ + this->fix(); +} + +/* [0] Affichage +=========================================================*/ +ostream& operator<<(ostream& o, const Duree& d){ + o << "[" << d._h << ":" << d._m << "," << d._s << "]"; + + return o; +} + + + +/* [1] Egalite +=========================================================*/ +bool Duree::operator==(const Duree& x){ + return _h == x._h && _m == x._m && _s == x._s; +} + + + +/* [2] Addition +=========================================================*/ +void Duree::operator+=(const Duree& d){ + _h += d._h; + _m += d._m; + _s += d._s; + + this->fix(); +} + +Duree operator+(Duree& l, Duree& r){ + Duree tmp(l); + tmp += r; + return tmp; +} + + + +/* [3] Multiplication +=========================================================*/ +void Duree::operator*=(const int d){ + _h *= d; + _m *= d; + _s *= d; + + this->fix(); +} + +Duree operator*(int l, Duree& r){ + Duree tmp(r); + tmp *= l; + return tmp; +} + +Duree operator*(Duree& l, int r){ + Duree tmp(l); + tmp *= r; + return tmp; +} \ No newline at end of file diff --git a/ex5/Duree.h b/ex5/Duree.h new file mode 100644 index 0000000..521f1fa --- /dev/null +++ b/ex5/Duree.h @@ -0,0 +1,36 @@ +#ifndef DEF_DUREE + + #define DEF_DUREE + #include + #include + using namespace std; + + class Duree{ + public: + Duree(int h=0, int m=0, int s=0); + Duree(const Duree& d); + + bool operator==(const Duree& x); + void operator+=(const Duree& d); + void operator*=(const int d); + + private: + int _h; + int _m; + int _s; + void fix(); + friend ostream& operator<<(ostream& o, const Duree& d); + + }; + + + // Operateurs binaires + Duree operator+(Duree& l, Duree& r); + Duree operator*(int l, Duree& r); + Duree operator*(Duree& l, int r); + + + + #include "Duree.cpp" + +#endif \ No newline at end of file diff --git a/exec/ex5 b/exec/ex5 new file mode 100755 index 0000000000000000000000000000000000000000..6acc3f6d9ebbbeda92f3c0cc0930b8dfbb191c07 GIT binary patch literal 13858 zcmeHOdvH|Oc|W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J{$vbAXW$So zpDe}n*qh4*Ko@ske6PZ*_xc&JU;KBDHGh_dH-jLT1CUJr?T%<0dkz|f$f<82Qq|l{plEkQbZZns`^V1FE3{l z#SU7=-(bVpewz(vcTDLzfy=h4{!o=)&5BBBV`bU=?OW~r>c`Vdiwn}n@lcFhf1bbS zGcQ;EPA>#V-?jb| z-xh%jy8|A#78u`U@f^Ptx37xk7_1O~rQukTXw7xtG{PP~4G*VSB0LMU5`CY-H5(pK z_@E8H2e<=q?!);SB_1DpWPWaY9tBS29=G}VmeilyFDHSQtJkR1zfRD(9wjdKH^`I! zAFKVWJOcto4MRBZb9o;~cIEtk0s;qaQu-EY&?u(05}(_iq*2b#?NWbkzu!#-<3`E; zz4a=b*HO}bw@AznXI_^(xe z-Jk@&0PbZa3;o?Z_}kLY+;RPm#Fq%W|0)DlF8*8Q!8gx?#}z(mt5*Sdx%j^~kDrUc zm*P)_=O6x$`ujY7uEx6IDbp`r;7gsgv-dvSbhi?28Si{=*Q)gC5ZjtMfiJ`N{5RDv zaV5N8;U3k$356d7?y>l1Uq=)^pvDp7!xEnx=VQu0-Iq}6k#8ZMlz5r>KbAFeMjj{K zxVFPEjFqpQW7;2T8?r0emFzBiTWji&Utk;&%tXnsIo|GO`h zFcNX!`VF612hU}cblQoV`)VC~${p8b-GD3a5G6rGMY*r1c$rSXgZGTQk+8MW0G%3;)Zim$F`P02Tbz4db^IR z&kegaW}{M}wJAs%`o`^nEkQlFwTZ&we_xI&-jf@myu1T_tFKsdCN^bX@06YPKvlY Q6KO=0kOxTe0^C&jCw>_AF#rGn diff --git a/tp2.cpp b/tp2.cpp index f94194a..9a6cb2a 100644 --- a/tp2.cpp +++ b/tp2.cpp @@ -7,11 +7,13 @@ using namespace std; int main(){ - ZFraction f1(1,10); - ZFraction f2(10); - ZFraction f3(10, 3); + ZFraction f1(1, 2); + ZFraction f2(1, 4); - f1.toString(); + + + cout << f1 << "+" << f2 << " = " << f1+f2 << endl; + cout << f1 << "*" << f2 << " = " << f1*f2 << endl; return 0; } \ No newline at end of file diff --git a/tp2/ZFraction.cpp b/tp2/ZFraction.cpp index 848b9ba..c875daf 100644 --- a/tp2/ZFraction.cpp +++ b/tp2/ZFraction.cpp @@ -1,23 +1,43 @@ /* [0] CONSTRUCTEUR =========================================================*/ -ZFraction::ZFraction(int a, int b) : a(a), b(b){} +ZFraction::ZFraction(int a, int b) : _a(a), _b(b){} /* [1] Getters =========================================================*/ -void ZFraction::toString() const{ cout << "(" << this->a << "/" << this->b << ")"; } +ostream& operator<<(ostream& o, const ZFraction& f){ + o << "(" << f._a << "/" << f._b << ")"; + + return o; +} /* [2] Operateurs =========================================================*/ /* (1) Addition */ - ZFraction& operator+=(ZFraction const& x){ - int new_a = this->a * x.b + this->b * x.a; - int new_b = this->b * x.b; + ZFraction ZFraction::operator+(const ZFraction& x) const{ + int new_a = _a * x._b + _b * x._a; + int new_b = _b * x._b; - ZFraction out = ZFraction(new_a, new_b); + return ZFraction(new_a, new_b); + } - return &out; + ZFraction& ZFraction::operator+=(const ZFraction& x){ + *this = *this + x; + return *this; + } + + /* (2) Multiplication */ + ZFraction ZFraction::operator*(const ZFraction& x) const{ + int new_a = _a * x._a; + int new_b = _b * x._b; + + return ZFraction(new_a, new_b); + } + + ZFraction& ZFraction::operator*=(const ZFraction& x){ + *this = *this * x; + return *this; } \ No newline at end of file diff --git a/tp2/ZFraction.h b/tp2/ZFraction.h index 1aed4ae..05e9357 100644 --- a/tp2/ZFraction.h +++ b/tp2/ZFraction.h @@ -11,13 +11,18 @@ public: ZFraction(int a=1, int b=1); - void toString() const; - ZFraction& operator+=(ZFraction const& operand); + ZFraction operator+(const ZFraction& x) const; + ZFraction& operator+=(const ZFraction& x); + + ZFraction operator*(const ZFraction& x) const; + ZFraction& operator*=(const ZFraction& x) const; private: - int a; - int b; + friend ostream& operator<<(ostream& o, const ZFraction& f); + + int _a; + int _b; };