Einzelnen Beitrag anzeigen

Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#2

Re: Stack: Infix nach Postfix und push und pop

  Alt 14. Mär 2010, 01:58
Hab mal auf die Schnelle die zweite Routine in eine Funktion gepackt und etwas standardkonformer formatiert (aber ungetestet):
Delphi-Quellcode:
function Eval(PostFix: string): integer;
var
  i: integer;
  x: integer;
  c: char;
  procedure NextChar(var c: char);
  begin
    inc(i);
    if i <= length(PostFix) then
      c := PostFix[i]
    else
      c := #0;
  end;
begin
  i := 0;
  repeat
    x := 0;

    repeat
      NextChar(c)
    until c <> ' ';

    case c of
      '*': x := pop*pop;
      '+': x := pop+pop;
      { ... }
    end;

    while (c >= '0') and (c <= '9') do
    begin
      x := 10*x + (ord(c) - ord('0'));
      NextChar(c);
    end;

    push(x);
  until c = #0;
  Result := pop;
end;
Hilft dir das weiter?
  Mit Zitat antworten Zitat