This commit is contained in:
xdrm-brackets 2016-03-06 17:29:02 +01:00
parent f8501300ab
commit d01c4eb135
9 changed files with 188 additions and 16 deletions

View File

@ -11,3 +11,7 @@ Lab de prog. en C++.
[ex4] Fichiers
[tp1] Mot mystere
[ex5] Surcharge d'operateurs (Duree)
[tp2] Surcharge d'operateurs (Fraction)

20
ex5.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
#include <string>
#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;
}

85
ex5/Duree.cpp Normal file
View File

@ -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;
}

36
ex5/Duree.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef DEF_DUREE
#define DEF_DUREE
#include <iostream>
#include <string>
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

BIN
exec/ex5 Executable file

Binary file not shown.

BIN
exec/tp2

Binary file not shown.

10
tp2.cpp
View File

@ -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;
}

View File

@ -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 &out;
return ZFraction(new_a, new_b);
}
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;
}

View File

@ -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;
};