DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fraction.cpp
1 
14 
15 
16 #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 Fraction::ConstIterator ConstIterator; // the iterator type for visiting quotients
49  typedef Fraction::Value Value; // the value of the iterator, a pair (quotient,depth).
50  typedef std::back_insert_iterator< Fraction > OutputIterator;
52 
54  Fraction f;
55  OutputIterator itback = std::back_inserter( f );
56  for ( Quotient i = 1; i < argc; ++i)
57  {
58  Quotient u = atoll( argv[ i ] );
59  *itback++ = std::make_pair( u, i-1 );
60  }
61  std::cout << "z = " << f.p() << " / " << f.q() << std::endl;
63  return 0;
64 }
65