Ada Programlama Dili — Örnekler: Paketler

mozgan
Ada Programlama Dili
3 min readJun 10, 2024
https://kulturveyasam.com/wp-content/uploads/2024/02/bal_petekleri_neden_altigen_03.jpg

1. Months

Bu örnekte, ayları karakter dizisi olarak ekrana basan Display_Months adındaki bir metodu Months paketi içerisinde yazılması isteniyor.

months.ads

package Months is

procedure Display_Months;

private

Jan : constant String := "January";
Feb : constant String := "February";
Mar : constant String := "March";
Apr : constant String := "April";
May : constant String := "May";
Jun : constant String := "June";
Jul : constant String := "July";
Aug : constant String := "August";
Sep : constant String := "September";
Oct : constant String := "October";
Nov : constant String := "November";
Dec : constant String := "December";

end Months;

months.adb

with Ada.Text_IO; use Ada.Text_IO;

package body Months is

procedure Display_Months is
begin
Put_Line ("Months:");
Put_Line ("- " & Jan);
Put_Line ("- " & Feb);
Put_Line ("- " & Mar);
Put_Line ("- " & Apr);
Put_Line ("- " & May);
Put_Line ("- " & Jun);
Put_Line ("- " & Jul);
Put_Line ("- " & Aug);
Put_Line ("- " & Sep);
Put_Line ("- " & Oct);
Put_Line ("- " & Nov);
Put_Line ("- " & Dec);
end Display_Months;

end Months;

Derleme ve Test

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

❯ ./main Months_Chk
Months:
- January
- February
- March
- April
- May
- June
- July
- August
- September
- October
- November
- December

2. Operations

Operations paketi içerisinde Integer türü için Add, Subtract, Multiply ve Divide fonksiyonları bulunacak. Bunun yanında Operations.Test alt paketi içerisinde bulunan Display metodu tamamlanacak.

operations.ads

package Operations is

function Add (A, B : Integer) return Integer;
function Subtract (A, B : Integer) return Integer;
function Multiply (A, B : Integer) return Integer;
function Divide (A, B : Integer) return Integer;

end Operations;

operations.adb

package body Operations is

function Add (A, B : Integer) return Integer is (A + B);
function Subtract (A, B : Integer) return Integer is (A - B);
function Multiply (A, B : Integer) return Integer is (A * B);
function Divide (A, B : Integer) return Integer is (A / B);

end Operations;

operations-test.ads

package Operations.Test is

procedure Display (A, B : Integer);

end Operations.Test;

operations-test.adb

with Ada.Text_IO; use Ada.Text_IO;

package body Operations.Test is

procedure Display (A, B : Integer) is
A_Str : constant String := Integer'Image (A);
B_Str : constant String := Integer'Image (B);
begin
Put_Line ("Operations:");
Put_Line
(A_Str & " + " & B_Str & " = " & Integer'Image (Add (A, B)) & ",");
Put_Line
(A_Str & " - " & B_Str & " = " & Integer'Image (Subtract (A, B)) &
",");
Put_Line
(A_Str & " * " & B_Str & " = " & Integer'Image (Multiply (A, B)) &
",");
Put_Line
(A_Str & " / " & B_Str & " = " & Integer'Image (Divide (A, B)) & ",");
end Display;

end Operations.Test;

Derleme ve Test

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

❯ ./main Operations_Chk
Add (100, 2) = 102
Subtract (100, 2) = 98
Multiply (100, 2) = 200
Divide (100, 2) = 50

❯ ./main Operations_Display_Chk
Operations:
10 + 5 = 15,
10 - 5 = 5,
10 * 5 = 50,
10 / 5 = 2,
Operations:
1 + 2 = 3,
1 - 2 = -1,
1 * 2 = 2,
1 / 2 = 0,

Soruların Aslı

  1. Months
  2. Operations

--

--