rationnal.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef RATIONNAL_HPP
  2. #define RATIONNAL_HPP
  3. #include <iostream>
  4. #include <cstdint>
  5. #include <iostream>
  6. #include "config.hpp"
  7. using namespace std;
  8. //*************
  9. //* Rationnal *
  10. //*************
  11. //! Class for representing rationnals.
  12. class Rationnal{
  13. private:
  14. //! Numerator
  15. Int num;
  16. //! Denominator
  17. Int den;
  18. //! Normalise th fraction
  19. void normalize();
  20. public:
  21. //! Construct the null rationnal
  22. Rationnal();
  23. //! Construct the rationnal n
  24. //! \param n integer
  25. Rationnal(Int n);
  26. //! Construct the rationnal n/d
  27. //! \param n numerator integer
  28. //! \param d denominator integer
  29. Rationnal(Int n,Int d);
  30. //! Copy constructor
  31. //! \param r rationnal to copy
  32. Rationnal(const Rationnal& r);
  33. //! Copy operator
  34. //! \param r rationnal to copy
  35. Rationnal& operator=(const Rationnal& r);
  36. //! Numerator accessor
  37. Int numerator() const;
  38. //! Denominator accessor
  39. Int denominator() const;
  40. //! Return an approximation of the rationnal
  41. explicit operator Reel() const;
  42. //! Addition operators
  43. Rationnal operator+(const Rationnal& r) const;
  44. Rationnal operator+(Int n) const;
  45. friend Rationnal operator+(Int n,const Rationnal& r);
  46. //! Substration operators
  47. Rationnal operator-(const Rationnal& r) const;
  48. Rationnal operator-(Int n) const;
  49. friend Rationnal operator-(Int n,const Rationnal& r);
  50. //! Multiplication operators
  51. Rationnal operator*(const Rationnal& r) const;
  52. Rationnal operator*(Int n) const;
  53. friend Rationnal operator*(Int n,const Rationnal& r);
  54. //! Division operators
  55. Rationnal operator/(const Rationnal& r) const;
  56. Rationnal operator/(Int n) const;
  57. friend Rationnal operator/(Int n,const Rationnal& r);
  58. //! Comparaison operator
  59. bool operator==(Int n) const;
  60. bool operator!=(Int n) const;
  61. bool operator>(Int n) const;
  62. bool operator<(Int n) const;
  63. };
  64. //-----------------
  65. // Other functions
  66. //-----------------
  67. ostream& operator<<(ostream& os,const Rationnal& r);
  68. Int gcd(Int a,Int b);
  69. //********************
  70. //* Inline functions *
  71. //********************
  72. inline
  73. Rationnal::Rationnal():num(0),den(1){
  74. }
  75. inline
  76. Rationnal::Rationnal(Int n):num(n),den(1){
  77. }
  78. inline
  79. Rationnal::Rationnal(Int n,Int d):num(n),den(d){
  80. normalize();
  81. }
  82. inline
  83. Rationnal::Rationnal(const Rationnal& r):num(r.num),den(r.den){
  84. }
  85. inline Rationnal&
  86. Rationnal::operator=(const Rationnal& r){
  87. num=r.num;
  88. den=r.den;
  89. return *this;
  90. }
  91. inline Int
  92. Rationnal::numerator() const{
  93. return num;
  94. }
  95. inline Int
  96. Rationnal::denominator() const{
  97. return den;
  98. }
  99. inline
  100. Rationnal::operator Reel() const{
  101. Reel n=num;
  102. Reel d=den;
  103. return n/d;
  104. }
  105. inline void
  106. Rationnal::normalize(){
  107. Int d=gcd((num<0)?-num:num,den);
  108. if(d==0){
  109. cout<<num<<" et "<<den<<endl;
  110. }
  111. num/=d;
  112. den/=d;
  113. }
  114. inline Rationnal
  115. Rationnal::operator+(const Rationnal& r) const{
  116. return Rationnal(num*r.den+r.num*den,den*r.den);
  117. }
  118. inline Rationnal
  119. Rationnal::operator+(Int n) const{
  120. return Rationnal(num+n*den,den);
  121. }
  122. inline Rationnal
  123. Rationnal::operator-(const Rationnal& r) const{
  124. return Rationnal(num*r.den-r.num*den,den*r.den);
  125. }
  126. inline Rationnal
  127. Rationnal::operator-(Int n) const{
  128. return Rationnal(num-n*den,den);
  129. }
  130. inline Rationnal
  131. Rationnal::operator*(const Rationnal& r) const{
  132. return Rationnal(num*r.num,den*r.den);
  133. }
  134. inline Rationnal
  135. Rationnal::operator*(Int n) const{
  136. return Rationnal(num*n,den);
  137. }
  138. inline Rationnal
  139. Rationnal::operator/(const Rationnal& r) const{
  140. Int s=(r.num<0)?-1:1;
  141. return Rationnal(s*num*r.den,s*den*r.num);
  142. }
  143. inline Rationnal
  144. Rationnal::operator/(Int n) const{
  145. Int s=(n<0)?-1:1;
  146. return Rationnal(s*num,s*den*n);
  147. }
  148. inline Rationnal
  149. operator+(Int n,const Rationnal& r){
  150. return Rationnal(n*r.den+r.num,r.den);
  151. }
  152. inline Rationnal
  153. operator-(Int n,const Rationnal& r){
  154. return Rationnal(-n*r.den+r.num,r.den);
  155. }
  156. inline Rationnal
  157. operator*(Int n,const Rationnal& r){
  158. return Rationnal(n*r.num,r.den);
  159. }
  160. inline Rationnal
  161. operator/(Int n,const Rationnal& r){
  162. Int s=(n<0)?-1:1;
  163. return Rationnal(s*n*r.den,s*r.num);
  164. }
  165. inline bool
  166. Rationnal::operator==(Int n) const{
  167. return num==n*den;
  168. }
  169. inline bool
  170. Rationnal::operator!=(Int n) const{
  171. return num!=n*den;
  172. }
  173. inline bool
  174. Rationnal::operator>(Int n) const{
  175. return num>n*den;
  176. }
  177. inline bool
  178. Rationnal::operator<(Int n) const{
  179. return num<n*den;
  180. }
  181. #endif