home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / simula / Lang.java < prev    next >
Encoding:
Java Source  |  1998-02-20  |  7.5 KB  |  254 lines

  1.  
  2. package simula;
  3.  
  4. import java.io.*;
  5.  
  6. public final class Lang {
  7.  
  8.  
  9.         /**
  10.          * Returns the greatest integer BELOW the parameter.
  11.          # Ex: 4.1 -> 4        -3.2 -> -4
  12.          * @param _num The number to floor off
  13.          * @return the greatest integer BELOW the parameter.
  14.          */
  15.         public static int entier(double _num) {
  16.                 return (int) java.lang.Math.floor(_num);
  17.         }
  18.         // ------------------------------ METHODS ------------------------------
  19.         /**
  20.          * Returns an integer number retrieved from the input.
  21.          * @return an integer number retrieved from the input.
  22.          * @exception SimulaException Error while reading from input.
  23.          */
  24.         public static int inint() throws SimulaException {
  25.                 BufferedReader Input = new BufferedReader(new InputStreamReader(System.in));
  26.                 String _string;
  27.                 int _int;
  28.  
  29.                 try {
  30.                         _string = Input.readLine();
  31.                 } catch (IOException err) {
  32.                         throw new SimulaException(SimulaException.ABNORMAL_INPUT);
  33.                 }
  34.                 try {
  35.                         _int = Integer.parseInt(_string);
  36.                 } catch (NumberFormatException err) {
  37.                         throw new SimulaException(SimulaException.ABNORMAL_INPUT);
  38.                 }
  39.  
  40.                 return _int ;
  41.         }
  42.         /**
  43.          * Returns a real number retrieved from the input.
  44.          * @return a real number retrieved from the input.
  45.          * @exception SimulaException Error while reading from input.
  46.          */
  47.         public static double inreal() throws SimulaException {
  48.                 BufferedReader Input = new BufferedReader(new InputStreamReader(System.in));
  49.                 String _string;
  50.                 Double _double;
  51.  
  52.                 try {
  53.                         _string = Input.readLine();
  54.                 } catch (IOException err) {
  55.                         throw new SimulaException(SimulaException.ABNORMAL_INPUT);
  56.                 }
  57.                 try {
  58.                         _double = new Double(_string);
  59.                 } catch (NumberFormatException err) {
  60.                         throw new SimulaException(SimulaException.ABNORMAL_INPUT);
  61.                 }
  62.  
  63.                 return _double.doubleValue();
  64.         }
  65.         /**
  66.          * Returns <code>True</code> if there are other items in the input buffer.
  67.          * @return <code>True</code> if there are other items in the input buffer.
  68.          * @exception java.io.IOException Error on the input.
  69.          */
  70.         public static boolean lastitem() throws java.io.IOException {
  71.                 BufferedReader Input = new BufferedReader(new InputStreamReader(System.in));
  72.                 return !Input.ready();
  73.         }
  74.         /**
  75.          * Returns the integer division of the two parameters.
  76.          * @param div The number to be divided.
  77.          * @param qt  The dividing number.
  78.          * @return the integer division of the two parameters.
  79.          */
  80.         public static int mod(int div, int qt) {
  81.                 return (div % qt);
  82.         }
  83.         /**
  84.          * Prints a floating point number on the screen with a number of fractional digits
  85.          * up to the given amount and aligned to the right in the given field.
  86.          *
  87.          * @param _out Number to print.
  88.          * @param _decimal Number of fractional digits.
  89.          * @param _fieldLen Size of the field.
  90.          * @exception SimulaException Error while printing on screen or wrong combination of parameters.
  91.          */
  92.         public static void outfix(double _out, int _decimal, int _fieldLen) throws SimulaException {
  93.  
  94.                 if (_decimal >= _fieldLen) throw new SimulaException(SimulaException.SENSELESS_PARAMETERS);
  95.  
  96.                 String tmpStr = String.valueOf(_out);
  97.                 String outStr = "";
  98.                 int len = tmpStr.length();
  99.                 int i;
  100.                 int intCount;
  101.  
  102.                 // Count the digits of the integer part plus the full stop, if it exists
  103.                 intCount = tmpStr.indexOf('.') + 1;             // .indexOf returns -1 if '.' doesn't exist
  104.                 if (intCount == 0) intCount = len;
  105.  
  106.                 // if fractional digits are more than requested reduce the number of digits that must be printed
  107.                 if (len - intCount > _decimal) len = intCount + _decimal;
  108.  
  109.                 if (len > _fieldLen) {
  110.                         // The field is too short to contain the number: display a dummy number and fire an Exception
  111.                         tmpStr = "";
  112.                         for(i = 0; i < _fieldLen; i++) {
  113.                                 tmpStr = tmpStr + "*";
  114.                         }
  115.                         System.out.println(tmpStr);
  116.                         throw new SimulaException(SimulaException.FIELD_TOO_SHORT);
  117.                 } else {
  118.                         // Take the digits to be printed and fill the remaining portion of the field with spaces left aligned
  119.                         outStr = tmpStr.substring(0, len);
  120.                         tmpStr = "";
  121.                         for( i = 0; i < _fieldLen - len; i++) {
  122.                                 tmpStr = tmpStr + " ";
  123.                         }
  124.                         System.out.println(tmpStr + outStr);
  125.                 }
  126.         }
  127.         /**
  128.          * Flushes the screen.
  129.          */
  130.         public static void outimage() {
  131.                 System.out.flush();
  132.         }
  133.         /**
  134.          * Prints an integer number on the screen and given the field size
  135.          * aligns the number on the right, preceding it with spaces enough to
  136.          * fill the field.
  137.          *
  138.          * @param _out Number to print.
  139.          * @param _fieldLen Size of the field.
  140.          * @exception SimulaException Error while printing on screen.
  141.          */
  142.         public static void outint(int _out, int _fieldLen) throws SimulaException {
  143.                 int len = (String.valueOf(_out)).length();
  144.                 String tmpStr = "";
  145.                 int i;
  146.  
  147.                 if (len > _fieldLen) {
  148.                         for( i = 1; i <= _fieldLen; i++) {
  149.                                 tmpStr = tmpStr + "*";
  150.                         }
  151.                         System.out.println(tmpStr);
  152.                         throw new SimulaException(SimulaException.FIELD_TOO_SHORT);
  153.                 } else {
  154.                         for( i = 1; i <= _fieldLen - len; i++) {
  155.                                 tmpStr = tmpStr + " ";
  156.                         }
  157.                         System.out.println(tmpStr + String.valueOf(_out));
  158.                 }
  159.         }
  160.         /**
  161.          * Prints out a number in scientific format
  162.          * @param value double number to print out
  163.          * @param dec int number of digits to use in fractional part
  164.          * @param len int lenght of field to print into
  165.             * @exception SimulaException Wrong combination of parameters.
  166.          */
  167.         public static void outreal(double value, int dec, int _fieldLen ) throws SimulaException {
  168.  
  169.         String intPart = "0" ;
  170.         String decPart = "" ;
  171.         double absVal = java.lang.Math.abs(value) ;
  172.         String tmp= new String("" + absVal) ;
  173.         String s ;
  174.         String exp = "" ;
  175.         String total = "" ;
  176.         int firstDigit = 0 ;
  177.         int i=0 ;
  178.         i=0 ;
  179.         if (absVal!=0){
  180.                 s=String.valueOf(tmp.charAt(i)) ;
  181.                 while (((s.compareTo(".")==0)) || ((s.compareTo("0")==0)))
  182.                         s=String.valueOf(tmp.charAt(++i)) ;
  183.                 intPart=String.valueOf(tmp.charAt(i)) ;
  184.                 firstDigit=i ;
  185.                 i++ ;
  186.                 while (i<tmp.length()){
  187.                         s=String.valueOf(tmp.charAt(i++)) ;
  188.                         if (s.compareTo(".") !=0)
  189.                                 decPart=decPart+s ;
  190.                 }
  191.  
  192.         }
  193.         else{
  194.                 intPart="0" ;
  195.                 decPart="" ;
  196.         }
  197.  
  198.  
  199.         if (absVal>=1) {
  200.                 exp = "" + (tmp.indexOf(".")-1) ;
  201.                 if (exp.length() == 1)
  202.                         exp = "0" + exp ;
  203.                 exp = "e+" + exp ;
  204.         } else {
  205.                 exp=""+(firstDigit-tmp.indexOf(".")) ;
  206.                 if (exp.length()==1)
  207.                         exp="0"+exp ;
  208.                 exp="e-"+exp ;
  209.         }
  210.  
  211.         total=intPart+"." ;
  212.  
  213.         for (i=1;i<=dec;i++)
  214.                 if (i<=decPart.length())
  215.                         total=total+String.valueOf(decPart.charAt(i-1)) ;
  216.                 else
  217.                         total=total+"0" ;
  218.         total=total+exp ;
  219.         if (value<0)
  220.                 total="-"+total ;
  221.  
  222.  
  223.         if (total.length() > _fieldLen) {
  224.           total="" ;
  225.           for(i = 1; i <= _fieldLen; i++) {
  226.                   total = total + "*";
  227.           }
  228.           System.out.println(total) ;
  229.           throw new SimulaException(SimulaException.FIELD_TOO_SHORT);
  230.  
  231.         } else {
  232.            System.out.println(total) ;
  233.            return ;
  234.         }
  235.  
  236.         }
  237.         /**
  238.          * Prints a string on the screen.
  239.          */
  240.         public static void outtext(String _text) {
  241.                 System.out.println(_text);
  242.         }
  243.         /**
  244.          * Returns the sign of the given real number.
  245.          * @return the sign of the given real number.
  246.          */
  247.         public static int sign(double _real) {
  248.                 if ((_real == 0) || (Double.isNaN(_real))) {
  249.                         return 1;
  250.                 } else {
  251.                         return (int)(_real/Math.abs(_real));
  252.                 }
  253.         }
  254. }