Einzelnen Beitrag anzeigen

Dejan Vu
(Gast)

n/a Beiträge
 
#8

AW: String nach bestimmten muster auflösen

  Alt 20. Okt 2015, 06:30
Hier mal Pseudocode.
Delphi-Quellcode:
procedure Parse;
begin
  While GetNextCommand(command) do
    Execute(command);
end;

Function GetNextCommand(var cmd : TCommand) : Boolean;
var
  c : Char;

Begin
   if AtEof Then Exit(False)
   c := TakeChar;
   if c=')then Exit(False);
   if not (c in allowedCommands) then raise Exception.Create('Invalid command');
   cmd := TCommand.Create;
   cmd.Command := c;
   cmd.Value := TakeNumber;
   if (cmd.Command = RepeatCmd) then
     if TakeChar<>'(then Raise Exception.Create('Expected "("')
   result := True;
End;

Procedure Execute (aCmd : TCommand);
Begin
  case aCmd.Command of
    RepeatCmd : Parse;
    ForwardCmd : GoForward (aCmd.Value);
    RightCmd : TurnRight(aCmd.Value);
    ...
  end
end;

Function AtEof : Boolean;
Begin
  result := CurrentPtr>Length(InputString);
End;

Function TakeChar : Char;
Begin
  result := InputString[CurrentPtr];
  inc(CurrentPtr);
End;

Function TakeNumber : Integer;
Var
  p : Integer;

Begin
  p := CurrentPtr;
  While TakeChar in Digits and Not AtEof;
  if p=CurrentPtr then Raise Exception.Create('Not a number');
  result := StrToInt(Copy(InputString,p,CurrentPtr-p));
end;

Procedure Run (aString : String);
Begin
  InputString := aString;
  CurrentPtr := 1;
  Parse;
End;
Ganz grob skizziert.
  Mit Zitat antworten Zitat