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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Exceptions 197<br />

14.3.1 Putt<strong>in</strong>g it all together<br />

The new package Class_stack_<strong>in</strong>t is tested by the follow<strong>in</strong>g program unit which is identical to the code<br />

seen <strong>in</strong> Section 13.5 except the with’ed and used’ed package is Class_Stack_Int rather than<br />

Class_Stack..<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Class_Stack_Int;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Class_Stack_Int;<br />

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

Number_Stack : Stack; --Stack of numbers<br />

Action : Character; --Action<br />

Number : Integer; --Number processed<br />

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

while not End_Of_File loop<br />

while not End_Of_L<strong>in</strong>e loop<br />

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

Get( Action );<br />

case Action is --Process action<br />

when '+' =><br />

Get( Number ); Push(Number_Stack,Number);<br />

Put("push number = "); Put(Number); New_L<strong>in</strong>e;<br />

when '-' =><br />

Pop(Number_Stack,Number);<br />

Put("Pop number = "); Put(Number); New_L<strong>in</strong>e;<br />

when others =><br />

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

end case;<br />

exception<br />

when Stack_Error =><br />

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

when Data_Error =><br />

Put("Not a number"); New_L<strong>in</strong>e; Skip_L<strong>in</strong>e;<br />

when End_Error =><br />

Put("Unexpected end of file"); New_L<strong>in</strong>e; exit;<br />

end;<br />

end loop;<br />

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

end loop;<br />

Reset( Number_Stack );<br />

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

When run with the follow<strong>in</strong>g data:<br />

+1+2+3+4----<br />

the follow<strong>in</strong>g results will be produced:<br />

push number = 1<br />

push number = 2<br />

push number = 3<br />

Stack_error<br />

Pop number = 3<br />

Pop number = 2<br />

Pop number = 1<br />

Stack_error<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!