home *** CD-ROM | disk | FTP | other *** search
- {
- STR_NUM contains two procedures which together convert a number
- read as characters into an array into a real number.
-
- The following global declarations are necessary:
-
- CONST
- Space = ' ';
-
- TYPE
- NumStrType = array [1..80] of char;
-
- VAR
- Numeric_chrs : set of char;
- Number : real;
- Good_num : boolean;
-
- At the beginning of the program body (or the body of the procedure
- which calls GET_NUM), insert the following line:
-
- Numeric_chrs := ['-', '.'] + ['0'..'9'];
-
-
- Procedure GET_NUM is called with the appropriate parameters. It reads
- the characters into an array and calls CONVERT. If Good_num is true,
- the resulting real number is stored in Number.
-
- At present, the following are all valid input:
-
- 2 ; 2.0 ; -2 ; .2 ; +2 ; +.02 ; 0.02
-
- Spaces before plus, minus, or a digit are skipped. Any non-numeric
- character (such as a space or letter) after the first plus, minus,
- or digit makes Good_num false.
-
- Function SomeDigits checks for at least one digit in the string.
- }
-
- procedure
- CONVERT ( Num_string : NumStrType;
- start : integer;
- VAR Number : real );
-
-
- VAR
-
- Factor,
- i : integer;
- Fraction,
- Int_val,
- Temp : long_integer;
- Neg_num : boolean;
- Denom : real;
-
- BEGIN {CONVERT}
-
- i := start;
- Int_val := 0; Temp := 0; Fraction := 0; Factor := 0;
- Neg_num := false;
- If Num_string[i] = '-' then
- begin
- Neg_num := true;
- i := i + 1
- end; {if}
- while Num_string[i] in ['0'..'9'] do begin
- Temp := ORD( Num_string[i] ) - ORD( '0' );
- Int_val := ( 10 * Int_val ) + Temp;
- i := i + 1
- end; {while}
- if Num_string[i] = '.' then
- begin
- i := i +1;
- while Num_string[i] in ['0'..'9'] do begin
- Temp := ( ORD( Num_string[i] ) - ORD( '0' ) );
- Fraction := ( 10 * Fraction ) + Temp;
- Factor := Factor + 1;
- i := i + 1
- end {while}
- end; {if}
- Denom := PwrOfTen( Factor );
- if Neg_num then
- Number := - (( 1.0 * Int_val) + (Fraction / Denom))
- else
- Number := ( 1.0 * Int_val ) + (Fraction / Denom)
-
- END; {CONVERT}
-
- function
- SomeDigits ( VAR Num_string : NumStrType;
- start,
- finish : integer): boolean;
-
-
- VAR
- i : integer;
- found_digit : boolean;
-
- BEGIN {SOMEDIGITS}
-
- found_digit := false;
- For i := start to finish do
- If (Num_string[i] in ['0'..'9']) then
- found_digit := true;
-
- SomeDigits := found_digit
-
- END; {SOMEDIGITS}
-
-
-
- procedure
- GET_NUM ( VAR Number : real;
- VAR Good_num : boolean );
-
- VAR
-
- Num_string : NumStrType;
- start,
- i : integer;
- chr : char;
-
- BEGIN {GET_NUM}
-
- for i := 1 to 80 do
- Num_string[i] := space;
- i := 1;
- Good_num := true;
- write( '> ');
- read( chr);
- while not eoln(input) do begin
- Num_string[i] := chr;
- i := i + 1;
- read( chr )
- end; {while}
- readln;
- Good_num := (i > 1);
- if Good_num then
- begin
- i := 1;
- while Num_string[i] = space do
- i := i + 1;
- if Num_string[i] = '+' then
- i := i + 1;
- start := i;
- while Num_string[i] in Numeric_chrs do
- i := i + 1;
- if (( Num_string[i] = space )
- And (SomeDigits(Num_string, start, i))) then
- { ok }
- CONVERT( Num_string, start, number )
- else
- Good_num := false
- end {if}
- END; {GET_NUM}
-