//===================================//
//  IO.Put : Simple Output Routines  //
//===================================//
//   Joseph Manning : 2000-Feb-12    //
//===================================//

package IO;

import java.io.*;

public class Put {

   //===========================================================================

   public static void Char( char c ) {

      // Output 'c' to 'System.out', with no surrounding spaces

      System.out.print( c ); }

   //===========================================================================

   public static void String( String s ) {

      // Output 's' to 'System.out', with no surrounding spaces

      String( s, 0 ); }

   //===========================================================================

   public static void String( String s, int w ) {

      // Output 's' to 'System.out', left-justified in minimum field-width 'w'

      System.out.print( s );
      for ( int k = 1 ; k <= w - s.length( ) ; k++ )
         System.out.print( ' ' ); }

   //===========================================================================

   public static void Bit( boolean b ) {

      // Output 'b' to 'System.out', as a bit, with no surrounding spaces

      System.out.print( b ? '1' : '0' ); }

   //===========================================================================

   public static void Int( int i ) {

      // Output 'i' to 'System.out', with no surrounding spaces

      Int( i, 0 ); }

   //===========================================================================

   public static void Int( int i, int w ) {

      // Output 'i' to 'System.out', right-justified in minimum field-width 'w'

      SpaceFill( i, w );
      System.out.print( i ); }

   //===========================================================================

   public static void Double( double d ) {

      // Output 'd' to 'System.out', with no surrounding spaces

      Double( d, 0, 0 ); }

   //===========================================================================

   public static void Double( double d, int wi, int wf ) {

      // Output 'd' to 'System.out', with its integer part right-justified
      // in minimum field-width 'wi', and its fractional part written either
      // with 'wf' digits, or to full precision if 'wf' <= 0

      int i    = (int) d;
      double f = ( d > i  ?  d - i  :  i - d );
      int k    = 0;

      if ( d > -1.0 && d < 0.0 ) {
         SpaceFill( i, wi - 1 );
         System.out.print( '-' ); }
      else
         SpaceFill( i, wi );

      System.out.print( i );

      System.out.print( '.' );

      do {
         f *= 10.0;
         i  = (int) f;
         System.out.print( i );
         f -= i;
         k++; }
         while ( wf > 0 && k < wf || wf <= 0 && f != 0.0 ); }

   //===========================================================================

   public static void LineEnd( ) {

      // Output end-of-line to 'System.out'

      System.out.println( ); }

   //===========================================================================

   private static void SpaceFill( int i, int w ) {

      // Output max( 'w' - width( 'i' ), 0 ) spaces to 'System.out'

      int a, m;

      a = ( i >= 0 ? i : -i );
      m = ( i >= 0 ? 0 : 1 );
      do {
         m++;
         a /= 10; }
         while ( a != 0 );
      for ( int k = 1 ; k <= w - m ; k++ )
         System.out.print( ' ' ); }

   //===========================================================================

}
