Delphi-PRAXiS
Seite 5 von 5   « Erste     345   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi Einheiten parsen (https://www.delphipraxis.net/184211-einheiten-parsen.html)

Bjoerk 14. Mär 2015 19:20

AW: Einheiten parsen
 
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:43 Uhr.
Seite 5 von 5   « Erste     345   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz