All pastes #434993 Raw Edit

Mine

public text v1 · immutable
·published 2007-04-11 18:36 UTC
rendered paste body
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:
with Ada.Text_IO, Ada.Integer_Text_IO;
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
        declare
            Max: Positive;
        begin
            Max := Name'Last;
            if Max > 10 then
                Max := 10;
            end if;
            for I in 1..Max loop
                P.Names(Index)(I) := Name(Name'First + I - 1);
            end loop;
        end;
    end;
    procedure InsertFlight( P: in out Airlines; From, To: in String ) is
    begin
        declare
            FromIndex, ToIndex: Integer := -1;
        begin
            for I in 1..P.Size 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;
                Ada.Text_IO.Put_Line("Ok!");
            end if;
            Ada.Integer_Text_IO.Put(FromIndex);
            Ada.Integer_Text_IO.Put(ToIndex);
        end;
    end;
end Airlines_Package;

main:
with Ada.Text_IO, Ada.Integer_Text_IO, Airlines_Package;
use Ada.Text_IO, Ada.Integer_Text_IO, Airlines_Package;
procedure Main is
    Q: Airlines(3);
    L: String(1..10) := "          ";
begin
    Init( Q );
    AssignName( Q, 1, "Wien" );
    AssignName( Q, 2, "London" );
    AssignName( Q, 3, "Prague" );
    InsertFlight( Q, "Prague", "London" );
end Main;

running:
nagyz@platoon:~/ada$ gnatmake main
gcc-4.1 -c airlines_package.adb
gnatbind -x main.ali
gnatlink main.ali
nagyz@platoon:~/ada$ ./main
         -1         -1
nagyz@platoon:~/ada$