Ada Programlama Dili — Örnekler: Diziler

mozgan
Ada Programlama Dili
7 min readJun 11, 2024
https://ecdn.teacherspayteachers.com/thumbitem/ARRAY-CITY-Fun-Multiplication-craft-activity-4780653-1656584194/original-4780653-1.jpg

1. Constrained Array

Constained_Array paketi içerisine aşağıdaki listede bulunan verilen türler, fonksiyonlar ve metotlar yazılacak:

  • My_Index, 0 ile 10 arasındaki sınırları belirten bir tür.
  • My_Array, indis elemenları My_Index‘ten olan bir dizi türü.
  • Init fonksiyonu My_Array türünden bir dizi oluşturup geri döndürecek. Bu dizinin elemanlarının değeri indis numaraları ile aynı olacak.
  • Double metodu parametre olarak verilen My_Array türündeki dizinin elemanlarının değerini iki katına çıkaracak.
  • First_Elem fonksiyonu, My_Array türündeki bir dizinin ilk elemanını döndürecek.
  • Last_Elem fonksiyonu, My_Array türündeki bir dizinin son elemanını döndürecek.
  • Length fonksiyonu, My_Array türündeki bir dizinin uzunluğunu döndürecek.
  • A, My_Array türünden üretilen bir dizi. Bu dizinin ilk elemanı 1, ikincisi 2, ve diğerleri 42 olacak.

constrained_array.ads

package Constrained_Arrays is

type My_Index is range 1 .. 10;
type My_Array is array (My_Index) of Integer;

function Init return My_Array;
procedure Double (Arr : in out My_Array);
function First_Elem (Arr : My_Array) return Integer;
function Last_Elem (Arr : My_Array) return Integer;
function Length (Arr : My_Array) return Integer;

A : My_Array := (1, 2, others => 42);

end Constrained_Arrays;

constrained_array.adb

package body Constrained_Arrays is

function Init return My_Array is
Arr : My_Array;
begin
for I in My_Array'Range loop
Arr (I) := Integer (I);
end loop;

return Arr;
end Init;

procedure Double (Arr : in out My_Array) is
begin
for I in Arr'Range loop
Arr (I) := Arr (I) * 2;
end loop;
end Double;

function First_Elem (Arr : My_Array) return Integer is (Arr (Arr'First));

function Last_Elem (Arr : My_Array) return Integer is (Arr (Arr'Last));

function Length (Arr : My_Array) return Integer is (Arr'Length);

end Constrained_Arrays;

Derleme ve Test

❯ gnatmake main.adb
gcc -c main.adb
gcc -c constrained_arrays.adb
gnatbind -x main.ali
gnatlink main.ali

❯ ./main Range_Chk
1
2
3
4
5
6
7
8
9
10

❯ ./main Array_Range_Chk
1
2
3
4
5
6
7
8
9
10

❯ ./main A_Obj_Chk
1
2
42
42
42
42
42
42
42
42

❯ ./main Init_Chk
1
2
3
4
5
6
7
8
9
10

❯ ./main Double_Chk
200
180
160
20
40
60
80
120
100
140

❯ ./main First_Elem_Chk
100

❯ ./main Last_Elem_Chk
70

❯ ./main Length_Chk
10

2. Colors: Lookup-Table

Bu örnekte Color_Types adındaki paketin içeriği aşağıdaki gibi doldurulacak:

  • İndisleri HTML_Color olan HTML_Color_RGB adında bir dizi türü oluşturulacak.
  • İndisleri HTML_Color_RGB türünden To_RGB_Lookup_Table isminde bir sabit dizi oluşturulacak. Bu dizinin her bir öğesi aşağıdaki listede belirtilen değerlere sahip olacak.
+-------------+-----+-------+------+
| Renk | Red | Green | Blue |
+=============+=====+=======+======+
| Salmon | #FA | #80 | #72 |
| Firebrick | #B2 | #22 | #22 |
| Red | #FF | #00 | #00 |
| Darkred | #8B | #00 | #00 |
| Lime | #00 | #FF | #00 |
| Forestgreen | #22 | #8B | #22 |
| Green | #00 | #80 | #00 |
| Darkgreen | #00 | #64 | #00 |
| Blue | #00 | #00 | #FF |
| Mediumblue | #00 | #00 | #CD |
| Darkblue | #00 | #00 | #8B |
+-------------+-----+-------+------+

color_types.ads

package Color_Types is

type HTML_Color is
(Salmon, Firebrick, Red, Darkred, Lime, Forestgreen, Green, Darkgreen,
Blue, Mediumblue, Darkblue);

subtype Int_Color is Integer range 0 .. 255;

type RGB is record
Red : Int_Color;
Green : Int_Color;
Blue : Int_Color;
end record;

function To_RGB (C : HTML_Color) return RGB;

function Image (C : RGB) return String;

type HTML_Color_RGB is array (HTML_Color) of RGB;
To_RGB_Lookup_Table : constant HTML_Color_RGB :=
(Salmon => (16#FA#, 16#80#, 16#72#),
Firebrick => (16#B2#, 16#22#, 16#22#),
Red => (16#FF#, 16#00#, 16#00#), Darkred => (16#8B#, 16#00#, 16#00#),
Lime => (16#00#, 16#FF#, 16#00#),
Forestgreen => (16#22#, 16#8B#, 16#22#),
Green => (16#00#, 16#80#, 16#00#),
Darkgreen => (16#00#, 16#64#, 16#00#),
Blue => (16#00#, 16#00#, 16#FF#),
Mediumblue => (16#00#, 16#00#, 16#CD#),
Darkblue => (16#00#, 16#00#, 16#8B#));

end Color_Types;

color_types.adb

with Ada.Integer_Text_IO;
package body Color_Types is

function To_RGB (C : HTML_Color) return RGB is (To_RGB_Lookup_Table (C));

function Image (C : RGB) return String is
subtype Str_Range is Integer range 1 .. 10;
SR : String (Str_Range);
SG : String (Str_Range);
SB : String (Str_Range);
begin
Ada.Integer_Text_IO.Put (To => SR, Item => C.Red, Base => 16);
Ada.Integer_Text_IO.Put (To => SG, Item => C.Green, Base => 16);
Ada.Integer_Text_IO.Put (To => SB, Item => C.Blue, Base => 16);
return
("(Red => " & SR & ", Green => " & SG & ", Blue => " & SB & ")");
end Image;

end Color_Types;

Derleme ve Test

❯ gnatmake main.adb
gcc -c main.adb
gcc -c color_types.adb
gnatbind -x main.ali
gnatlink main.ali

❯ ./main Color_Table_Chk
Size of HTML_Color_RGB: 11
Firebrick: (Red => 16#B2#, Green => 16#22#, Blue => 16#22#)

❯ ./main HTML_Color_To_Integer_Chk
SALMON => (Red => 16#FA#, Green => 16#80#, Blue => 16#72#).
FIREBRICK => (Red => 16#B2#, Green => 16#22#, Blue => 16#22#).
RED => (Red => 16#FF#, Green => 16#0#, Blue => 16#0#).
DARKRED => (Red => 16#8B#, Green => 16#0#, Blue => 16#0#).
LIME => (Red => 16#0#, Green => 16#FF#, Blue => 16#0#).
FORESTGREEN => (Red => 16#22#, Green => 16#8B#, Blue => 16#22#).
GREEN => (Red => 16#0#, Green => 16#80#, Blue => 16#0#).
DARKGREEN => (Red => 16#0#, Green => 16#64#, Blue => 16#0#).
BLUE => (Red => 16#0#, Green => 16#0#, Blue => 16#FF#).
MEDIUMBLUE => (Red => 16#0#, Green => 16#0#, Blue => 16#CD#).
DARKBLUE => (Red => 16#0#, Green => 16#0#, Blue => 16#8B#).

3. Unconstrained Array

Unconstrained_Array paketi aşağıdaki yönergeler doğrultusunda yazılacak:

  • My_Array dizi türü oluşturulacak. Bu türün indisleri Positive türünden ve elemanları da Integer değerine sahip olacak.
  • Init metodu, parametre olarak verilen dizinin elemanlarının değerini disinin indislerinin tersi şeklinde dolduracak. Örneğin; My_Array(1 .. 5) disinin elemanları baştan başlayarak 5, 4, 3, 2, 1 olacak.
  • Init fonksiyonuna iki adet sınır değeri verilecek. Bu sınır değerleri ile My_Array türünden bir dizi oluşturulacak. Ardından Init metodu ile dizinin içi doldurulacak.
  • Double metodu, kendisine verilen dizinin elemanlarının değerini iki katına çıkaracak.
  • Diff_Prev_Elem fonksiyonu, My_Array türünden verilen dizinin her bir elemanını kendisinden öncekinin farkını alacak. Böylece My_Array türünden başka bir diziyi dolduracak. Ek olarak, ilk eleman her zaman 0 olacak. Örneğin (2, 5, 15) dizisi verilmiş ise Diff_Prev_Elem geriye (0, 3, 10) dizisini gönderecek.

unconstrained_array.ads

package Unconstrained_Arrays is

type My_Array is array (Positive range <>) of Integer;

procedure Init (Arr : in out My_Array);
function Init (I, L : Positive) return My_Array;
procedure Double (Arr : in out My_Array);
function Diff_Prev_Elem (Arr : My_Array) return My_Array;

end Unconstrained_Arrays;

unconstrained_array.adb

package body Unconstrained_Arrays is

procedure Init (Arr : in out My_Array) is
Elem : Natural := Arr'Last;
begin
for I in Arr'Range loop
Arr (I) := Elem;
Elem := Elem - 1;
end loop;
end Init;

function Init (I, L : Positive) return My_Array is
Arr : My_Array (I .. I + L - 1);
begin
Init (Arr);
return Arr;
end Init;

procedure Double (Arr : in out My_Array) is
begin
for I in Arr'Range loop
Arr (I) := Arr (I) * 2;
end loop;
end Double;

function Diff_Prev_Elem (Arr : My_Array) return My_Array is
Temp : My_Array (Arr'Range);
begin
Temp (Temp'First) := 0;

for I in Arr'First + 1 .. Arr'Last loop
Temp (I) := Arr (I) - Arr (I - 1);
end loop;

return Temp;
end Diff_Prev_Elem;

end Unconstrained_Arrays;

Derleme ve Test

❯ gnatmake main.adb
gcc -c main.adb
gcc -c unconstrained_arrays.adb
gnatbind -x main.ali
gnatlink main.ali

❯ ./main Init_Chk
5
4
3
2
1
9
8
7
6
5

❯ ./main Init_Proc_Chk
5
4
3
2
1
9
8
7
6
5

❯ ./main Double_Chk
2
4
10
20
-20

❯ ./main Diff_Prev_Chk
0
1
3
5
-20

❯ ./main Diff_Prev_Single_Chk
0

4. Product Info

Bu örnekte Product_Info_Pkg paketi içerisine aşağıdaki türler, fonksiyonlar ve metotlar eklenecek:

  • Product_Infos, Product_Info türüne ait bilgileri depolamak için kullanılacak olan dizi türü.
  • Currency_Array, Currency türüne ait bilgileri depolamak için kullanılacak olan dizi türü.
  • Total metodu, parametre olarak verilen Product_Infos dizisinin her bir elemanının Unit birimi ile Price birimini çarpıp Currency_Array dizisini dolduracak.
  • Total fonksiyonu, parametre olarak verilen Product_Infos dizisini Total metodu yardımıyla Currency_Array türüne çevirecek ve bunu geri döndürecek.
  • Total fonksiyonu tek bir Currency verisi için yükleme yapılacak.

product_info_pkg.ads

package Product_Info_Pkg is

subtype Quantity is Natural;
subtype Currency is Float;

type Product_Info is record
Units : Quantity;
Price : Currency;
end record;

type Product_Infos is array (Positive range <>) of Product_Info;
type Currency_Array is array (Positive range <>) of Currency;

procedure Total (P : Product_Infos; Tot : out Currency_Array);
function Total (P : Product_Infos) return Currency_Array;
function Total (P : Product_Infos) return Currency;

end Product_Info_Pkg;

product_info_pkg.adb

package body Product_Info_Pkg is

procedure Total (P : Product_Infos; Tot : out Currency_Array) is
begin
for I in P'Range loop
Tot (I) := Currency (P (I).Units) * P (I).Price;
end loop;
end Total;

function Total (P : Product_Infos) return Currency_Array is
T : Currency_Array (P'Range);
begin
Total (P, T);
return T;
end Total;

function Total (P : Product_Infos) return Currency is
T : Currency := 0.0;
begin
for I in P'Range loop
T := T + Currency (P (I).Units) * P (I).Price;
end loop;

return T;
end Total;

end Product_Info_Pkg;

Derleme ve Test

❯ gnatmake main.adb
gcc -c main.adb
gcc -c product_info_pkg.adb
gnatbind -x main.ali
gnatlink main.ali

❯ ./main Total_Func_Chk
0.50
20.00
200.00
100.00
200.00

❯ ./main Total_Proc_Chk
0.50
20.00
200.00
100.00
200.00

❯ ./main Total_Value_Chk
520.50

5. String_10

String_10 paketi içerisinde String türünü kullanarak

  • 10 karakterlik veri içeren String_10 alt türü ve
  • yalnızca 10 karakterlik bir veriyi geri döndüren To_String_10 fonksiyonu yazılacak.

string_10.ads

package Strings_10 is

subtype String_10 is String (1 .. 10);

function To_String_10 (Str : String) return String_10;

end Strings_10;

string_10.adb

package body Strings_10 is

function To_String_10 (Str : String) return String_10 is
S_Out : String_10;

begin
for I in String_10'First .. Integer'Min (String_10'Last, Str'Last) loop
S_Out (I) := Str (I);
end loop;

for I in Str'Last + 1 .. S_Out'Last loop
S_Out (I) := ' ';
end loop;

return S_Out;
end To_String_10;

end Strings_10;

Derleme ve Test

❯ gnatmake main.adb
gcc -c main.adb
gcc -c strings_10.adb
gnatbind -x main.ali
gnatlink main.ali

❯ ./main String_10_Long_Chk
And this i

❯ ./main String_10_Short_Chk
Hey!

--

--