ads:
package Airlines_Package is
type Airlines( Size: Positive ) is limited private;
procedure Init( P: out Airlines );
procedure AssignName( P: in out Airlines; Index: in Positive; Name: in String );
procedure InsertFlight( P: in out Airlines; From, To: in String );
private
type Matrix is array ( Integer range <>, Integer range <>) of Boolean;
type NamesArray is array ( Integer range <> ) of String(1..10);
type Airlines( Size: Positive ) is record
Names: NamesArray( 1..Size ) := (others => " ");
Flights: Matrix( 1..Size, 1..Size );
end record;
end Airlines_Package;
adb:
package body Airlines_Package is
procedure Init( P: out Airlines ) is
begin
for I in 1..P.Size loop
for J in 1..P.Size loop
if I = J then
P.Flights(I, J) := True;
else
P.Flights(I, J) := False;
end if;
end loop;
end loop;
end;
procedure AssignName( P: in out Airlines; Index: in Positive; Name: in String ) is
begin
for I in 1..10 loop
P.Names(1)(I) := Name(Name'First + I);
end loop;
-- P.Names(Index) := Name;
end;
procedure InsertFlight( P: in out Airlines; From, To: in String ) is
begin
declare
FromIndex, ToIndex: Integer := -1;
begin
for I in 1..3 loop
if P.Names(I) = From then
FromIndex := I;
elsif P.Names(I) = To then
ToIndex := I;
end if;
end loop;
if( ( FromIndex /= -1 ) and ( ToIndex /= -1 ) ) then
P.Flights(FromIndex, ToIndex) := True;
P.Flights(ToIndex, FromIndex) := True;
end if;
end;
end;
end Airlines_Package;
test program:
with Ada.Integer_Text_IO, Airlines_Package;
use Ada.Integer_Text_IO, Airlines_Package;
procedure Main is
Q: Airlines(3);
begin
Init( Q );
AssignName( Q, 1, "Wien" );
AssignName( Q, 2, "London" );
AssignName( Q, 3, "Prague" );
-- InsertFlight( Q, "Prague", "London" );
end Main;
nagyz@platoon:~/ada$ gnatmake main
gcc-4.1 -c airlines_package.adb
gnatbind -x main.ali
gnatlink main.ali
nagyz@platoon:~/ada$ ./main
raised CONSTRAINT_ERROR : airlines_package.adb:17 index check failed
nagyz@platoon:~/ada$