fork download
  1. /**
  2.  * The hw2 class is a direct port of hw2.c to java.
  3.  * As you already know in java when you pass literal strings like
  4.  * <P>
  5.  * writeline("a literal string\n", stream);
  6.  * <P>
  7.  * in C is considered a char[], but in java it's automatically
  8.  * converted and treated as a String object. Therefore
  9.  * the function writeline accepts literal strings and
  10.  * String types. The getaline function returns a String type.
  11.  */
  12.  
  13. import java.io.*; // System.in and System.out
  14. import java.util.*; // Stack
  15.  
  16. class MyLibCharacter {
  17. private Character character;
  18.  
  19. public MyLibCharacter (int ch) {
  20. character = new Character ( (char) ch );
  21. }
  22.  
  23. public char charValue () {
  24. return character.charValue ();
  25. }
  26.  
  27. public String toString () {
  28. return "" + character;
  29. }
  30. }
  31.  
  32. class Ideone {
  33. private static final int ASCII_ZERO = 48;
  34.  
  35. private static final int CR = 13; // Carriage Return
  36. private static final int MAXLENGTH = 80; // Max string length
  37.  
  38. private static final int EOF = -1; // process End Of File
  39.  
  40. private static final long COUNT = 16; // # of hex digits
  41.  
  42. private static final long DECIMAL = 10; // to indicate base 10
  43. private static final long HEX = 16; // to indicate base 16
  44.  
  45. private static final char digits[] = // for ASCII conversion
  46. new String("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
  47.  
  48. private static final String DEBUG_GETALINE =
  49. "[*DEBUG: The length of the string just entered is ";
  50.  
  51. private static final String DIGIT_STRING = "digit ";
  52. private static final String REENTER_NUMBER ="\nPlease reenter number: ";
  53. private static final String OUT_OF_RANGE = " out of range!!!\n";
  54. private static final String CAUSED_OVERFLOW = " caused overflow!!!\n";
  55. private static final String DEBUG_WRITELINE =
  56. "\n[*DEBUG: The length of the string displayed is ";
  57.  
  58. private static Stack<MyLibCharacter> InStream =
  59. new Stack<MyLibCharacter>();
  60.  
  61. private static boolean debug_on = false;
  62. private static long hexCounter = 0; // counter for the number hex digits
  63.  
  64. /**
  65.   * Takes in a positive number and displays in a given base.
  66.   *
  67.   * @number Numeric value to be displayed.
  68.   * @base Base to used to display number.
  69.   * @stream Where to display, likely System.out or System.err.
  70.   */
  71. private static void baseout (long number, long base, PrintStream stream){
  72. char[] result = new char[(int)COUNT]; //array to store number
  73. hexCounter = 0; //reset hexCounter to be used
  74.  
  75. //inputs 0 and returns the method
  76. if(number == 0)
  77. {
  78. fputc('0', stream);
  79. return;
  80. }
  81.  
  82. //stores the number into the result array
  83. while (number > 0)
  84. {
  85. result[(int)hexCounter++] = digits[(int)number%(int)base];
  86. number /= base;
  87. }
  88.  
  89. //inputs 0 padding for HEX
  90. if(base == HEX)
  91. {
  92. //loops through what is left in COUNT for HEX
  93. for(long i = 0; i < (COUNT - hexCounter); i++)
  94. {
  95. fputc(digits[0], stream);
  96. }
  97.  
  98. }
  99.  
  100. //prints out the array that stored the number
  101. for(long i = hexCounter-1; i >= 0; i--)
  102. {
  103. fputc(result[(int)i], stream);
  104. }
  105.  
  106. }
  107. // YOUR HEADER FOR clrbuf GOES HERE
  108. public static void clrbuf (int character) {
  109.  
  110. while (character != 10)
  111. {
  112. character = fgetc(System.in);
  113.  
  114. if (character == 10)
  115. {
  116. return;
  117. }
  118. }
  119.  
  120. /*for (int i = 0; i < message.length; i++)
  121.   {
  122.   if ( character == 10)
  123.   {
  124.   return;
  125.   }
  126.   }*/
  127.  
  128. }
  129.  
  130.  
  131. // YOUR HEADER FOR decin GOES HERE
  132. public static long decin() {
  133. long number = 0;
  134. return number;
  135. }
  136.  
  137.  
  138.  
  139. /**
  140.   * Takes in a positive number and displays it in decimal.
  141.   *
  142.   * @number positive numeric value to be displayed
  143.   * @stream where to display, likely System.out or System.err
  144.   */
  145. public static void decout (long number, PrintStream stream) {
  146. //calls baseout using DECIMAL as the base
  147. baseout (number, DECIMAL, stream);
  148. }
  149.  
  150.  
  151. /*--------------------------------------------------------------------------
  152.   Function Name: digiterror
  153.   Purpose: This function handles erroneous user input.
  154.   Description: This function displays and error message to the user,
  155.   and asks for fresh input.
  156.   Input: character: The character that began the problem.
  157.   message: The message to display to the user.
  158.   Result: The message is displayed to the user.
  159.  
  160.   --------------------------------------------------------------------------*/
  161. public static void digiterror (int character, String message) {
  162.  
  163. /* handle error */
  164. clrbuf (character);
  165.  
  166. /* output error message */
  167. writeline (DIGIT_STRING, System.err);
  168. fputc ( (char)character, System.err);
  169. writeline (message, System.err);
  170.  
  171. writeline (REENTER_NUMBER, System.err);
  172. }
  173.  
  174.  
  175. // YOUR HEADER FOR getaline GOES HERE
  176. public static long getaline( char message[], int maxlength ) {
  177. for(int i = 0; i < message.length; i++)
  178. {
  179. int character = fgetc(System.in);
  180. message[i] = (char) character;
  181.  
  182. if (character == 10)
  183. {
  184. message[i] = '\0';
  185. maxlength = i;
  186. return maxlength;
  187. }
  188.  
  189. else if (character == EOF)
  190. {
  191. return EOF;
  192. }
  193.  
  194. else if ( i < MAXLENGTH )
  195. {
  196. maxlength = i;
  197. }
  198.  
  199. else if ( i > MAXLENGTH )
  200. {
  201. clrbuf(message[i]);
  202. }
  203.  
  204. maxlength = i;
  205. }
  206. return maxlength;
  207. }
  208.  
  209. /**
  210.   * Takes in a positive number and displays it in hex.
  211.   *
  212.   * @number A positive numeric value to be displayed in hex.
  213.   * @stream Where to display, likely System.out or System.err.
  214.   */
  215. public static void hexout (long number, PrintStream stream) {
  216.  
  217. // Output "0x" for hexidecimal.
  218. writeline ("0x", stream);
  219. baseout (number, HEX, stream);
  220. }
  221.  
  222.  
  223. /**
  224.   * Returns a character from the input stream.
  225.   *
  226.   * @return <code>char</code>
  227.   */
  228. public static int fgetc(InputStream stream) {
  229.  
  230. char ToRet = '\0';
  231.  
  232. // Check our local input stream first.
  233. // If it's empty read from System.in
  234. if (InStream.isEmpty ()) {
  235.  
  236. try {
  237. // Java likes giving the user the
  238. // CR character too. Dumb, so just
  239. // ignore it and read the next character
  240. // which should be the '\n'.
  241. ToRet = (char) stream.read ();
  242. if (ToRet == CR)
  243. ToRet = (char) stream.read ();
  244.  
  245. // check for EOF
  246. if ((int) ToRet == 0xFFFF)
  247. return EOF;
  248. }
  249.  
  250. // Catch any errors in IO.
  251. catch (EOFException eof) {
  252.  
  253. // Throw EOF back to caller to handle
  254. return EOF;
  255. }
  256.  
  257. catch (IOException ioe) {
  258. writeline ("Unexpected IO Exception caught!\n",
  259. System.out);
  260. writeline (ioe.toString (), System.out);
  261. }
  262.  
  263. }
  264.  
  265. // Else just pop it from the InStream.
  266. else
  267. ToRet = ((MyLibCharacter) InStream.pop ()).charValue ();
  268. return ToRet;
  269. }
  270.  
  271.  
  272. /**
  273.   * Displays a single character.
  274.   *
  275.   * @param Character to display.
  276.   */
  277. public static void fputc(char CharToDisp, PrintStream stream) {
  278.  
  279. // Print a single character.
  280. stream.print (CharToDisp);
  281.  
  282. // Flush the system.out buffer, now.
  283. stream.flush ();
  284. }
  285.  
  286. /**
  287.   * Prints out a newline character.
  288.   * @stream Where to display, likely System.out or System.err.
  289.   *
  290.   */
  291. public static void newline ( PrintStream stream ) {
  292. fputc('\n', stream); //prints out a new line
  293. }
  294.  
  295. /**
  296.   * Prints out a string.
  297.   *
  298.   * @message A string to print out.
  299.   * @stream Where to display, likely System.out or System.err.
  300.   * @return <code>int</code> The length of the string.
  301.   */
  302. public static long writeline (String message, PrintStream stream) {
  303. //makes the String into a Char array
  304. char[] messageArray = message.toCharArray();
  305. int i = 0;
  306. debug_on = false;
  307.  
  308. //prints out each character from the char array
  309. for(i = 0; i < messageArray.length; i++)
  310. {
  311. fputc(messageArray[i], stream);
  312.  
  313. /*if (message.equals("-x"))
  314.   {
  315.   debug_on = true;
  316.   }*/
  317. }
  318.  
  319. long length = i;
  320.  
  321. /*if (message.equals("-x"))
  322.   {
  323.   debug_on = true;
  324.   }*/
  325.  
  326. /*if (debug_on = true)
  327.   {
  328.   System.err.println(DEBUG_WRITELINE + i);
  329.   }*/
  330.  
  331. System.out.println(length);
  332.  
  333. return length; //returns the length of the string
  334. }
  335.  
  336.  
  337.  
  338. /**
  339.   * Places back a character into the input stream buffer.
  340.   *
  341.   * @param A character to putback into the input buffer stream.
  342.   */
  343. public static void ungetc (int ToPutBack) {
  344.  
  345. // Push the char back on our local input stream buffer.
  346. InStream.push (new MyLibCharacter (ToPutBack));
  347. }
  348.  
  349.  
  350. public static void main( String[] args ) {
  351.  
  352. char buffer[] = new char[MAXLENGTH]; /* to hold string */
  353.  
  354. long number; /* to hold number entered */
  355. long strlen; /* length of string */
  356. long base; /* to hold base entered */
  357.  
  358. /* initialize debug states */
  359. debug_on = false;
  360.  
  361. /* check command line options for debug display */
  362. for (int index = 0; index < args.length; ++index) {
  363. if (args[index].equals("-x"))
  364. debug_on = true;
  365. }
  366.  
  367. /* infinite loop until user enters ^D */
  368. while (true) {
  369. writeline ("\nPlease enter a string: ", System.out);
  370.  
  371. strlen = getaline (buffer, MAXLENGTH);
  372. newline (System.out);
  373.  
  374. /* check for end of input */
  375. if ( EOF == strlen )
  376. break;
  377.  
  378. writeline ("The string is: ", System.out);
  379. writeline ( new String(buffer), System.out);
  380.  
  381. writeline ("\nIts length is ", System.out);
  382. decout (strlen, System.out);
  383. newline (System.out);
  384.  
  385. writeline ("\nPlease enter a decimal number: ", System.out);
  386. if ((number = decin ()) == EOF)
  387. break;
  388.  
  389. writeline ("\nPlease enter a decimal base: ", System.out);
  390. if ((base = decin ()) == EOF)
  391. break;
  392.  
  393. /* correct bases that are out of range */
  394. if (base < 2)
  395. base = 2;
  396. else if (base > 36)
  397. base = 36;
  398.  
  399. newline (System.out);
  400.  
  401. writeline ("Number entered in base ", System.out);
  402. decout (base, System.out);
  403. writeline (" is: ", System.out);
  404. baseout (number, base, System.out);
  405.  
  406. writeline ("\nAnd in decimal is: ", System.out);
  407. decout (number, System.out);
  408.  
  409. writeline ("\nAnd in hexidecimal is: ", System.out);
  410. hexout (number, System.out);
  411.  
  412. writeline ("\nNumber entered multiplied by 8 is: ", System.out);
  413. decout (number << 3, System.out);
  414. writeline ("\nAnd in hexidecimal is: ", System.out);
  415. hexout (number << 3, System.out);
  416. newline (System.out);
  417. }
  418. }
  419. }
  420.  
Success #stdin #stdout 0.09s 46884KB
stdin
Standard input is empty
stdout
Please enter a string:  25