Console.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Body of Console interface.
  3. // This file implements the code of the Console.cp file.
  4. // kjg November 1998.
  5. package CP.Console;
  6. public class Console
  7. {
  8. public static void WriteLn()
  9. {
  10. System.out.println();
  11. }
  12. public static void Write(char ch)
  13. {
  14. System.out.print(ch);
  15. }
  16. private static char[] strRep(int val)
  17. {
  18. if (val < 0) { // ==> must be minInt
  19. char[] min = {' ',' ','2','1','4','7','4','8','3','6','4','8'};
  20. return min;
  21. }
  22. char[] str = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  23. str[11] = (char) (val % 10 + (int) '0'); val = val / 10;
  24. for (int i = 10; val != 0; i--) {
  25. str[i] = (char) (val % 10 + (int) '0'); val = val / 10;
  26. }
  27. return str;
  28. }
  29. public static void WriteInt(int val, int fwd)
  30. {
  31. char[] str = (val >= 0 ? strRep(val) : strRep(-val));
  32. int blank;
  33. for (blank = 0; str[blank] == ' '; blank++)
  34. ;
  35. if (val < 0) {
  36. str[blank-1] = '-'; blank--;
  37. }
  38. // format ...
  39. // 01...............901
  40. // _________xxxxxxxxxxx
  41. // <-blank->< 12-blank>
  42. // <-----fwd------>
  43. if (fwd == 0) // magic case, put out exactly one blank
  44. System.out.print(new String(str, blank-1, 13-blank));
  45. else if (fwd < (12-blank))
  46. System.out.print(new String(str, blank, 12-blank));
  47. else if (fwd <= 12)
  48. System.out.print(new String(str, 12-fwd, fwd));
  49. else { // fwd > 12
  50. for (int i = fwd-12; i > 0; i--)
  51. System.out.print(" ");
  52. System.out.print(new String(str));
  53. }
  54. }
  55. public static void WriteHex(int val, int wid)
  56. {
  57. char[] str = new char[9];
  58. String jls;
  59. int j; // index of last blank
  60. int i = 8;
  61. do {
  62. int dig = val & 0xF;
  63. val = val >>> 4;
  64. if (dig >= 10)
  65. str[i] = (char) (dig + ((int) 'A' - 10));
  66. else
  67. str[i] = (char) (dig + (int) '0');
  68. i--;
  69. } while (val != 0);
  70. j = i;
  71. while (i >= 0) {
  72. str[i] = ' '; i--;
  73. }
  74. if (wid == 0) // special case, exactly one blank
  75. jls = new String(str, j, 9-j);
  76. else if (wid < (8-j))
  77. jls = new String(str, j+1, 8-j);
  78. else if (wid <= 9)
  79. jls = new String(str, 9-wid, wid);
  80. else {
  81. for (i = wid-9; i > 0; i--)
  82. System.out.print(" ");
  83. jls = new String(str);
  84. }
  85. System.out.print(jls);
  86. }
  87. public static void WriteString(char[] str)
  88. {
  89. int len = str.length;
  90. for (int i = 0; i < len && str[i] != '\0'; i++)
  91. System.out.print(str[i]);
  92. }
  93. } // end of public class Console