Vektoren, Maps und Sets
Die Standardbibliothek Ada.Containers bietet generische Datenstrukturen:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Containers.Ordered_Sets;
procedure Main is
package Int_Vektor is new Ada.Containers.Vectors (Index_Type => Positive, Element_Type => Integer);
package Alter_Map is new Ada.Containers.Indefinite_Ordered_Maps (String, Natural);
package Int_Set is new Ada.Containers.Ordered_Sets (Integer);
package Sortierer is new Int_Vektor.Generic_Sorting;
use Int_Vektor;
V : Vector;
M : Alter_Map.Map;
S : Int_Set.Set;
Summe : Integer := 0;
begin
V.Append (5); V.Append (3); V.Append (9);
V.Prepend (1);
Put_Line (Ada.Containers.Count_Type'Image (V.Length) & Integer'Image (V.First_Element) & Integer'Image (V.Last_Element));
for X of V loop
Summe := Summe + X;
end loop;
Put_Line (Integer'Image (Summe));
Sortierer.Sort (V);
for X of V loop Put (Integer'Image (X)); end loop;
New_Line;
M.Insert ("Mia", 17);
M.Insert ("Tom", 25);
M.Include ("Mia", 18);
for C in M.Iterate loop
Put_Line (Alter_Map.Key (C) & " =" & Natural'Image (Alter_Map.Element (C)));
end loop;
Put_Line (Boolean'Image (M.Contains ("Tom")) & Boolean'Image (M.Contains ("Zoe")));
S.Insert (3); S.Insert (1); S.Include (3); S.Insert (2);
Put_Line (Ada.Containers.Count_Type'Image (S.Length));
for X of S loop Put (Integer'Image (X)); end loop;
New_Line;
end Main;Ausgabe
4 1 9 18 1 3 5 9 Mia = 18 Tom = 25 TRUEFALSE 3 1 2 3
Textdateien
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
F : File_Type;
Pfad : constant String := "ada-demo.txt";
Zeilen : Natural := 0;
begin
Create (F, Out_File, Pfad);
Put_Line (F, "eins");
Put_Line (F, "zwei");
Put_Line (F, "drei");
Close (F);
Open (F, In_File, Pfad);
while not End_Of_File (F) loop
declare
Zeile : constant String := Get_Line (F);
begin
Zeilen := Zeilen + 1;
Put_Line (Natural'Image (Zeilen) & ": " & Zeile);
end;
end loop;
Close (F);
Open (F, Append_File, Pfad);
Put_Line (F, "vier");
Close (F);
Open (F, In_File, Pfad);
Zeilen := 0;
while not End_Of_File (F) loop
declare
Dummy : constant String := Get_Line (F);
begin
Zeilen := Zeilen + 1;
end;
end loop;
Close (F);
Put_Line (Natural'Image (Zeilen));
Open (F, In_File, Pfad);
Delete (F);
exception
when Name_Error => Put_Line ("Datei nicht gefunden");
end Main;Ausgabe
1: eins 2: zwei 3: drei 4
Zeit und Kalender
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting;
with Ada.Calendar.Arithmetic;
procedure Main is
T : constant Time := Time_Of (2024, 3, 15, 14 * 3600.0 + 30 * 60.0);
Spaeter : constant Time := T + Duration (86_400);
Tage : Ada.Calendar.Arithmetic.Day_Count;
Sekunden : Duration;
Schalt : Ada.Calendar.Arithmetic.Leap_Seconds_Count;
begin
Put_Line (Integer'Image (Year (T)) & Integer'Image (Month (T)) & Integer'Image (Day (T)));
Put_Line (Ada.Calendar.Formatting.Image (T));
Put_Line (Ada.Calendar.Formatting.Image (Spaeter, Include_Time_Fraction => False));
Ada.Calendar.Arithmetic.Difference (Spaeter, T, Tage, Sekunden, Schalt);
Put_Line (Integer'Image (Integer (Tage)) & " Tag(e)");
Put_Line (Ada.Calendar.Formatting.Day_Of_Week (T)'Image);
delay 0.05;
Put_Line ("Wartezeit vorbei");
end Main;Ausgabe
2024 3 15 2024-03-15 14:30:00 2024-03-16 14:30:00 1 Tag(e) FRIDAY Wartezeit vorbei
Umgebung und Zufall
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line;
with Ada.Environment_Variables;
with Ada.Numerics.Discrete_Random;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
procedure Main is
subtype Wuerfel is Integer range 1 .. 6;
package Zufall is new Ada.Numerics.Discrete_Random (Wuerfel);
G : Zufall.Generator;
Wurf : Wuerfel;
begin
Put_Line (Integer'Image (Ada.Command_Line.Argument_Count));
Put_Line (Boolean'Image (Ada.Environment_Variables.Exists ("PATH")));
Zufall.Reset (G, 42);
Wurf := Zufall.Random (G);
Put_Line (Boolean'Image (Wurf in 1 .. 6));
Put_Line (Float'Image (Sqrt (16.0)));
Put_Line (Float'Image (Float'Floor (2.7)));
Put_Line (Float'Image (Float'Rounding (2.5)));
Put_Line (Float'Image (Log (100.0, 10.0)));
end Main;Ausgabe
0 TRUE TRUE 4.00000E+00 2.00000E+00 3.00000E+00 2.00000E+00
Merke
Ada.ContainersliefertVectors,Maps,Setsund mehr als generische Pakete- Dateien:
Create,Open,Get_Line,Put_Line,Close,End_Of_File Ada.CalendarundAda.Calendar.Formattingrechnen mit Datum und ZeitAda.Numerics.*bietet Zufall und Mathematikfunktionen
Aufgabe
Lies Zahlen aus einer Datei in einen Vektor ein und sortiere sie.