// // COMP102 // Example 5: Printing a sequence of GIANT digits // (Alternative version of Example 4, using int[] // rather than String). // // Paul E. Dunne 13/10/99 // import java.io.*; import GIANT_TERNARY; // Ternary Digit Class // public class AltGiantTernaryOut { // Instantiate Input streams public static InputStreamReader input = new InputStreamReader(System.in); public static BufferedReader keyboardInput = new BufferedReader(input); // static GIANT_TERNARY NumberForm=new GIANT_TERNARY(); static char fore; // For user-defined foreground symbol static char back; // For user-defined background symbol static int InputValue; // User supplied integer to be printed in ternary // using GIANT_TERNARY representation. static int[] TernaryValue; // This will contain the ternary value of InputValue // as an int[] of {0,1,2} symbols. static int starting_point; //*************************************************************** // Read user-defined foreground and background * // symbols. * //*************************************************************** public static void Get_fore_and_back() throws IOException { String input_line; System.out.print("Foreground Character (hit return for default value):"); input_line = keyboardInput.readLine(); if (input_line.length()==0) // Empty String read: use default. { fore='*'; } else { fore = input_line.charAt(0); // otherwise take first character. }; System.out.print("Background Character (hit return for default value):"); input_line = keyboardInput.readLine(); if (input_line.length()==0) // Empty String read: use default. { back=' '; } else { back = input_line.charAt(0); // otherwise take first character. }; } // Get_fore_and_back //************************************************************* // Convert integer to ternary representation returning int[] * // of the encoding digits as the result. * //************************************************************* public static int[] IntegerToTernary( final int n ) { int[] res; // Result array (can't constrain this yet) int tn = n; // Save value of input parameter. int next_digit; // The next ternary digit to be stored in res. if (tn==0) // Have to convert 0 immediately. { // otherwise, taking log(0) in the 'else' res = new int[1]; // clause will cause a run-time error. res[0] =0; } else { res = new int[1+(int)(Math.ceil(Math.log(n)/Math.log(3)))]; // The most that will be needed for (int i=0; i< res.length; i++) { next_digit = tn%3; // Find the least significant ternary digit left; tn=tn/3; // and `shift' this off the current value of tn. res[i] = next_digit; // Store it. }; }; return res; } // IntegerToTernary // // Main Method // public static void main( String[] args ) throws IOException { Get_fore_and_back(); NumberForm.ChangeOver(fore,back); // Modify the instantiation to // use the new foreground/background // symbols. System.out.print("Enter non-negative integer value:"); InputValue = new Integer(keyboardInput.readLine()).intValue(); TernaryValue = IntegerToTernary(InputValue); // This conversion may leave extraneous leading 0s which // we don't want to print. Following code determines at which // position printing should begin. starting_point = TernaryValue.length; // N.B. 0..length-1 is `reverse' of printing order // and so leading 0s, if present, start from length-1. if (TernaryValue.length> 1) // Make sure that if InputValue=0 then while (TernaryValue[starting_point-1]==0) // this *does not* get changed. starting_point = starting_point-1; // // Output Stage // // Proceeding from left-to-right output // the current (i'th) row of the GIANT_TERNARY digit // corresponding to each digit in the ternary representation // of the integer InputValue // System.out.println(); // Start output on a new line for (int i=0; i< 9; i++) { for (int j=starting_point; j> 0; j--) { NumberForm.PrintRow(i,TernaryValue[j-1]); }; System.out.println(); // Throw a new line after one row completed. }; } } |
Java Implementation of GIANT_TERNARY Printing Using int[] to Store Conversion.