1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <cstdio>
template<typename T>
void display( T xmin, T xmax, int i, int n, const char *fmt, T f( T), T g( T ) = nullptr )
{
T dx = ( xmax - xmin ) / ( n - 1 );
if (i < n)
{
T x = xmin + i * dx;
if ( g ) printf( fmt, x, f(x), g(x) );
else printf( fmt, x, f(x) );
display<T>( xmin, xmax, i+1, n, fmt, f, g );
}
}
int main()
{
printf( "%6s\t%6s\n", "x", "-3x+2" );
display<double>( -5, 5, 0, 11, "%6.1f\t%6.1f\n", []( double x ){ return -3 * x + 2; } );
printf( "\n%6s\t%6s\t%6s\n", "x", "x^2", "x^3" );
display<int>( 0, 20, 0, 21, "%6d\t%6d\t%6d\n", []( int x ){ return x * x; }, []( int x ){ return x * x * x; } );
}
| |