Thema: Delphi Einheiten parsen

Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#41

AW: Einheiten parsen

  Alt 14. Mär 2015, 20:20
Genau so ist es.

"3 kN/m * 4 kN / 3 m" = [KeineEinheit][kN/m] * [KeineEinheit][kN] / [KeineEinheit][m]

[KeineEinheit][IrgendeineEinheit] ersetzen wir zu [IrgendeineEinheit]

-> [kN/m] * [kN] / [m]

Wenn an einer Operation [KeineEinheit] beteiligt ist, dann ist das Ergebnis dieser Operation die andere Einheit.

Delphi-Quellcode:
function TParserUnits.Valid(const Value: TParserUnitStyle): boolean;
begin
  Result := Value <> pusNone; // pusNone = Ergebnis einer inkompatiblen Operation
end;

function TParserUnits.Default(const Value: TParserUnitStyle): boolean;
begin
  Result := Value = pusDefault; // pusDefault = Keine Einheit;
end;

function TParserUnits.GetAdd(const A, B: TParserUnitStyle): TParserUnitStyle;
begin
  Result := pusNone;
  if Valid(A) and Valid(B) then
  begin
    if A = B then
      Result := A
    else
      if Default(A) then
        Result := B
      else
        if Default(B) then
          Result := A;
  end;
end;

function TParserUnits.GetMult(const A, B: TParserUnitStyle): TParserUnitStyle;
var
  Style: TParserUnitStyle;
  KN, M: integer;
begin
  Result := pusNone;
  if Valid(A) and Valid(B) then
  begin
    if Default(A) then
      Result := B
    else
      if Default(B) then
        Result := A
      else
      begin
        KN := FItems[A, pubKN] + FItems[B, pubKN];
        M := FItems[A, pubM] + FItems[B, pubM];
        for Style := pusDefault to pusM do
          if (KN = FItems[Style, pubKN]) and (M = FItems[Style, pubM]) then
            Result := Style;
      end;
  end;
end;

function TParserUnits.GetDiv(const A, B: TParserUnitStyle): TParserUnitStyle;
var
  Style: TParserUnitStyle;
  KN, M: integer;
begin
  Result := pusNone;
  if Valid(A) and Valid(B) then
  begin
    if Default(A) then
      Result := B
    else
      if Default(B) then
        Result := A
      else
      begin
        KN := FItems[A, pubKN] - FItems[B, pubKN];
        M := FItems[A, pubM] - FItems[B, pubM];
        for Style := pusDefault to pusM do
          if (KN = FItems[Style, pubKN]) and (M = FItems[Style, pubM]) then
            Result := Style;
      end;
  end;
end;

Geändert von Bjoerk (14. Mär 2015 um 20:31 Uhr) Grund: Schreibfehler
  Mit Zitat antworten Zitat