//----------------------------------------------------------------------
//
// Time-stamp: < 05/02/01 23:18 Kenneth L Moore>
//
// :File: BinOp.cpp
// :Purpose: Clarify the
discussion in section 1.4.1
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// includes
#include <iostream.h>
#include <stdlib.h>
//----------------------------------------------------------------------
// implement recursive gcd
algorithm:
// see
http://www.mike95.com/c_plusplus/tutorial/algorithms/Euclid.asp
int gcd_implement( int num1,
int num2 )
{
int remainder = num2 % num1;
if ( remainder != 0 )
return gcd_implement(
remainder,num1 );
return num1;
}
// typedefs, globals
struct GCD {
typedef int res_type;
typedef int arg1_type;
typedef int arg2_type;
int operator()(int x, int
y) const {
return gcd_implement( x, y );
}
};
//----------------------------------------------------------------------
// function declarations
template <class BinOp>
struct BindFirst {
BinOp op;
typename BinOp::arg1_type
value;
// constructor
BindFirst(BinOp const& f, typename BinOp::arg1_type
const& v):
op(f), value(v) {cout
<< "in BindFirst" << " value " << value
<<
" op intialized " << endl;}
typename BinOp::res_type
operator()(typename BinOp::arg2_type const& x) const {
cout << "in BinOP calling op with value "
<< value << " x " << x << endl;
return op(value, x);
}
};
int main()
{
GCD gcd;
BindFirst<GCD>
gcd10(gcd, 10); // instantiate gcd10 using a gcd struct and 10
int n;
// ask user for a number to compare to 10
cout << " input a positive integer - neg to stop
" << endl;
cin >> n;
while (n > 0){
cout <<" the greatest common denominator of 10 and
" << n << " is " << gcd10(n)<< endl;
cout << " input a positive integer - neg to stop
" << endl;
cin >> n;
}
return 0;
}
// eof