Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

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

Re: Stack: Infix nach Postfix und push und pop

  Alt 18. Mär 2010, 23:10
Dabei macht der Code doch eigentlich nichts Anderes?
Außer daß die Schittstellen der Ein- und Ausgabe umgestellt wurde.

Statt READ wird jetzt nacheinander ein Zeichen aus dem String genommen
und anstatt WRITE wird der Text an den Result-String angehängt.
OK, für ein nachfolgendes EOL wird nun vorher die aktuelle Zeichenpostition über LENGTH geprüft.

// dein Code aus Post #1
{} nachfolgend jeweils der entsprechende Befehl aus Pst #10

Delphi-Quellcode:
function Infix2Postfix(const S: String): String;
var
  i: Integer;
begin
  Result := '';
//Init;
{}init;
//repeat
{}i := 1;
{}while i <= Length(S) do begin
  //repeat read(c) until c <> ' ';
  {}if S[i] <> ' then begin
    //if c = ')' then write(chr(pop));
    {}if S[i] = ')then Result := Result + chr(pop);
    //if c = '+' then push(ord(c));
    {}if S[i] = '+then push(ord(S[i]));
    //if c = '*' then push(ord(c));
    {}if S[i] = '*then push(ord(S[i]));
    //while (c >='0') and (c<='9') do
    // begin write(c), read(c); end;
    {}if (S[i] >= '0') and (S[i] <= '9') then begin
    {}  Result := Result + S[i];
    {}  while (i < Length(S)) and (S[i + 1] >= '0') and (S[i + 1] <= '9') do begin
    {}    Result := Result + S[i];
    {}    Inc(i);
    {}  end;
    {}end;
    //if c <> '(' then write (' ');
    {}if (i <= Length(S)) and (S[i] <> '(') then Result := Result + ' ';
  {}end;
//until eoln;
{}  Inc(i);
{}end;
end;

function CalcPostfix(const S: String): String;
var
  i, x: Integer;
begin
  Result := '';
//Init;
{}init;
//repeat
{}i := 1;
{}while i <= Length(S) do begin
  //x := 0;
  //repeat read(c) until c <> ' ';
  {}if S[i] <> ' then begin
  {}  x := 0;
    //if c = '*' then x := pop*poop;
    {}if S[i] = '*then x := pop * pop;
    //if c = '+' then x := pop+pop;
    {}if S[i] = '+then x := pop + pop;
    //while (c >= '0') and (c <= '9') do
    //begin
    // x := 10*x+(ord(c)-ord('0'));
    // read(c);
    //end;
    {}if (S[i] >= '0') and (S[i] <= '9') then begin
    {}  x := ord(S[i]) - ord('0');
    {}  while (i < Length(S)) and (S[i + 1] >= '0') and (S[i + 1] <= '9') do begin
    {}    x := 10 * x + (ord(S[i]) - ord('0'));
    {}    Inc(i);
    {}  end;
    {}end;
    //push(x);
    {}push(x);
  {}end;
//until eoln;
{}  Inc(i);
{}end;
//writeln(pop);
{}Result := Result + IntToStr(pop);
end;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat