/** table.cpp: print out information about basic types
	in a formatted table.
	Use climits and symbolic constants.
	Ulrik Schroeder, 26.12.2002
*/

#include <iostream>  // cout, <<
#include <iomanip>   // setw( ), fill( )
#include <climits>   // type information (constants for int types)
#include <cfloat>    // float information (constants)
#include <string>    // string( char, size )
using namespace std;

// prototype declarations
void printLine( char ); // auxiliary funtion to print separator line

// global constants for the width of specific columns
const int wType = 14, wByte = 5, wValu = 16;

int main( ) {
	// Table header
	printLine( '=' ); // ==============================
	cout << setw( wType ) << "TYPE" << " | "
		 << setw( wByte ) << "BYTE" << " | "
		 << setw( wValu ) << "MIN"  << " | "
		 << setw( wValu ) << "MAX"  << endl;
	printLine( '=' ); // ==============================

	// --------------------------------------------- Bool
	cout << setw( wType ) << "bool"         << " | "
		 << setw( wByte ) << sizeof( bool ) << " | "
		 << setw( wValu ) << "false ( 0 )"  << " | "
		 << setw( wValu ) << "true  ( 1 )"  << endl;
	printLine( '-' ); // ---------------------------

	// --------------------------------------------- Char
	cout << setw( wType ) << "char"         << " | "
		 << setw( wByte ) << sizeof( char ) << " | "
		 << setw( wValu ) << CHAR_MIN       << " | "
		 << setw( wValu ) << CHAR_MAX       << endl;
	cout << setw( wType ) << "unsigned char"         << " | "
		 << setw( wByte ) << sizeof( unsigned char ) << " | "
		 << setw( wValu ) << 0                       << " | "
		 << setw( wValu ) << UCHAR_MAX               << endl;
	cout << setw( wType ) << "signed char"         << " | "
		 << setw( wByte ) << sizeof( signed char ) << " | "
		 << setw( wValu ) << SCHAR_MIN             << " | "
		 << setw( wValu ) << SCHAR_MAX             << endl;
	printLine( '-' ); // ---------------------------

	// --------------------------------------------- Int
	cout << setw( wType ) << "int"         << " | "
		 << setw( wByte ) << sizeof( int ) << " | "
		 << setw( wValu ) << INT_MIN       << " | "
		 << setw( wValu ) << INT_MAX       << endl;
	cout << setw( wType ) << "unsigned int"         << " | "
		 << setw( wByte ) << sizeof( unsigned int ) << " | "
		 << setw( wValu ) << 0                      << " | "
		 << setw( wValu ) << UINT_MAX               << endl;
	printLine( '.' ); // ...........................
	cout << setw( wType ) << "short"         << " | "
		 << setw( wByte ) << sizeof( short ) << " | "
		 << setw( wValu ) << SHRT_MIN        << " | "
		 << setw( wValu ) << SHRT_MAX        << endl;
	cout << setw( wType ) << "unsigned short"         << " | "
		 << setw( wByte ) << sizeof( unsigned short ) << " | "
		 << setw( wValu ) << 0                        << " | "
		 << setw( wValu ) << USHRT_MAX                << endl;
	printLine( '.' ); // ...........................
	cout << setw( wType ) << "long"         << " | "
		 << setw( wByte ) << sizeof( long ) << " | "
		 << setw( wValu ) << LONG_MIN       << " | "
		 << setw( wValu ) << LONG_MAX       << endl;
	cout << setw( wType ) << "unsigned long"         << " | "
		 << setw( wByte ) << sizeof( unsigned long ) << " | "
		 << setw( wValu ) << 0                       << " | "
		 << setw( wValu ) << ULONG_MAX               << endl;
	printLine( '-' ); // ---------------------------

	// --------------------------------------------- float
	cout << setw( wType ) << "float"         << " | "
		 << setw( wByte ) << sizeof( float ) << " | "
		 << setw( wValu ) << FLT_MIN         << " | "
		 << setw( wValu ) << FLT_MAX         << endl;
	cout << setw( wType ) << " "             << " | "
		 << setw( wByte ) << " "             << " | "
		 << setw( wValu ) << "precision"     << " | "
		 << setw( wValu ) << FLT_EPSILON     << endl;
	printLine( '.' ); // ...........................
	cout << setw( wType ) << "double"         << " | "
		 << setw( wByte ) << sizeof( double ) << " | "
		 << setw( wValu ) << DBL_MIN          << " | "
		 << setw( wValu ) << DBL_MAX << endl;
	cout << setw( wType ) << " "             << " | "
		 << setw( wByte ) << " "             << " | "
		 << setw( wValu ) << "precision"     << " | "
		 << setw( wValu ) << DBL_EPSILON     << endl;
	printLine( '.' ); // ...........................
	cout << setw( wType ) << "long double"         << " | "
		 << setw( wByte ) << sizeof( long double ) << " | "
		 << setw( wValu ) << LDBL_MIN              << " | "
		 << setw( wValu ) << LDBL_MAX << endl;
	cout << setw( wType ) << " "             << " | "
		 << setw( wByte ) << " "             << " | "
		 << setw( wValu ) << "precision"     << " | "
		 << setw( wValu ) << LDBL_EPSILON    << endl;
	printLine( '=' ); // ===========================
	return 0;
} // main()

/** Print separator line in a table with 4 columns defined by
    column width for Type, Size and Min/Max information.
*/
void printLine( char c ) {
	cout << string( wType+1, c ) << "|" << string( 1, c )
		 << string( wByte+1, c ) << "|" << string( 1, c )
		 << string( wValu+1, c ) << "|" << string( 1, c )
		 << string( wValu+1, c ) << endl;
} // printLine

/*
===============|=======|==================|==================
          TYPE |  BYTE |              MIN |              MAX
===============|=======|==================|==================
          bool |     1 |      false ( 0 ) |      true  ( 1 )
---------------|-------|------------------|------------------
          char |     1 |             -128 |              127
 unsigned char |     1 |                0 |              255
   signed char |     1 |             -128 |              127
---------------|-------|------------------|------------------
           int |     4 |      -2147483648 |       2147483647
  unsigned int |     4 |                0 |       4294967295
...............|.......|..................|..................
         short |     2 |           -32768 |            32767
unsigned short |     2 |                0 |            65535
...............|.......|..................|..................
          long |     4 |      -2147483648 |       2147483647
 unsigned long |     4 |                0 |       4294967295
---------------|-------|------------------|------------------
         float |     4 |      1.17549e-38 |      3.40282e+38
               |       |        precision |      1.19209e-07
...............|.......|..................|..................
        double |     8 |     2.22507e-308 |     1.79769e+308
               |       |        precision |      2.22045e-16
...............|.......|..................|..................
   long double |    12 |     2.22507e-308 |     1.79769e+308
               |       |        precision |      2.22045e-16
===============|=======|==================|==================
*/

