AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Stack: Infix nach Postfix und push und pop

Ein Thema von Luckie · begonnen am 14. Mär 2010 · letzter Beitrag vom 20. Mär 2010
 
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.372 Beiträge
 
Delphi 12 Athens
 
#7

Re: Stack: Infix nach Postfix und push und pop

  Alt 14. Mär 2010, 08:41
Zitat:
Wenn ich da 56+ eingebe gibt er mir 0 aus.
Du müßtest dort '5 6 +' eingeben ... halt so wie '123 456 +', denn so wäre es als Infix '56 + unbekannt' und dieser nicht grade fehlerresistente/fehlererkennende Code reagiert da offenbar nicht sehr optimal.

Zitat von Luckie:
Mut dem Push und Pop Routinen habe ich noch Verständnis Probleme, was da passiert.
http://de.wikipedia.org/wiki/Stapelspeicher

Push fügst einen Wert an oben in einer Liste ein und Pop holt den obersten Wert wieder aus der Lieste raus.


Als Funktion umgestellt könnte es eventuell so aussehn:
Delphi-Quellcode:
function Infix2Postfix(const S: String): String;
var
  i: Integer;
begin
  Result := '';
  init;
  i := 1;
  while i <= Length(S) do begin
    if S[i] <> ' then begin
      if S[i] = ')then Result := Result + chr(pop);
      if S[i] = '+then push(ord(S[i]));
      if S[i] = '*then push(ord(S[i]));
      while (i <= Length(S)) and (S[i] >= '0') and (S[i] <= '9') do begin
        Result := Result + c;
        Inc(i);
      end;
      if (i <= Length(S)) and (S[i] <> '(') then Result := Result + ' ';
    end;
    Inc(i);
  end;
end;
Delphi-Quellcode:
function CalcPostfix(const S: String): String;
var
  i, x: Integer;
begin
  Result := '';
  init;
  i := 1;
  while i <= Length(S) do begin
    if S[i] <> ' then begin
      x := 0;
      if S[i] = '*then x := pop * pop;
      if S[i] = '+then x := pop + pop;
      while (i <= Length(S)) and (S[i] >= '0') and (S[i] <= '9') do begin
        x := 10 * x + (ord(S[i]) - ord('0'));
        Inc(i);
      end;
      push(x);
    end;
    Inc(i);
  end;
  Result := Result + IntToStr(pop);
end;
und noch ein bissl umgestellt käme z.B. sowas raus:
Delphi-Quellcode:
function Infix2Postfix(const S: String): String;
var
  i: Integer;
begin
  Result := '';
  init;
  for i := 1 to Length(S) do
    case S[i] of
      ')': Result := Result + chr(pop);
      '+': push(ord(S[i]));
      '*': push(ord(S[i]));
      '0'..'9': Result := Result + c;
    end;
end;
Delphi-Quellcode:
function CalcPostfix(const S: String): String;
var
  i, x: Integer;
begin
  Result := '';
  init;
  i := 1;
  while i <= Length(S) do begin
    x := 0;
    case S[i] of
      '*': x := pop * pop;
      '+': x := pop + pop;
      '0'..'9': begin
        x := ord(S[i]) - ord('0');
        while (i < Length(S)) and (S[i + 1] >= '0') and (S[i + 1] <= '9') do begin
          Inc(i);
          x := 10 * x + (ord(S[i]) - ord('0'));
        end;
      end;
    end;
    push(x);
    Inc(i);
  end;
  Result := Result + IntToStr(pop);
end;

[ot]
Jetzt bastelt auch noch der Luckie an einem Taschenrechner.
Wann kommt denn der BF-Interpreter?
Ein Therapeut entspricht 1024 Gigapeut.
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:35 Uhr.
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