Thema: Delphi Einheiten parsen

Einzelnen Beitrag anzeigen

Bjoerk

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

AW: Einheiten parsen

  Alt 12. Mär 2015, 20:24
Was ich aber nicht hinkrieg ist daraus einen Stack zu machen?

Delphi-Quellcode:
function TJMUnitParser.Solve(S: string): TParserUnitStyle;
begin
  try
    if LastPos('+', S) > 0 then
      Result := FUnits.GetAdd(Solve(Left(S, '+', true)), Solve(Right(S, '+', true)))
    else
      if LastPos('/', S) > 0 then
        Result := FUnits.GetMult(Solve(Left(S, '/', true)), Solve(Right(S, '/', true)))
      else
        if LastPos('*', S) > 0 then
          Result := FUnits.GetDiv(Solve(Left(S, '*', true)), Solve(Right(S, '*', true)))
        else
          if FirstPos('^', S) > 0 then
            Result := FUnits.GetPower(Solve(Left(S, '^', false)), Solve(Right(S, '^', false)))
          else
            if Parenthesis(S) then
              Result := Solve(S)
            else
              Result := FUnits.SignToStyle(S);
  except
    Result := pusNone;
  end;
end;
In meinem jugendlichen Leichtsinn hab ich mal dahin überlegt? Geht das so in der Richtung?
Delphi-Quellcode:
function TJMUnitParser.Solve(const Term: string): TParserUnitStyle;
var
  Stack: TParserStack;
begin
  Result := pusNone;
  Stack := TParserStack.Create;
  try
    Stack.Push(Term);
    while not Stack.Empty do
    begin
      S := Stack.Pop.S;
      if LastPos('+', S) > 0 then
      begin
        Result :=
        Stack.Push(S);
      end
      else
        if LastPos('/', S) > 0 then
        begin
          Result :=
          Stack.Push(S);
        end
        else
          if LastPos('*', S) > 0 then
          begin
            Result :=
            Stack.Push(S);
          end
          else
            if Parenthesis(S) then
            begin
              Result :=
              Stack.Push(S);
            end
            else
              Result := FUnits.SignToStyle(S);
    end;
  finally
    Stack.Free;
  end;
end;
  Mit Zitat antworten Zitat