18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

106 Arrays<br />

procedure Add_To(The:<strong>in</strong> out Histogram; A_Ch:<strong>in</strong> Character) is<br />

Ch : Character;<br />

beg<strong>in</strong><br />

Ch := A_Ch;<br />

--As write to ch<br />

if Is_Lower(Ch) then --Convert to upper case<br />

Ch := To_Upper( Ch );<br />

end if;<br />

if Is_Upper( Ch ) then --so record<br />

declare<br />

C : Alphabet_Index := Alphabet_Index(Ch);<br />

beg<strong>in</strong><br />

The.Number_Of(C) := The.Number_Of(C) + 1;<br />

end;<br />

end if;<br />

end Add_To;<br />

The histogram is displayed as a bar graph correspond<strong>in</strong>g to the accuracy of the output device, which <strong>in</strong> this case is<br />

an ANSI term<strong>in</strong>al. The size of the histogram is set by the defaulted parameter Height.<br />

procedure Put(The:<strong>in</strong> Histogram;<br />

Height:<strong>in</strong> Positive:=Def_Height) is<br />

Frequency : Alphabet_Array; --Copy to process<br />

Max_Height : Natural := 0; --Observed max<br />

beg<strong>in</strong><br />

Frequency := The.Number_Of; --Copy data (Array)<br />

for Ch <strong>in</strong> Alphabet_Array'Range loop --F<strong>in</strong>d max frequency<br />

if Frequency(Ch) > Max_Height then<br />

Max_Height:= Frequency(Ch);<br />

end if;<br />

end loop;<br />

if Max_Height > 0 then<br />

for Ch <strong>in</strong> Alphabet_Array'Range loop --Scale to max height<br />

Frequency(Ch):=(Frequency(Ch)*Height)/(Max_Height);<br />

end loop;<br />

end if;<br />

for Row <strong>in</strong> reverse 1 .. Height loop --Each l<strong>in</strong>e<br />

Put( " | " );<br />

--start of l<strong>in</strong>e<br />

for Ch <strong>in</strong> Alphabet_Array'Range loop<br />

if Frequency(Ch) >= Row then<br />

Put('*');<br />

--bar of hist >= col<br />

else<br />

Put(' ');<br />

--bar of hist < col<br />

end if;<br />

end loop;<br />

Put(" | "); New_L<strong>in</strong>e; --end of l<strong>in</strong>e<br />

end loop;<br />

Put(" +----------------------------+"); New_L<strong>in</strong>e;<br />

Put(" ABCDEFGHIJKLMNOPQRSTUVWXYZ " ); New_L<strong>in</strong>e;<br />

Put(" * = (approx) ");<br />

Put( Float(Max_Height) / Float(Height), Aft=>2, Exp=>0 );<br />

Put(" characters "); New_L<strong>in</strong>e;<br />

end Put;<br />

end Class_Histogram;<br />

Note:<br />

By implement<strong>in</strong>g the pr<strong>in</strong>t<strong>in</strong>g of the histogram as part of the package Class_Histogram this<br />

severely limits the cases when this code can be re-used.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!