Ada Programlama Dili — Örnekler: Skaler Türler

mozgan
Ada Programlama Dili
4 min readJun 11, 2024
https://demmelearning.com/wp-content/uploads/2020/06/Abacus-and-Algorithm-A-History-of-Math.jpg

1. Colors

Color_Types adında bir paket oluşturulacak, ve içerisinde aşağıdaki sayım türleri ve fonksiyonlar bulunacak:

  • HTML_Color sayım türünün öğeleri: Salmon, Firebrick, Red, Darkred, Lime, Forestgreen, Green, Darkgreen, Blue, Mediumblue, Darkblue.
  • Basic_HTML_Color sayım türünün öğeleri: Red, Green, Blue.
  • To_Integer fonksiyonu HTML_Color türünden bir argüman olacak ve aşağıdaki tabloya göre her bir öğe için Integer türünden bir değer döndürecek:
+-------------+-----------------+
| Renk | HTML kodu (HEX) |
+=============+=================+
| Salmon | #FA8072 |
| Firebrick | #B22222 |
| Red | #FF0000 |
| Darkred | #8B0000 |
| Lime | #00FF00 |
| Forestgreen | #228B22 |
| Green | #008000 |
| Darkgreen | #006400 |
| Blue | #0000FF |
| Mediumblue | #0000CD |
| Darkblue | #00008B |
+-------------+-----------------+
  • To_HTML_Color fonksiyonu ise Basic_HTML_Color türüne ait bir öğeyi HTML_Color türüne çevirip geri döndürecek.

color_types.ads

package Color_Types is

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

type Basic_HTML_Color is (Red, Green, Blue);

function To_Integer (C : HTML_Color) return Integer;
function To_HTML_Color (C : Basic_HTML_Color) return HTML_Color;

private

HTML_Color_Array : array (HTML_Color) of Integer :=
(Salmon => 16#FA_8072#, Firebrick => 16#B2_2222#, Red => 16#FF_0000#,
Darkred => 16#8B_0000#, Lime => 16#00_FF00#, Forestgreen => 16#22_8B22#,
Green => 16#00_8000#, Darkgreen => 16#00_6400#, Blue => 16#00_00FF#,
Mediumblue => 16#00_00CD#, Darkblue => 16#00_008B#);

end Color_Types;

color_types.adb

package body Color_Types is

function To_Integer (C : HTML_Color) return Integer is
(HTML_Color_Array (C));
function To_HTML_Color (C : Basic_HTML_Color) return HTML_Color is
(case C is when Red => Red, when Green => Green, when Blue => Blue);

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 HTML_Color_Range
SALMON
FIREBRICK
RED
DARKRED
LIME
FORESTGREEN
GREEN
DARKGREEN
BLUE
MEDIUMBLUE
DARKBLUE

❯ ./main HTML_Color_To_Integer
16#FA8072#
16#B22222#
16#FF0000#
16#8B0000#
16#FF00#
16#228B22#
16#8000#
16#6400#
16#FF#
16#CD#
16#8B#

❯ ./main Basic_HTML_Color_To_HTML_Color
RED
GREEN
BLUE

2. Integers

Int_Types paketi içerisinde aşağıda açıklanan türler ve bazı fonksiyonlar bulunmakta:

  • I_100 türü 0 ile 100 arasında tanımlı.
  • U_100 modüler türü de 0 ile 100 arasında tanımlı.
  • D_50, I_100‘den türetilen ve değer aralığı 10 ile 50 olan bir tür.
  • E_50 ise I_100‘ün bir alt türü ve değer aralığı da 10 ile 50 arası.
  • To_I_100 fonksiyonu U_100 türüne ait bir parametreyi I_100 türüne çeviriyor.
  • To_U_100 fonksiyonu ise I_100 türüne ait bir parametreyi U_100 türüne çeviriyor.
  • To_D_50 fonksiyonu I_100 türüne ait bir parametreyi D_50 türüne çeviriyor. Şayet alt sınır aşılırsa D_50'First değeri, üst sınır aşılırsa D_50'Last değeri döndürülecek.
  • To_S_50 fonksiyonu I_100 türüne ait bir parametreyi S_50 türüne çeviriyor. Şayet alt sınır aşılırsa S_50'First değeri, üst sınır aşılırsa S_50'Last değeri döndürülecek.
  • To_I_100 ise D_50 türüne ait bir parametreyi I_100 türüne çeviriyor.

int_types.ads

package Int_Types is

type I_100 is range 0 .. 100;
type U_100 is mod 101;
type D_50 is new I_100 range 10 .. 50;
subtype S_50 is I_100 range 10 .. 50;

function To_I_100 (V : U_100) return I_100;
function To_U_100 (V : I_100) return U_100;
function To_D_50 (V : I_100) return D_50;
function To_S_50 (V : I_100) return S_50;
function To_I_100 (V : D_50) return I_100;

end Int_Types;

int_types.adb

package body Int_Types is

function To_I_100 (V : U_100) return I_100 is (I_100 (V));

function To_U_100 (V : I_100) return U_100 is (U_100 (V));

function To_D_50 (V : I_100) return D_50 is
Min : constant I_100 := I_100 (D_50'First);
Max : constant I_100 := I_100 (D_50'Last);
Return_Value : D_50;
begin
if V > Max then
Return_Value := D_50'Last;
elsif V < Min then
Return_Value := D_50'First;
else
Return_Value := D_50 (V);
end if;

return Return_Value;
end To_D_50;

function To_S_50 (V : I_100) return S_50 is
begin

if V > S_50'Last then
return S_50'Last;
elsif V < S_50'First then
return S_50'First;
else
return V;
end if;

end To_S_50;

function To_I_100 (V : D_50) return I_100 is (I_100 (V));

end Int_Types;

Derleme ve Test

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

❯ ./main I_100_Range
0
100

❯ ./main U_100_Range
0
100

❯ ./main U_100_Wraparound
100
0

❯ ./main S_50_Range
10
50

3. Temperatures

Bu örnek, sıcaklık birimleri arasında dönüşümü gerçekleştirecek. Aşağıda yazılacak olan türler ve fonksiyonlar verilmiştir:

  • Celsius, -273.15 ve 5504.85 sınırları arasında tanımlı kayan noktalı tür olarak tanımlanacak.
  • Int_Celsius ise -273 ve 5505 arasında Integer türü olarak tanımlanacak.
  • Kelvin de 0.0 ve 5778.0 arasında kayan noktalı tür olarak tanımlanacak.
  • To_Celsius fonksiyonu Int_Celsius türünden bir argüman alacak ve değerini Celcius olarak döndürecek.
  • To_Int_Celsius ise Celsius türüne ait değerleri Int_Celsius türüne çevirecek.
  • To_Celsius fonksiyonu Kelvin türünden bir parametreyi Celsius türüne çevirecek. Dikkat edilirse burada fonksiyon yüklemesi yapılacak.
  • To_Kelvin fonksiyonu ise Celsius türünü Kelvin olarak çevirecek.

temperature_types.ads

package Temperature_Types is

type Celsius is digits 6 range -273.15 .. 5_504.85;
type Int_Celsius is range -273 .. 5505;
type Kelvin is digits 6 range 0.0 .. 5_778.00;

function To_Celsius (T : Int_Celsius) return Celsius;
function To_Int_Celsius (T : Celsius) return Int_Celsius;
function To_Celsius (T : Kelvin) return Celsius;
function To_Kelvin (T : Celsius) return Kelvin;

end Temperature_Types;

temperature_types.adb

package body Temperature_Types is

function To_Celsius (T : Int_Celsius) return Celsius is
Min : constant Float := Float (Celsius'First);
Max : constant Float := Float (Celsius'Last);
F : constant Float := Float (T);
begin
if F < Min then
return Celsius (Min);
elsif F > Max then
return Celsius (Max);
else
return Celsius (F);
end if;
end To_Celsius;

function To_Int_Celsius (T : Celsius) return Int_Celsius is
(Int_Celsius (T));

function To_Celsius (T : Kelvin) return Celsius is
(Celsius (T - Kelvin (273.15)));

function To_Kelvin (T : Celsius) return Kelvin is
(Kelvin (T + Celsius (273.15)));

end Temperature_Types;

Derleme ve Test

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

❯ ./main Celsius_Range
-2.73150E+02
5.50485E+03

❯ ./main Celsius_To_Int_Celsius
-273
0
5505

❯ ./main Int_Celsius_To_Celsius
-2.73000E+02
0.00000E+00
5.50485E+03

❯ ./main Kelvin_To_Celsius
-2.73150E+02
0.00000E+00
5.50485E+03

❯ ./main Celsius_To_Kelvin
0.00000E+00
5.77800E+03

--

--