home *** CD-ROM | disk | FTP | other *** search
- Program Test_PrintUsing;
- {$C-,V-}
- { this program can be used to test the PrintUsing procedure in the
- file USING.INC. The print mask rules are as follows:
-
- Mask Description
- --------------- ---------------------------------------------
- **##.## the leading '**' reserve space for digits as
- well as causing the field to filled in with '*'
- in all blank positions
-
- ####.##- The number is output and if negative the sign
- is after j\e number otherwise the sign is blanked.
-
- $$##.## The '$$' prints a '$' just prior to the first
- digit of the number. The '$$' counts as only
- one '#'.
-
- ##,###.## Commas to be printed in the output field should
- be inserted into the format.
-
- If the number exceeds the format the number will be printed using the
- default format preceeded by a '%'.
-
- }
- type
- Str80 = String[80];
-
- {Begin using.inc }
- procedure PrintUsing(var FileOut:Text; Mask:Str80; Number:real);
- { This procedure emulates the print using function of BASIC-PLUS }
- { on DEC's RSTS/E. All functions except those for printing }
- { exponential format are implemented as described in the language}
- { manual. }
-
- var
- TrailSign,
- AsteriskFill,
- FloatDollar,
- FirstDigit,
- Good : boolean;
- Sign,
- I,
- digit,
- Rdigit,
- Point,
- Dol,
- k : integer;
- Source : String[80];
- begin
- TrailSign:=Copy(Mask,Length(Mask),1)='-';
- If Number > 0.0 then
- Sign:=1
- else begin { number is negative }
- Sign:=-1;
- If TrailSign then
- Number:=-Number;
- end; { number is negative }
- AsteriskFill:=Copy(Mask,1,2)='**';
- FloatDollar:=Copy(Mask,1,2)='$$';
- Point:=0;
- digit:=0;
- Rdigit:=0;
- Good:=true;
- If AsteriskFill and FloatDollar then
- Good:=false;
- if Good then
- begin { format valid }
- for I:=1 to Length(Mask) do
- case Mask[I] of
- '#' : begin
- digit:=digit+1;
- If (Point>0) then
- Rdigit:=Rdigit+1;
- end;
- '.' : Point:=I;
- end;
- If FloatDollar then
- digit:=digit+1
- else If AsteriskFill then
- digit:=digit+2;
- If Point>0 then
- digit:=digit+1;
- Str(Number:digit:Rdigit,Source);
- If Length(Source)>digit then
- Good:=false;
- If Good then
- begin { not too many digits }
- If (Rdigit>0) then
- begin { decimal point expected }
- Point:=Pos('.',Source);
- If (Point>0) then
- Source:=Copy(Source,1,Point-1)+Copy(Source,Point+1,Rdigit);
- end; { decimal point expected }
- k:=0;
- Dol:=0;
- FirstDigit:=false;
- for I:=1 to Length(Mask) do
- begin { move digits into mask loop }
- case Mask[I] of
- ',' : If Not FirstDigit then
- If AsteriskFill then
- Mask[I]:='*'
- else If FloatDollar then
- Mask[I]:=' ';
- '#',
- '*' : begin { digit holder }
- k:=k+1;
- Mask[I]:=Source[k];
- If (Mask[I]=' ') then
- begin { blank entry }
- if AsteriskFill then
- Mask[I]:='*';
- end { blank entry }
- else
- If Not FirstDigit then
- begin { floating dollar and non blank entry }
- FirstDigit:=true;
- If FloatDollar then
- Mask[I-1]:='$';
- FloatDollar:=false;
- end; { floating dollar and non blank entry }
- end; { digit holder }
- '$' : begin { dollar sign }
- If FloatDollar then
- begin { floating dollar sign requested }
- Dol:=Dol+1;
- Mask[I]:=' ';
- If Dol=2 then
- begin { 2nd dollar sign encountered }
- k:=k+1;
- Mask[I]:=Source[k];
- end; { 2nd dollar sign encountered }
- end; { floating dollar sign requested }
- end; { dollar sign }
- end; { case Mask[I] of }
- end; { move digits into mask loop }
- If TrailSign then
- if Sign=1 then
- Mask[Length(Mask)]:=' ';
- write(FileOut,Mask);
- end; { not too many digits }
- end; { format valid }
- If Not Good then
- write(FileOut,'%',Number);
- end;
-
- var
- Mask : String[20];
- Number : Real;
- Junk : Integer;
-
- begin
- Mask:='$$##,###.##-';
- Number:=1234.45;
- PrintUsing(Con,Mask,Number); { Output ' $1,234.45 ' }
- writeln(Con);
- Junk:=-444;
- PrintUsing(Con,Mask,Junk); { Output ' $444.00-' }
- writeln(Con);
- Number:=446557899.;
- Mask:='###-##-####';
- PrintUsing(Con,Mask,Number); { Output '446-55-7899' }
- writeln(Con);
- Mask:='**#,###,###.##-';
- Number:=-12345.66;
- PrintUsing(Con,Mask,Number); { Output '*****12,345.66-' }
- writeln(Con);
- Mask:='##.#';
- Junk:=345;
- PrintUsing(Con,Mask,Junk); { Output '% 3.4500000000E+02'}
- writeln(Con);
- end.