type
TUnitBase = (fuKN, fuM);
TUnitStyle = (usNone, usKNdivM3, usKNdivM2, usKNdivM, usKNM, usKN, usM3, usM2, usM);
TUnits = class
private
FItems: array[usNone..usM, fuKN..fuM] of integer;
function GetValues(Index: TUnitStyle): double;
function GetAsString(Index: TUnitStyle): string;
public
property Values[Index: TUnitStyle]: double read GetValues;
property AsString[Index: TUnitStyle]: string read GetAsString;
function GetPlus(const A, B: TUnitStyle): TUnitStyle;
function GetMinus(const A, B: TUnitStyle): TUnitStyle;
function GetDiv(const A, B: TUnitStyle): TUnitStyle;
function GetMult(const A, B: TUnitStyle): TUnitStyle;
constructor Create;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TUnits }
constructor TUnits.Create;
begin
FItems[usNone, fuKN] := 0;
FItems[usNone, fuM] := 0;
FItems[usKNdivM3, fuKN] := 1;
FItems[usKNdivM3, fuM] := -3;
FItems[usKNdivM2, fuKN] := 1;
FItems[usKNdivM2, fuM] := -2;
FItems[usKNdivM, fuKN] := 1;
FItems[usKNdivM, fuM] := -1;
FItems[usKNM, fuKN] := 1;
FItems[usKNM, fuM] := 1;
FItems[usKN, fuKN] := 1;
FItems[usKN, fuM] := 0;
FItems[usM3, fuKN] := 0;
FItems[usM3, fuM] := 3;
FItems[usM2, fuKN] := 0;
FItems[usM2, fuM] := 2;
FItems[usM, fuKN] := 0;
FItems[usM, fuM] := 1;
end;
function TUnits.GetValues(Index: TUnitStyle): double;
begin
Result := IntPower(FItems[Index, fuKN], FItems[Index, fuM]);
end;
function TUnits.GetAsString(Index: TUnitStyle): string;
begin
case Index of
usKNdivM3:
Result := 'kN/m3';
usKNdivM2:
Result := 'kN/m2';
usKNdivM:
Result := 'kN/m';
usKNM:
Result := 'kNm';
usKN:
Result := 'kN';
usM3:
Result := 'm3';
usM2:
Result := 'm2';
usM:
Result := 'm';
else
Result := '';
end;
end;
function TUnits.GetPlus(const A, B: TUnitStyle): TUnitStyle;
begin
if A = B then
Result := A
else
Result := usNone;
end;
function TUnits.GetMinus(const A, B: TUnitStyle): TUnitStyle;
begin
if A = B then
Result := A
else
Result := usNone;
end;
function TUnits.GetMult(const A, B: TUnitStyle): TUnitStyle;
var
I: TUnitStyle;
KN, M: integer;
begin
Result := usNone;
KN := FItems[A, fuKN] + FItems[B, fuKN];
M := FItems[A, fuM] + FItems[B, fuM];
for I := Low(TUnitStyle) to High(TUnitStyle) do
if (KN = FItems[I, fuKN]) and (M = FItems[I, fuM]) then
begin
Result := I;
Break;
end;
end;
function TUnits.GetDiv(const A, B: TUnitStyle): TUnitStyle;
var
I: TUnitStyle;
KN, M: integer;
begin
Result := usNone;
KN := FItems[A, fuKN] - FItems[B, fuKN];
M := FItems[A, fuM] - FItems[B, fuM];
for I := Low(TUnitStyle) to High(TUnitStyle) do
if (KN = FItems[I, fuKN]) and (M = FItems[I, fuM]) then
begin
Result := I;
Break;
end;
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
Units: TUnits;
A, B, C: TUnitStyle;
begin
Units := TUnits.Create;
try
A := usM3;
B := usKNdivM2;
C := Units.GetMult(A, B);
ShowMessage(Units.AsString[C]);
A := usKN;
B := usM;
C := Units.GetDiv(A, B);
ShowMessage(Units.AsString[C]);
finally
Units.Free;
end;
end;