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.

176 Child libraries<br />

The function Image returns a str<strong>in</strong>g represent<strong>in</strong>g a rational number <strong>in</strong> canonical form. The strategy used is to<br />

use the <strong>in</strong>built function Integer’Image to convert a number <strong>in</strong>to a character str<strong>in</strong>g. However, as this leaves a<br />

lead<strong>in</strong>g space for the sign character an <strong>in</strong>ternal function Trim is provided to strip off the lead<strong>in</strong>g character from<br />

such a str<strong>in</strong>g.<br />

The nested function To_Str<strong>in</strong>g delivers a str<strong>in</strong>g representation <strong>in</strong> canonical form of a positive rational<br />

number. By us<strong>in</strong>g a s<strong>in</strong>gle case of recursion this function can deal with the case when a rational number is of the<br />

form “a b/c”.<br />

function Image( The:<strong>in</strong> Rational ) return Str<strong>in</strong>g is<br />

Above : Integer := The.Above;<br />

Below : Integer := The.Below;<br />

function Trim( Str:<strong>in</strong> Str<strong>in</strong>g ) return Str<strong>in</strong>g is<br />

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

return Str( Str'First+1 .. Str'Last );<br />

end Trim;<br />

function To_Str<strong>in</strong>g( Above, Below : <strong>in</strong> Integer )<br />

return Str<strong>in</strong>g is<br />

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

if Above = 0 then --No fraction<br />

return "";<br />

elsif Above >= Below then --Whole number<br />

return Trim( Integer'Image(Above/Below) ) & " " &<br />

To_Str<strong>in</strong>g( Above rem below, Below );<br />

else<br />

return Trim( Integer'Image( Above ) ) & "/" &<br />

Trim( Integer'Image( Below ) );<br />

end if;<br />

end To_Str<strong>in</strong>g;<br />

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

if Above = 0 then<br />

return "0";<br />

--Zero<br />

elsif Above < 0 then<br />

return "-" & To_Str<strong>in</strong>g( abs Above, Below ); ---ve<br />

else<br />

return To_Str<strong>in</strong>g( Above, Below );<br />

end if;<br />

end Image;<br />

end Class_Rational;<br />

12.3 A bounded str<strong>in</strong>g class<br />

--+ve<br />

A partial solution to overcome the fixed size limitations of Ad str<strong>in</strong>gs is to use a discrim<strong>in</strong>ated record that can<br />

hold a str<strong>in</strong>g of any length up to a pre-def<strong>in</strong>ed maximum. The responsibilities of the class Bounded_Str<strong>in</strong>g<br />

which holds a variable length str<strong>in</strong>g is as follows:<br />

Method<br />

Operator:<br />

&<br />

Operators:<br />

> >= <

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

Saved successfully!

Ooh no, something went wrong!