home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / pascal / passrc / rinput.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-10-15  |  5.5 KB  |  218 lines

  1. { -----------------------------------------------------------------------------
  2.  
  3.                                  NOTICE:
  4.  
  5.       THESE MATERIALS are UNSUPPORTED by OSS!  If you do not understand how to
  6.       use them do not contact OSS for help!  We will not teach you how to
  7.       program in Pascal.  If you find an error in these materials, feel free
  8.       to SEND US A LETTER explaining the error, and how to fix it.
  9.  
  10.       THE BOTTOM LINE:
  11.  
  12.          Use it, enjoy it, but you are on your own when using these materials!
  13.  
  14.  
  15.                                DISCLAIMER:
  16.  
  17.       OSS makes no representations or warranties with respect to the contents
  18.       hereof and specifically disclaim all warranties of merchantability or
  19.       fitness for any particular purpose.   This document is subject to change
  20.       without notice.
  21.  
  22.       OSS provides these materials for use with Personal Pascal.  Use them in
  23.       any way you wish.
  24.  
  25.    -------------------------------------------------------------------------- }
  26.  
  27.  
  28. {**************************************************************************
  29.  
  30.    RINPUT.PAS - function takes a high, low, and default number
  31.                 and a prompt and returns when satisfied
  32.                 10-14-86 MJC
  33.  
  34.    Include in your program (*$I rinput.pas *)
  35.  
  36.  
  37.    Rinput( Prompt, Low, High, Default )
  38.  
  39.    Prompt user to input a number.
  40.  
  41.    Example:
  42.  
  43.       Real_Var := Rinput( 'Enter Num ', 5.5, 1000.55, 57.654 );
  44.  
  45.    would prompt the user for a number in the range of 5.5 to 1000.55, with
  46.    a default value of 57.654 (if user hits <RETURN> without entering
  47.    anything, this is the value returned).  If a number outside the range of
  48.    Low to High is entered the user will be prompted to enter another number.
  49.  
  50. ***************************************************************************}
  51.  
  52. FUNCTION RVal( St: String): Real; { convert a string to a real number -
  53.                                         see Rstrval.pas }
  54.  
  55. Const
  56.   Max_Digits    = 09;
  57.  
  58. Var
  59.   Dec_Exp,
  60.   Exp_Value,
  61.   Count,
  62.   Position:     Integer;
  63.   Chr:          Char;
  64.   Result:       Real;
  65.   Dec_Sign,
  66.   Exp_Sign:     Boolean;
  67.  
  68.   PROCEDURE Add_Digit;
  69.  
  70.   Begin
  71.     Result := (Result * 10) + (Ord(Chr) & $0F);
  72.   End;
  73.  
  74.   PROCEDURE Read_Chr;
  75.  
  76.   Begin
  77.     Position := Position + 1;
  78.     If Position > Length(St) Then
  79.       Chr := 'X'
  80.     Else
  81.       Chr := St[Position];
  82.   End;
  83.  
  84. Begin
  85.   Position := 0;
  86.   Read_Chr;
  87.   Result := 0.0;
  88.  
  89. { Get sign }
  90.  
  91.   Dec_Sign := False;
  92.   If Chr = '+' Then Read_Chr;
  93.   If Chr = '-' Then
  94.   Begin
  95.     Read_Chr;
  96.     Dec_Sign := True;
  97.   End;
  98.  
  99. { Get digits to left of decimal point }
  100.  
  101.   Dec_Exp := 0;
  102.   Count := Max_Digits;
  103.   While ('0' <= Chr) And (Chr <= '9') Do
  104.   Begin
  105.     If Count > 0 Then
  106.     Begin
  107.       Add_Digit;
  108.       Count := Count - 1;
  109.     End
  110.     Else
  111.       Dec_Exp := Dec_Exp + 1;
  112.     Read_Chr;
  113.   End;
  114.  
  115. { Get digits to the right of decimal point }
  116.  
  117.   If Chr = '.' Then
  118.   Begin
  119.     Read_Chr;
  120.     While ('0' <= Chr) And (Chr <= '9') Do
  121.     Begin
  122.       If Count > 0 Then
  123.       Begin
  124.         Add_Digit;
  125.         Dec_Exp := Dec_Exp - 1;
  126.         Count := Count - 1;
  127.       End;
  128.       Read_Chr;
  129.     End;
  130.   End;
  131.  
  132. { Get exponent part }
  133.  
  134.   If (Chr = 'E') Or (Chr = 'e') Then
  135.   Begin
  136.     Read_Chr;
  137.     Exp_Sign := False;
  138.     If Chr = '+' Then Read_Chr;
  139.     If Chr = '-' Then
  140.     Begin
  141.       Read_Chr;
  142.       Exp_Sign := True;
  143.     End;
  144.     Exp_Value := 0;
  145.     If ('0'<=Chr) And (Chr<='9') Then Exp_Value := (Ord(Chr) & $0F)*10;
  146.     Read_Chr;
  147.     If ('0'<=Chr) And (Chr<='9') Then Exp_Value := Exp_Value+(Ord(Chr) & $0F);
  148.     If (Chr = 'X') And (Exp_Value >= 10) Then Exp_Value := Exp_Value Div 10;
  149.     If Exp_Sign Then
  150.       Dec_Exp := Dec_Exp - Exp_Value
  151.     Else
  152.       Dec_Exp := Dec_Exp + Exp_Value;
  153.   End;
  154.  
  155. { Multiply or divide Result by power of 10 specified by Dec_Exp }
  156.  
  157.   If Dec_Exp > 0 Then
  158.     Result := Result * PwrOfTen(Dec_Exp)
  159.   Else
  160.     Result := Result / PwrOfTen(Abs(Dec_Exp));
  161.  
  162.   If Dec_Sign Then Result := -Result;
  163.  
  164.   RVal := Result;
  165.  
  166. End;
  167.  
  168.  
  169. {************************************************************************
  170.  
  171.         The Famous Rinput Function...
  172.  
  173. ************************************************************************}
  174.  
  175.  
  176. FUNCTION Rinput( Prompt : STRING; Low, High, Def : REAL ) : REAL;
  177.  
  178.  
  179. VAR
  180.  
  181.    Tempval : REAL;      { hold temp value... }
  182.    Tempstr : STRING;    { get input }
  183.    Done : BOOLEAN;      { Are we done... }
  184.  
  185.    BEGIN
  186.  
  187.       REPEAT
  188.          BEGIN
  189.             Write( Prompt );  { send prompt string }
  190.             Readln( Tempstr );  { get input from user }
  191.  
  192.             IF Length( Tempstr ) = 0 THEN
  193.                BEGIN
  194.                   Tempval := Def;  { use default value }
  195.                   Done := TRUE;    { and fall thru }
  196.                END
  197.  
  198.             ELSE
  199.  
  200.                BEGIN
  201.                   Tempval := Rval( Tempstr );      { convert string to real }
  202.                   IF (Tempval >= Low) AND (Tempval <= High) THEN
  203.                      Done := TRUE         { All is well, continue }
  204.                   ELSE
  205.                      BEGIN
  206.                         Write( '* Value outside range: ' );
  207.                         Writeln( Low:1:2, ' - ', High:1:2 );
  208.                         Writeln;
  209.                         Done := FALSE        { otherwise go again... }
  210.                      END;
  211.                END;
  212.  
  213.          END
  214.       UNTIL Done;
  215.       Rinput := Tempval;                { and return the value...}
  216.    END;
  217.  
  218.