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.

174 Child libraries<br />

The <strong>in</strong>ternal function simplify reduces the rational number to its simplest form. Thus the rational number<br />

4/8 is reduced to 1/2.<br />

function Simplify( The:<strong>in</strong> Rational ) return Rational is<br />

Res: Rational := The;<br />

D : Positive;<br />

--Divisor to reduce with<br />

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

if Res.Below = 0 then --Invalid treat as 0<br />

Res.Above := 0; Res.Below := 1;<br />

end if;<br />

D := 2; --Divide by 2, 3, 4 ...<br />

while D < Res.Below loop<br />

while Res.Below rem D = 0 and then Res.Above rem D = 0 loop<br />

Res.Above := Res.Above / D;<br />

Res.Below := Res.Below / D;<br />

end loop;<br />

D := D + 1;<br />

end loop;<br />

return Res;<br />

end Simplify;<br />

Note:<br />

It is left to the reader to improve the efficiency of the algorithm used.<br />

The standard operators of +, -, /, and * are overloaded to allow these standard operations to be performed<br />

between <strong>in</strong>stances of rational numbers.<br />

function "+" (F:<strong>in</strong> Rational; S:<strong>in</strong> Rational) return Rational is<br />

Res : Rational;<br />

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

Res.Below := F.Below * S.Below;<br />

Res.Above := F.Above * S.Below + S.Above * F.Below;<br />

return Simplify(Res);<br />

end "+";<br />

function "-" (F:<strong>in</strong> Rational; S:<strong>in</strong> Rational) return Rational is<br />

Res : Rational;<br />

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

Res.Below := F.Below * S.Below;<br />

Res.Above := F.Above * S.Below - S.Above * F.Below;<br />

return Simplify(Res);<br />

end "-";<br />

function "*" (F:<strong>in</strong> Rational; S:<strong>in</strong> Rational) return Rational is<br />

Res : Rational;<br />

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

Res.Above := F.Above * S.Above;<br />

Res.Below := F.Below * S.Below;<br />

return Simplify(Res);<br />

end "*";<br />

function "/" (F:<strong>in</strong> Rational; S:<strong>in</strong> Rational) return Rational is<br />

Res : Rational;<br />

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

Res.Above := F.Above * S.Below;<br />

Res.Below := F.Below * S.Above;<br />

return Simplify(Res);<br />

end "/";<br />

Note:<br />

Additional def<strong>in</strong>itions of these standard operators would need to be provided if it was required to be<br />

able to perform operations such as Rat_Const(1,2) + 1.<br />

In this particular case a def<strong>in</strong>ition of + between a rational and an <strong>in</strong>teger would also need to be<br />

provided.<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!