fork download
  1. ///////////////////// fraction.h //////////////////////////////
  2.  
  3. // include guard see: http://e...content-available-to-author-only...a.org/wiki/Include_guard
  4. #ifndef FRACTION_H_INCLUDED_
  5. #define FRACTION_H_INCLUDED_
  6.  
  7. #include <iosfwd> // forward declaration of std::ostream
  8.  
  9. struct fraction
  10. {
  11. fraction( int numerator = 1, int denominator = 1 ) ;
  12. fraction( double v ) ;
  13.  
  14. static const fraction zero ;
  15. static const fraction one ;
  16. static int precision() ;
  17.  
  18. // ...
  19.  
  20. private:
  21. int num ;
  22. int den ;
  23. void normalize() ;
  24. friend std::ostream& operator<< ( std::ostream& stm, const fraction& f ) ;
  25. };
  26.  
  27. #endif // FRACTION_H_INCLUDED_
  28.  
  29. ///////////////////// fraction.cpp //////////////////////////////
  30.  
  31. //#include "fraction.h"
  32. #include <iostream>
  33.  
  34. const fraction fraction::zero(0,1) ;
  35. const fraction fraction::one(1,1) ;
  36. int fraction::precision() { return 1000000 ; }
  37.  
  38. static unsigned int hcf( unsigned int a, unsigned int b )
  39. {
  40. if( b == 0 ) return a ;
  41. else return hcf( b, a%b ) ;
  42. }
  43.  
  44. static int make_pos( int a ) { return a<0 ? -a : +a ; }
  45.  
  46. void fraction::normalize()
  47. {
  48. bool positive = ( num >= 0 ) == ( den >= 0 ) ;
  49. num = make_pos(num) ;
  50. den = make_pos(den) ;
  51. int h = hcf(num,den) ;
  52. if( h == 0 ) h = 1 ;
  53. num /= h ;
  54. den /= h ;
  55. if( !positive ) num = -num ;
  56. }
  57.  
  58. fraction::fraction( int numerator, int denominator )
  59. : num(numerator), den(denominator) { normalize() ; }
  60.  
  61. fraction::fraction( double d ) : den( precision() )
  62. { num = int( d * precision() ) ; normalize() ; }
  63.  
  64. std::ostream& operator<< ( std::ostream& stm, const fraction& f )
  65. {
  66. if( f.num < 0 ) stm << '-' ;
  67. return stm << '(' << make_pos(f.num) << '/' << f.den << ')' ;
  68. }
  69.  
  70. ///////////////////// fraction.cpp //////////////////////////////
  71.  
  72. //#include "fraction.h"
  73. #include <iostream>
  74.  
  75. int main()
  76. {
  77. fraction a( 24, 36 ) ; std::cout << a << '\n' ; // (2/3)
  78. fraction b( -24, -36 ) ; std::cout << b << '\n' ; // (2/3)
  79. fraction c( 24, -36 ) ; std::cout << c << '\n' ; // -(2/3)
  80. fraction d( -24, 36 ) ; std::cout << d << '\n' ; // -(2/3)
  81. fraction e( -24, -36 ) ; std::cout << e << '\n' ; // (2/3)
  82.  
  83. fraction f( 0, -36 ) ; std::cout << f << '\n' ; // (0/1)
  84. fraction g( 36, 0 ) ; std::cout << g << '\n' ; // (1/0)
  85. fraction h( 0, 0 ) ; std::cout << h << '\n' ; // (0/0)
  86.  
  87. fraction i( 1.25 ) ; std::cout << i << '\n' ; // (5/4)
  88. fraction j( -1.25 ) ; std::cout << j << '\n' ; // -(5/4)
  89. fraction k( 1.252868 ) ; std::cout << k << '\n' ; // (313217/250000)
  90. fraction l( 1.2500009999 ) ; std::cout << l << '\n' ; // (5/4)
  91. }
  92.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
(2/3)
(2/3)
-(2/3)
-(2/3)
(2/3)
(0/1)
(1/0)
(0/0)
(5/4)
-(5/4)
(313217/250000)
(5/4)