Jump to content

Set: Difference between revisions

796 bytes removed ,  5 years ago
(→‎{{header|VBA}}: Collection as set)
Line 36:
An alternative hash-based solution could use the Hashed_Maps package from Ada.Containers.
 
<lang Ada>with Adaada.Text_IOcontainers.ordered_sets, Ada.Containersada.Ordered_Setstext_io;
use ada.text_io;
 
procedure Set_Demoset_demo is
package cs is new ada.containers.ordered_sets (character); use cs;
 
function "+" (s : string) return set is
package CS is new Ada.Containers.Ordered_Sets(Character); use CS;
(if s = "" then empty_set else Union(+ s(s'first..s'last - 1), To_Set (s(s'last))));
package IO renames Ada.Text_IO;
 
function "-" (s : Set) return string is
-- helper functions for string to something conversion, and vice versa
(if s = empty_set then "" else - function(s - To_Set(S: String(s.last_element)) return& Set iss.last_element);
s1, s2 : set;
Result: Set;
begin
loop
for I in S'Range loop
put ("s1= ");
begin
s1 := + get_line;
Result.Insert(S(I));
exit when S1s1 = To_Set(+"quitQuit!");
-- raises Constraint_Error if S(I) is already in Result
put ("s2= ");
exception
s2 := + get_line;
when Constraint_Error => null;
Put_Line("Sets [" & (-s1) & "], [" & (-s2) & "] of end;size"
& Image(S1.Length)'img & " and" & Image(S2s2.Length)'img & ".");
end loop;
IO. Put_Line("Intersection: [" & Image(-(Intersection(S1, S2))) & "],");
return Result;
IO. Put_Line("Union: [" & Image(-(Union(S1s1, S2s2))) & "],");
end To_Set;
IO. Put_Line("Difference: [" & Image(-(Difference(S1s1, S2s2))) & "],");
 
IO. Put_Line("Symmetric Diff: [" & Image(S1-(s1 xor S2s2)) & "],");
function Image(S: Set) return String is
IO. Put_Line("Subset: " & Boolean'Image(S1s1.Is_Subset(S2s2))
C: Character;
T: Set & ", Equal: " & Boolean'Image(s1 = Ss2) & ".");
end loop;
begin
end set_demo;
if T.Is_Empty then
return "";
else
C:= T.First_Element;
T.Delete_First;
return C & Image(T);
end if;
end Image;
 
function Image(C: Ada.Containers.Count_Type) return String renames
Ada.Containers.Count_Type'Image;
 
S1, S2: Set;
begin -- main program
loop
S1 := To_Set(Ada.Text_IO.Get_Line);
exit when S1 = To_Set("quit!");
S2 := To_Set(Ada.Text_IO.Get_Line);
IO.Put_Line("Sets [" & Image(S1) & "], [" & Image(S2) & "] of size"
& Image(S1.Length) & " and" & Image(S2.Length) & ".");
IO.Put_Line("Intersection: [" & Image(Intersection(S1, S2)) & "],");
IO.Put_Line("Union: [" & Image(Union(S1, S2)) & "],");
IO.Put_Line("Difference: [" & Image(Difference(S1, S2)) & "],");
IO.Put_Line("Symmetric Diff: [" & Image(S1 xor S2) & "],");
IO.Put_Line("Subset: " & Boolean'Image(S1.Is_Subset(S2))
& ", Equal: " & Boolean'Image(S1 = S2) & ".");
end loop;
end Set_Demo;
</lang>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.