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 0000000..6acc3f6 Binary files /dev/null and b/exec/ex5 differ diff --git a/exec/tp2 b/exec/tp2 index 17ca2ff..ca2e098 100755 Binary files a/exec/tp2 and b/exec/tp2 differ 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; };