home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 138 / pascal / str_real.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-05-13  |  3.5 KB  |  155 lines

  1. {
  2.   STR_NUM contains two procedures which together convert a number
  3.   read as characters into an array into a real number.
  4.  
  5.   The following global declarations are necessary:
  6.  
  7.    CONST
  8.       Space = ' ';
  9.  
  10.    TYPE
  11.       NumStrType  = array [1..80] of char;
  12.  
  13.    VAR
  14.       Numeric_chrs : set of char;
  15.       Number       : real;
  16.       Good_num     : boolean;
  17.  
  18.   At the beginning of the program body (or the body of the procedure
  19.   which calls GET_NUM), insert the following line:
  20.  
  21.       Numeric_chrs := ['-', '.'] + ['0'..'9'];
  22.  
  23.  
  24.   Procedure GET_NUM is called with the appropriate parameters.  It reads
  25.   the characters into an array and calls CONVERT.  If Good_num is true,
  26.   the resulting real number is stored in Number.
  27.  
  28.   At present, the following are all valid input:
  29.  
  30.   2   ;   2.0 ;   -2  ;  .2   ;  +2  ;   +.02   ;  0.02
  31.  
  32.   Spaces before plus, minus, or a digit are skipped.  Any non-numeric
  33.   character (such as a space or letter) after the first plus, minus,
  34.   or digit makes Good_num false.
  35.  
  36.   Function SomeDigits checks for at least one digit in the string.
  37. }
  38.  
  39. procedure
  40. CONVERT   (    Num_string : NumStrType;
  41.                start      : integer;
  42.            VAR Number     : real );
  43.  
  44.  
  45.    VAR
  46.  
  47.       Factor,
  48.       i             : integer;
  49.       Fraction,
  50.       Int_val,
  51.       Temp          : long_integer;
  52.       Neg_num       : boolean;
  53.       Denom         : real;
  54.  
  55. BEGIN {CONVERT}
  56.  
  57. i := start;
  58. Int_val := 0;  Temp := 0;  Fraction := 0; Factor := 0;
  59. Neg_num := false;
  60. If Num_string[i] = '-' then
  61.    begin
  62.       Neg_num := true;
  63.       i := i + 1
  64.    end; {if}
  65. while Num_string[i] in ['0'..'9'] do begin
  66.    Temp := ORD( Num_string[i] ) - ORD( '0' );
  67.    Int_val := ( 10 * Int_val ) + Temp;
  68.    i := i + 1
  69. end; {while}
  70. if Num_string[i] = '.' then
  71.    begin
  72.       i := i +1;
  73.       while Num_string[i] in ['0'..'9'] do begin
  74.          Temp := ( ORD( Num_string[i] ) - ORD( '0' ) );
  75.          Fraction := ( 10 *  Fraction ) +  Temp;
  76.          Factor :=  Factor + 1;
  77.          i := i + 1
  78.       end {while}
  79.    end; {if}
  80. Denom := PwrOfTen( Factor );
  81. if Neg_num then
  82.    Number := - (( 1.0 * Int_val) + (Fraction / Denom))
  83. else
  84.    Number := ( 1.0 * Int_val ) + (Fraction / Denom)
  85.  
  86. END; {CONVERT}
  87.  
  88. function
  89. SomeDigits ( VAR Num_string : NumStrType;
  90.                  start,
  91.                  finish     : integer): boolean;
  92.  
  93.  
  94.    VAR
  95.        i           : integer;
  96.        found_digit : boolean;
  97.  
  98. BEGIN {SOMEDIGITS}
  99.  
  100. found_digit := false;
  101. For i := start to finish do
  102.  If (Num_string[i] in ['0'..'9']) then
  103.     found_digit := true;
  104.  
  105. SomeDigits := found_digit
  106.  
  107. END; {SOMEDIGITS}
  108.  
  109.  
  110.  
  111. procedure
  112. GET_NUM   ( VAR Number   : real;
  113.             VAR Good_num : boolean );
  114.  
  115.    VAR
  116.  
  117.       Num_string  : NumStrType;
  118.       start,
  119.       i           : integer;
  120.       chr         : char;
  121.  
  122. BEGIN {GET_NUM}
  123.  
  124. for i := 1 to 80 do
  125.    Num_string[i] := space;
  126. i := 1;
  127. Good_num := true;
  128. write( '> ');
  129. read( chr);
  130. while not eoln(input) do begin
  131.    Num_string[i] := chr;
  132.    i := i + 1;
  133.    read( chr )
  134. end; {while}
  135. readln;
  136. Good_num := (i > 1);
  137. if Good_num then
  138.    begin
  139.       i := 1;
  140.       while Num_string[i] = space do
  141.          i := i + 1;
  142.       if Num_string[i] = '+' then
  143.          i := i + 1;
  144.       start := i;
  145.       while Num_string[i] in Numeric_chrs do
  146.          i := i + 1;
  147.       if (( Num_string[i] = space )
  148.       And (SomeDigits(Num_string, start, i)))  then
  149.          { ok }
  150.          CONVERT( Num_string, start, number )
  151.       else
  152.          Good_num := false
  153.    end {if}
  154. END; {GET_NUM}
  155.