/**
* The hw2 class is a direct port of hw2.c to java.
* As you already know in java when you pass literal strings like
* <P>
* writeline("a literal string\n", stream);
* <P>
* in C is considered a char[], but in java it's automatically
* converted and treated as a String object. Therefore
* the function writeline accepts literal strings and
* String types. The getaline function returns a String type.
*/
import java.io.*; // System.in and System.out
import java.util.*; // Stack
class MyLibCharacter {
public MyLibCharacter (int ch) {
}
public char charValue () {
return character.charValue ();
}
return "" + character;
}
}
class Ideone {
private static final int ASCII_ZERO = 48;
private static final int CR = 13; // Carriage Return
private static final int MAXLENGTH = 80; // Max string length
private static final int EOF = -1; // process End Of File
private static final long COUNT = 16; // # of hex digits
private static final long DECIMAL = 10; // to indicate base 10
private static final long HEX = 16; // to indicate base 16
private static final char digits[] = // for ASCII conversion
new String("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").
toCharArray();
private static final String DEBUG_GETALINE
= "[*DEBUG: The length of the string just entered is ";
private static final String DIGIT_STRING
= "digit "; private static final String REENTER_NUMBER
="\nPlease reenter number: "; private static final String OUT_OF_RANGE
= " out of range!!!\n"; private static final String CAUSED_OVERFLOW
= " caused overflow!!!\n"; private static final String DEBUG_WRITELINE
= "\n[*DEBUG: The length of the string displayed is ";
private static Stack<MyLibCharacter> InStream =
new Stack<MyLibCharacter>();
private static boolean debug_on = false;
private static long hexCounter = 0; // counter for the number hex digits
/**
* Takes in a positive number and displays in a given base.
*
* @number Numeric value to be displayed.
* @base Base to used to display number.
* @stream Where to display, likely System.out or System.err.
*/
private static void baseout
(long number,
long base,
PrintStream stream
){ char[] result = new char[(int)COUNT]; //array to store number
hexCounter = 0; //reset hexCounter to be used
//inputs 0 and returns the method
if(number == 0)
{
fputc('0', stream);
return;
}
//stores the number into the result array
while (number > 0)
{
result[(int)hexCounter++] = digits[(int)number%(int)base];
number /= base;
}
//inputs 0 padding for HEX
if(base == HEX)
{
//loops through what is left in COUNT for HEX
for(long i = 0; i < (COUNT - hexCounter); i++)
{
fputc(digits[0], stream);
}
}
//prints out the array that stored the number
for(long i = hexCounter-1; i >= 0; i--)
{
fputc(result[(int)i], stream);
}
}
// YOUR HEADER FOR clrbuf GOES HERE
public static void clrbuf (int character) {
while (character != 10)
{
if (character == 10)
{
return;
}
}
/*for (int i = 0; i < message.length; i++)
{
if ( character == 10)
{
return;
}
}*/
}
// YOUR HEADER FOR decin GOES HERE
public static long decin() {
long number = 0;
return number;
}
/**
* Takes in a positive number and displays it in decimal.
*
* @number positive numeric value to be displayed
* @stream where to display, likely System.out or System.err
*/
public static void decout
(long number,
PrintStream stream
) { //calls baseout using DECIMAL as the base
baseout (number, DECIMAL, stream);
}
/*--------------------------------------------------------------------------
Function Name: digiterror
Purpose: This function handles erroneous user input.
Description: This function displays and error message to the user,
and asks for fresh input.
Input: character: The character that began the problem.
message: The message to display to the user.
Result: The message is displayed to the user.
--------------------------------------------------------------------------*/
public static void digiterror
(int character,
String message
) {
/* handle error */
clrbuf (character);
/* output error message */
writeline
(DIGIT_STRING,
System.
err); fputc
( (char)character,
System.
err); writeline
(message,
System.
err);
writeline
(REENTER_NUMBER,
System.
err); }
// YOUR HEADER FOR getaline GOES HERE
public static long getaline( char message[], int maxlength ) {
for(int i = 0; i < message.length; i++)
{
int character
= fgetc
(System.
in); message[i] = (char) character;
if (character == 10)
{
message[i] = '\0';
maxlength = i;
return maxlength;
}
else if (character == EOF)
{
return EOF;
}
else if ( i < MAXLENGTH )
{
maxlength = i;
}
else if ( i > MAXLENGTH )
{
clrbuf(message[i]);
}
maxlength = i;
}
return maxlength;
}
/**
* Takes in a positive number and displays it in hex.
*
* @number A positive numeric value to be displayed in hex.
* @stream Where to display, likely System.out or System.err.
*/
public static void hexout
(long number,
PrintStream stream
) {
// Output "0x" for hexidecimal.
writeline ("0x", stream);
baseout (number, HEX, stream);
}
/**
* Returns a character from the input stream.
*
* @return <code>char</code>
*/
char ToRet = '\0';
// Check our local input stream first.
// If it's empty read from System.in
if (InStream.isEmpty ()) {
try {
// Java likes giving the user the
// CR character too. Dumb, so just
// ignore it and read the next character
// which should be the '\n'.
ToRet = (char) stream.read ();
if (ToRet == CR)
ToRet = (char) stream.read ();
// check for EOF
if ((int) ToRet == 0xFFFF)
return EOF;
}
// Catch any errors in IO.
// Throw EOF back to caller to handle
return EOF;
}
writeline ("Unexpected IO Exception caught!\n",
writeline
(ioe.
toString (),
System.
out); }
}
// Else just pop it from the InStream.
else
ToRet = ((MyLibCharacter) InStream.pop ()).charValue ();
return ToRet;
}
/**
* Displays a single character.
*
* @param Character to display.
*/
public static void fputc
(char CharToDisp,
PrintStream stream
) {
// Print a single character.
stream.print (CharToDisp);
// Flush the system.out buffer, now.
stream.flush ();
}
/**
* Prints out a newline character.
* @stream Where to display, likely System.out or System.err.
*
*/
fputc('\n', stream); //prints out a new line
}
/**
* Prints out a string.
*
* @message A string to print out.
* @stream Where to display, likely System.out or System.err.
* @return <code>int</code> The length of the string.
*/
//makes the String into a Char array
char[] messageArray = message.toCharArray();
int i = 0;
debug_on = false;
//prints out each character from the char array
for(i = 0; i < messageArray.length; i++)
{
fputc(messageArray[i], stream);
/*if (message.equals("-x"))
{
debug_on = true;
}*/
}
long length = i;
/*if (message.equals("-x"))
{
debug_on = true;
}*/
/*if (debug_on = true)
{
System.err.println(DEBUG_WRITELINE + i);
}*/
return length; //returns the length of the string
}
/**
* Places back a character into the input stream buffer.
*
* @param A character to putback into the input buffer stream.
*/
public static void ungetc (int ToPutBack) {
// Push the char back on our local input stream buffer.
InStream.push (new MyLibCharacter (ToPutBack));
}
public static void main
( String[] args
) {
char buffer[] = new char[MAXLENGTH]; /* to hold string */
long number; /* to hold number entered */
long strlen; /* length of string */
long base; /* to hold base entered */
/* initialize debug states */
debug_on = false;
/* check command line options for debug display */
for (int index = 0; index < args.length; ++index) {
if (args[index].equals("-x"))
debug_on = true;
}
/* infinite loop until user enters ^D */
while (true) {
writeline
("\nPlease enter a string: ",
System.
out);
strlen = getaline (buffer, MAXLENGTH);
/* check for end of input */
if ( EOF == strlen )
break;
writeline
("The string is: ",
System.
out);
writeline
("\nIts length is ",
System.
out);
writeline
("\nPlease enter a decimal number: ",
System.
out); if ((number = decin ()) == EOF)
break;
writeline
("\nPlease enter a decimal base: ",
System.
out); if ((base = decin ()) == EOF)
break;
/* correct bases that are out of range */
if (base < 2)
base = 2;
else if (base > 36)
base = 36;
writeline
("Number entered in base ",
System.
out); writeline
(" is: ",
System.
out); baseout
(number, base,
System.
out);
writeline
("\nAnd in decimal is: ",
System.
out);
writeline
("\nAnd in hexidecimal is: ",
System.
out);
writeline
("\nNumber entered multiplied by 8 is: ",
System.
out); decout
(number
<< 3,
System.
out); writeline
("\nAnd in hexidecimal is: ",
System.
out); hexout
(number
<< 3,
System.
out); }
}
}