DL C++
DL C++
DL C++
h" using namespace std; int CRationnel::GetNum() { return this->Num; } int CRationnel::GetDen() { return this->Den; } CRationnel::CRationnel() { this->Num=0; this->Den=1; } CRationnel::CRationnel(int n,int d) { this->Num=n; this->Den=d; } void CRationnel::SetDen(int d) { this->Den=d; } void CRationnel::SetNum(int n) { this->Num=n; } /* Les Operateurs */ ostream& operator<<(ostream &os,const CRationnel &r1) { os<<' '<<r1.Num<<'/'<<r1.Den<<' '<<endl; return os; } istream& operator>>(istream &is, CRationnel &r1) { char c; is>>c; int n,d; if(c=='('){ cin>>n>>c; if(c=='/'){ cin>>d>>c; if(c==')') r1.SetNum(n); r1.SetDen(d);} else cout<<"Erreur de lecture"<<endl; } } CRationnel operator+(CRationnel &r1,CRationnel &r2) { CRationnel temp; int a=r1.GetNum()*r2.GetDen()+r2.GetNum()*r1.GetDen(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a);
temp.SetDen(b); temp.reduire(); return temp; } CRationnel operator+(CRationnel &r1,int n) { CRationnel temp; CRationnel r2=CRationnel(n,1); int a=r1.GetNum()*r2.GetDen()+r2.GetNum()*r1.GetDen(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); return temp; } CRationnel operator-(CRationnel &r1,CRationnel &r2) { CRationnel temp; int a=r1.GetNum()*r2.GetDen()-r2.GetNum()*r1.GetDen(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); return temp; } CRationnel operator-(CRationnel &r1,int n) { CRationnel temp; CRationnel r2=CRationnel(n,1); int a=r1.GetNum()*r2.GetDen()-r2.GetNum()*r1.GetDen(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); return temp; } CRationnel operator*(CRationnel &r1,CRationnel &r2) { CRationnel temp; int a=r1.GetNum()*r2.GetNum(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); return temp; } CRationnel operator*(CRationnel &r1,int n) { CRationnel temp; CRationnel r2=CRationnel(n,1); int a=r1.GetNum()*r2.GetNum(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); return temp; } CRationnel operator/(CRationnel &r1,CRationnel &r2) { CRationnel temp;
int a=r1.GetNum()*r2.GetDen(); int b=r1.GetDen()*r2.GetNum(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); return temp; } CRationnel operator/(CRationnel &r1,int n) { CRationnel temp; CRationnel r2=CRationnel(n,1); int a=r1.GetNum()*r2.GetDen(); int b=r1.GetDen()*r2.GetNum(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); return temp; } void operator+=(CRationnel &r1,CRationnel &r2) { CRationnel temp; int a=r1.GetNum()*r2.GetDen()+r2.GetNum()*r1.GetDen(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); r1=temp; } void operator-=(CRationnel &r1,CRationnel &r2) { CRationnel temp; int a=r1.GetNum()*r2.GetDen()-r2.GetNum()*r1.GetDen(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); r1=temp; } void operator*=(CRationnel &r1,CRationnel &r2) { CRationnel temp; int a=r1.GetNum()*r2.GetNum(); int b=r1.GetDen()*r2.GetDen(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); r1=temp; } void operator/=(CRationnel &r1,CRationnel &r2) { CRationnel temp; int a=r1.GetNum()*r2.GetDen(); int b=r1.GetDen()*r2.GetNum(); temp.SetNum(a); temp.SetDen(b); temp.reduire(); r1=temp; } bool operator==(CRationnel &r1,CRationnel &r2)
{ r1.reduire(); r1.reduire(); if(r1.GetNum()==r2.GetNum() && r1.GetDen()==r2.GetDen()) return true; else return false; } bool operator!=(CRationnel &r1,CRationnel &r2) { if(r1==r2) return false; else return true; } bool operator>(CRationnel &r1,CRationnel &r2) { double a=double(r1.GetNum())/double(r1.GetDen()); double b=double(r2.GetNum())/double(r2.GetDen()); if(a>b) return true; else return false; } bool operator>=(CRationnel &r1,CRationnel &r2) { double a=double(r1.GetNum())/double(r1.GetDen()); double b=double(r2.GetNum())/double(r2.GetDen()); if(a>=b) return true; else return false; } bool operator<(CRationnel &r1,CRationnel &r2) { double a=double(r1.GetNum())/double(r1.GetDen()); double b=double(r2.GetNum())/double(r2.GetDen()); if(a<b) return true; else return false; } bool operator<=(CRationnel &r1,CRationnel &r2) { double a=double(r1.GetNum())/double(r1.GetDen()); double b=double(r2.GetNum())/double(r2.GetDen()); if(a<=b) return true; else return false; } void CRationnel::reduire( ) {int i; int c; if(this->Num>this->Den) c=this->Num; else c=this->Den; if ((this->Num==this->Den) && (this->Num=!0)) { this->Num=1; this->Den=1; } else { for( i=c/2;i>1;i--) { if ( (this->Num%i ==0) && (this->Den%i== 0)) break; } this->Num=this->Num/i; this->Den=this->Den/i; }