DGtal  0.9.3beta
fraction.cpp
1 
14 #include <iostream>
17 #include "DGtal/arithmetic/LighterSternBrocot.h"
19 
21 
22 using namespace DGtal;
23 
25 
26 void usage( int, char** argv )
27 {
28  std::cerr << "Usage: " << argv[ 0 ] << " <u_0> <u_1> ... <u_k>" << std::endl;
29  std::cerr << "\t - computes the fraction [u_0; u_1, ..., u_k] from its partial quotients." << std::endl;
30 }
31 
35 int main( int argc, char** argv )
36 {
37  if ( argc < 3 )
38  {
39  usage( argc, argv );
40  return 1;
41  }
42 
44  typedef DGtal::int64_t Integer;
45  typedef DGtal::int64_t Quotient;
46  typedef LighterSternBrocot<Integer, Quotient, StdMapRebinder> SB; // the type of the Stern-Brocot tree
47  typedef SB::Fraction Fraction; // the type for fractions
48  typedef std::back_insert_iterator< Fraction > OutputIterator;
50 
52  Fraction f;
53  OutputIterator itback = std::back_inserter( f );
54  for ( Quotient i = 1; i < argc; ++i)
55  {
56  Quotient u = atoll( argv[ i ] );
57  *itback++ = std::make_pair( u, i-1 );
58  }
59  std::cout << "z = " << f.p() << " / " << f.q() << std::endl;
61  return 0;
62 }
63 
DGtal::int32_t Integer
Definition: StdDefs.h:74
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74