Path: utzoo!attcan!uunet!husc6!bbn!uwmcsd1!csd4.milw.wisc.edu!markh From: markh@csd4.milw.wisc.edu (Mark William Hopkins) Newsgroups: comp.lang.pascal Subject: Number-string conversions Message-ID: <5817@uwmcsd1.UUCP> Date: 17 May 88 00:43:33 GMT Sender: daemon@uwmcsd1.UUCP Reply-To: markh@csd4.milw.wisc.edu (Mark William Hopkins) Organization: University of Wisconsin-Milwaukee Lines: 135 While the issue is "on the table", I feel it appropriate to give a few procedures to show how the conversions take place when Pascal reads & writes in a text file. READING INTEGERS FROM TEXT: procedure ReadInt(var T : text; var I : integer); var Sign : char; begin (*** SKIP SPACES ***) while T^ = ' ' do get(T); (*** GET THE SIGN ***) if T^ in ['+','-'] then begin read(T, Sign) end else Sign := '+'; (*** CONVERT THE STRING TO AN INTEGER ***) I := 0; while T^ in ['0'..'9'] do begin I := 10*I + ord(T^) - ord('0'); get(T) end; (*** ADJUST FOR THE SIGN ***) if Sign = '-' then I := -I end; READING REAL NUMBERS FROM TEXT: (The syntax allowed by this procedure is slightly more liberal than that which standard Pascal allows for.) procedure ReadReal(var T : text; var R : real); var Exponent : integer; Power : real; begin (*** CONVERT THE INTEGER PART ***) ReadInt(T, R); (*** CONVERT THE DECIMAL PART ***) if T^ = '.' then begin get(T); Power := 1; while T^ in ['0'..'9'] do begin Power := Power / 10; R := R + ( ord(T^) - ord('0') ) * Power; get(T) end end; (*** CONVERT THE EXPONENT PART ***) if T^ in ['e','E'] then begin get(T); ReadInt(T, Exponent); while Exponent > 0 do begin Exponent := Exponent - 1; R := R * 10 end; while Exponent < 0 do begin Exponent := Exponent + 1; R := R / 10 end end end; FORMATTING INTEGERS INTO TEXT: procedure WriteInt(T : text; I : integer; FieldWidth : integer); var Sign : char; Size, Exponent : integer; begin (*** DETERMINE THE SIGN ***) if I < 0 then begin Sign := '-'; I := -I end else if I > 0 then Sign := '+' else Sign := '0'; (*** DETERMINE THE SIZE OF THE INTEGER STRING ***) Exponent := 1; Size := 0; while (Exponent < I) do begin Exponent := 10 * Exponent; Size := Size + 1 end; (*** PAD THE INTEGER TO THE LEFT, IF NECESSARY ***) if FieldWidth > Size then begin while FieldWidth > Size + 1 do begin FieldWidth := FieldWidth - 1; write(T, ' ') end; if Sign = '+' then write(T, ' ') end; (*** CONVERT THE NUMBER TO A STRING ***) if Sign = '+' then write(T, ' ') else if Sign = '-' then write(T, '-') else if Sign = '0' then write(T, '0'); else while Size > 0 do begin Size := Size - 1; Exponent := Exponent / 10; write(T, chr(I mod Exponent + ord('0')) ); I := I div Exponent end end; FORMATTING REALS TO TEXT: (F1, F2 are the two field widths used for formatting real numbers and rounding them to the desired number of decimal places.) procedure WriteReal(var T : text; R : real; FW1, FW2 : integer); var Mantissa : integer; Decimal : real; begin Mantissa := trunc(R); Decimal := R - Mantissa; (*** The field width FW1 includes the decimal part and decimal ***) (*** point in it. Therefore the integer part is written out ***) (*** with a field width of FW1 - FW2 - 1. ***) WriteInt(T, Mantissa, FW1 - FW2 - 1); if FW2 > 0 then begin write(T, '.'); while FW2 > 1 do begin FW2 := FW2 - 1; Decimal := Decimal * 10; write(T, chr( trunc(Decimal) + ord('0') ) ); Decimal := Decimal - trunc(Decimal) end; (*** ROUND THE LAST DECIMAL DIGIT ***) Decimal := Decimal * 10; write(T, chr( round(Decimal) + ord('0') ) ) end end