// // 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 (cant' 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; i1) // 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. }; } // main } // GiantTernaryOutput