1
0
lazarus-tutorials/set_demo/dualdigitsset.pas

96 lines
2.0 KiB
ObjectPascal
Raw Normal View History

2021-10-13 15:50:09 -04:00
unit dualdigitsset;
{$mode objfpc}{$H+}
interface
uses
SysUtils;
type
TDigits = 0..9;
TDigitSet = set of TDigits;
{ TDualAZSet }
TDualAZSet = class
private
FsetA, FsetZ: TDigitSet;
function GetDiffAZAsText: string;
function GetDiffZAAsText: string;
function GetIntersectionAsText: string;
function GetSetAasText: string;
function GetSetZasText: string;
function GetSymDiffAsText: string;
function GetUnionAsText: string;
function SetAsString(s: TDigitSet): string;
public
property DiffAZAsText: string read GetDiffAZAsText;
property DiffZAAsText: string read GetDiffZAAsText;
property IntersectionAsText: string read GetIntersectionAsText;
property SetA: TDigitSet read FsetA write FsetA;
property SetAasText: string read GetSetAasText;
property SetZ: TDigitSet read FsetZ write FsetZ;
property SetZasText: string read GetSetZasText;
property SymDiffAsText: string read GetSymDiffAsText;
property UnionAsText: string read GetUnionAsText;
end;
implementation
{ TDualAZSet }
function TDualAZSet.GetDiffAZAsText: string;
begin
Result := SetAsString(FsetA - FsetZ);
end;
function TDualAZSet.GetDiffZAAsText: string;
begin
Result := SetAsString(FsetZ - FsetA);
end;
function TDualAZSet.GetIntersectionAsText: string;
begin
Result := SetAsString(FsetA * FsetZ);
end;
function TDualAZSet.GetSetAasText: string;
begin
Result := SetAsString(FsetA);
end;
function TDualAZSet.GetSetZasText: string;
begin
Result := SetAsString(FsetZ);
end;
function TDualAZSet.GetSymDiffAsText: string;
begin
Result := SetAsString(FsetA + FsetZ - FsetA * FsetZ);
end;
function TDualAZSet.GetUnionAsText: string;
begin
Result := SetAsString(FsetA + FsetZ);
end;
function TDualAZSet.SetAsString(s: TDigitSet): string;
var d: TDigits;
begin
Result := EmptyStr;
for d in TDigitSet do
if (d in s) then
begin
if Length(Result) > 0
then AppendStr(Result, ',');
AppendStr(Result, IntToStr(d));
end;
Result := Format('[%s]', [Result]);
end;
end.