home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 7.5 KB | 254 lines |
-
- package simula;
-
- import java.io.*;
-
- public final class Lang {
-
-
- /**
- * Returns the greatest integer BELOW the parameter.
- # Ex: 4.1 -> 4 -3.2 -> -4
- * @param _num The number to floor off
- * @return the greatest integer BELOW the parameter.
- */
- public static int entier(double _num) {
- return (int) java.lang.Math.floor(_num);
- }
- // ------------------------------ METHODS ------------------------------
- /**
- * Returns an integer number retrieved from the input.
- * @return an integer number retrieved from the input.
- * @exception SimulaException Error while reading from input.
- */
- public static int inint() throws SimulaException {
- BufferedReader Input = new BufferedReader(new InputStreamReader(System.in));
- String _string;
- int _int;
-
- try {
- _string = Input.readLine();
- } catch (IOException err) {
- throw new SimulaException(SimulaException.ABNORMAL_INPUT);
- }
- try {
- _int = Integer.parseInt(_string);
- } catch (NumberFormatException err) {
- throw new SimulaException(SimulaException.ABNORMAL_INPUT);
- }
-
- return _int ;
- }
- /**
- * Returns a real number retrieved from the input.
- * @return a real number retrieved from the input.
- * @exception SimulaException Error while reading from input.
- */
- public static double inreal() throws SimulaException {
- BufferedReader Input = new BufferedReader(new InputStreamReader(System.in));
- String _string;
- Double _double;
-
- try {
- _string = Input.readLine();
- } catch (IOException err) {
- throw new SimulaException(SimulaException.ABNORMAL_INPUT);
- }
- try {
- _double = new Double(_string);
- } catch (NumberFormatException err) {
- throw new SimulaException(SimulaException.ABNORMAL_INPUT);
- }
-
- return _double.doubleValue();
- }
- /**
- * Returns <code>True</code> if there are other items in the input buffer.
- * @return <code>True</code> if there are other items in the input buffer.
- * @exception java.io.IOException Error on the input.
- */
- public static boolean lastitem() throws java.io.IOException {
- BufferedReader Input = new BufferedReader(new InputStreamReader(System.in));
- return !Input.ready();
- }
- /**
- * Returns the integer division of the two parameters.
- * @param div The number to be divided.
- * @param qt The dividing number.
- * @return the integer division of the two parameters.
- */
- public static int mod(int div, int qt) {
- return (div % qt);
- }
- /**
- * Prints a floating point number on the screen with a number of fractional digits
- * up to the given amount and aligned to the right in the given field.
- *
- * @param _out Number to print.
- * @param _decimal Number of fractional digits.
- * @param _fieldLen Size of the field.
- * @exception SimulaException Error while printing on screen or wrong combination of parameters.
- */
- public static void outfix(double _out, int _decimal, int _fieldLen) throws SimulaException {
-
- if (_decimal >= _fieldLen) throw new SimulaException(SimulaException.SENSELESS_PARAMETERS);
-
- String tmpStr = String.valueOf(_out);
- String outStr = "";
- int len = tmpStr.length();
- int i;
- int intCount;
-
- // Count the digits of the integer part plus the full stop, if it exists
- intCount = tmpStr.indexOf('.') + 1; // .indexOf returns -1 if '.' doesn't exist
- if (intCount == 0) intCount = len;
-
- // if fractional digits are more than requested reduce the number of digits that must be printed
- if (len - intCount > _decimal) len = intCount + _decimal;
-
- if (len > _fieldLen) {
- // The field is too short to contain the number: display a dummy number and fire an Exception
- tmpStr = "";
- for(i = 0; i < _fieldLen; i++) {
- tmpStr = tmpStr + "*";
- }
- System.out.println(tmpStr);
- throw new SimulaException(SimulaException.FIELD_TOO_SHORT);
- } else {
- // Take the digits to be printed and fill the remaining portion of the field with spaces left aligned
- outStr = tmpStr.substring(0, len);
- tmpStr = "";
- for( i = 0; i < _fieldLen - len; i++) {
- tmpStr = tmpStr + " ";
- }
- System.out.println(tmpStr + outStr);
- }
- }
- /**
- * Flushes the screen.
- */
- public static void outimage() {
- System.out.flush();
- }
- /**
- * Prints an integer number on the screen and given the field size
- * aligns the number on the right, preceding it with spaces enough to
- * fill the field.
- *
- * @param _out Number to print.
- * @param _fieldLen Size of the field.
- * @exception SimulaException Error while printing on screen.
- */
- public static void outint(int _out, int _fieldLen) throws SimulaException {
- int len = (String.valueOf(_out)).length();
- String tmpStr = "";
- int i;
-
- if (len > _fieldLen) {
- for( i = 1; i <= _fieldLen; i++) {
- tmpStr = tmpStr + "*";
- }
- System.out.println(tmpStr);
- throw new SimulaException(SimulaException.FIELD_TOO_SHORT);
- } else {
- for( i = 1; i <= _fieldLen - len; i++) {
- tmpStr = tmpStr + " ";
- }
- System.out.println(tmpStr + String.valueOf(_out));
- }
- }
- /**
- * Prints out a number in scientific format
- * @param value double number to print out
- * @param dec int number of digits to use in fractional part
- * @param len int lenght of field to print into
- * @exception SimulaException Wrong combination of parameters.
- */
- public static void outreal(double value, int dec, int _fieldLen ) throws SimulaException {
-
- String intPart = "0" ;
- String decPart = "" ;
- double absVal = java.lang.Math.abs(value) ;
- String tmp= new String("" + absVal) ;
- String s ;
- String exp = "" ;
- String total = "" ;
- int firstDigit = 0 ;
- int i=0 ;
- i=0 ;
- if (absVal!=0){
- s=String.valueOf(tmp.charAt(i)) ;
- while (((s.compareTo(".")==0)) || ((s.compareTo("0")==0)))
- s=String.valueOf(tmp.charAt(++i)) ;
- intPart=String.valueOf(tmp.charAt(i)) ;
- firstDigit=i ;
- i++ ;
- while (i<tmp.length()){
- s=String.valueOf(tmp.charAt(i++)) ;
- if (s.compareTo(".") !=0)
- decPart=decPart+s ;
- }
-
- }
- else{
- intPart="0" ;
- decPart="" ;
- }
-
-
- if (absVal>=1) {
- exp = "" + (tmp.indexOf(".")-1) ;
- if (exp.length() == 1)
- exp = "0" + exp ;
- exp = "e+" + exp ;
- } else {
- exp=""+(firstDigit-tmp.indexOf(".")) ;
- if (exp.length()==1)
- exp="0"+exp ;
- exp="e-"+exp ;
- }
-
- total=intPart+"." ;
-
- for (i=1;i<=dec;i++)
- if (i<=decPart.length())
- total=total+String.valueOf(decPart.charAt(i-1)) ;
- else
- total=total+"0" ;
- total=total+exp ;
- if (value<0)
- total="-"+total ;
-
-
- if (total.length() > _fieldLen) {
- total="" ;
- for(i = 1; i <= _fieldLen; i++) {
- total = total + "*";
- }
- System.out.println(total) ;
- throw new SimulaException(SimulaException.FIELD_TOO_SHORT);
-
- } else {
- System.out.println(total) ;
- return ;
- }
-
- }
- /**
- * Prints a string on the screen.
- */
- public static void outtext(String _text) {
- System.out.println(_text);
- }
- /**
- * Returns the sign of the given real number.
- * @return the sign of the given real number.
- */
- public static int sign(double _real) {
- if ((_real == 0) || (Double.isNaN(_real))) {
- return 1;
- } else {
- return (int)(_real/Math.abs(_real));
- }
- }
- }