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.

Arrays 105<br />

with <strong>Ada</strong>.Text_Io, Class_Histogram;<br />

use <strong>Ada</strong>.Text_Io, Class_Histogram;<br />

procedure Ma<strong>in</strong> is<br />

Ch:Character;<br />

--Current character<br />

Text_Histogram: Histogram; --Histogram object<br />

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

Reset(Text_Histogram); --Reset to empty<br />

while not End_Of_File loop --For each l<strong>in</strong>e<br />

while not End_Of_L<strong>in</strong>e loop --For each character<br />

Get(Ch);<br />

--Get current character<br />

Add_To( Text_Histogram, Ch ); --Add to histogram<br />

end loop;<br />

Skip_L<strong>in</strong>e;<br />

--Next l<strong>in</strong>e<br />

end loop;<br />

Put( Text_Histogram ); --Pr<strong>in</strong>t histogram<br />

end Ma<strong>in</strong>;<br />

The class histogram is def<strong>in</strong>ed by the follow<strong>in</strong>g package specification:<br />

package Class_Histogram is<br />

type Histogram is private;<br />

Def_Height : constant Positive := 14;<br />

procedure Reset( The:<strong>in</strong> out Histogram );<br />

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

procedure Put(The:<strong>in</strong> Histogram; Height:<strong>in</strong> Positive:=Def_Height);<br />

private<br />

type Alphabet_Index is new Character range 'A' .. 'Z';<br />

type Alphabet_Array is array (Alphabet_Index) of Natural;<br />

type Histogram is record<br />

Number_Of : Alphabet_Array := ( others => 0 );<br />

end record;<br />

end Class_Histogram;<br />

The histogram is calculated us<strong>in</strong>g the <strong>in</strong>dividual letter frequencies that are stored <strong>in</strong> an array of Naturals<br />

<strong>in</strong>dexed by the upper case letters 'A' .. 'Z'. The implementation is simplified by allow<strong>in</strong>g each letter to <strong>in</strong>dex<br />

directly the frequency count for the letter.<br />

In the implementation of the class histogram shown below, the procedure reset is used to set the<br />

<strong>in</strong>dividual frequency counts for each letter to 0.<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io, <strong>Ada</strong>.Characters.Handl<strong>in</strong>g;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io, <strong>Ada</strong>.Characters.Handl<strong>in</strong>g;<br />

package body Class_Histogram is<br />

procedure Reset(The:<strong>in</strong> out Histogram) is<br />

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

The.Number_Of := ( others => 0 ); --Reset counts to 0<br />

end Reset;<br />

The procedure Add_To uses the functions Is_Lower, To_Upper and Is_Upper which are conta<strong>in</strong>ed <strong>in</strong><br />

the package <strong>Ada</strong>.Characters.Handl<strong>in</strong>g. A full description of these functions can be found <strong>in</strong> Section C.7,<br />

Appendix C.<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!