Ciągni znaków i ich dzielenie.

Klawisz95

Użytkownik
Dołączył
Styczeń 24, 2010
Posty
54
Witam!

Piszę sobie właśnie programik który będzie rozpoznawał swój język skryptowy, powiedzmy składnia by była taka:
Kod:
Polecenie(Parametr1,Parametr2,Parametr3)
A przykład to:
Kod:
CosTam('Bla bla bla bla','Tralalalala, buhaha','Hmm')

Chodzi o to - że każdy parametr rozpoznawany jest na zasadzie tego czy jest w apostrofie. Jeżeli jest - oznacza to że jest to ciąg znaków, a jeżeli nie - oznacza liczbę.

Czy jest coś takiego trudne do zrealizowania? Bo ja próbowałem opierając się na analizie każdego znaku osobno (pętla FOR), no ale wyniki - to były pustki. Nic mi nie wyświetłało.

Pozdrawiam ;)


PS: Przed chwilą próbowałem znowu coś wykodzić, ale nici. Przedstawiam wam kod:
Kod:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  StrArray = array of string;
  TForm1 = class(TForm)
    function Decode(const S: String): StrArray;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.Decode(const S: String): StrArray;
var
  SA: StrArray;
  State: Integer;
  I,Counter: Integer;
const
  BRAK=                     $00;
  W_NAWIASIE=               $01;
  W_NAWIASIE_I_APOSTROFIE=  $02;
begin
  SetLength(SA, 255);
  State := $00;
  Counter := 0;

  for I := 0 to Length(S)-1 do
  begin
    if(S[I] = '(') and ((State = $00)) then
    begin
      State := $01;
      Inc(Counter);
      Continue;
    end;
    if(S[I] = ')') and ((State = $01)) then
    begin
      State := $00;
      Inc(Counter);
      Continue;
    end;
    if(S[I] = ',') and ((State = $01)) then
    begin
      Inc(Counter);
      Continue;
    end;
    if(S[I] = '''') and ((State = $01)) then
    begin
      if(State = $02) then
      begin
        State := $01;
        Inc(Counter);
        Continue;
      end; if(State = $01) then
      begin
        State := $02;
        Inc(Counter);
        Continue;
      end;
    end;
    SA[Counter] := SA[Counter]+S[I];
  end;

  SetLength(Result,Counter+1);

  for I := 0 to Counter do
    Result[I] := SA[I];
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  SA: StrArray;
  S: String;
  I: Integer;
begin
  SA := Decode('Powitaj(''Witaj, co u Ciebie?'',''A spi**** szmato'')');

  S := '';
  for I := 0 to High(SA) do
    S := S+Format('SA[%d] = %s',[I, SA[I]])+#10;

  ShowMessage(S);
end;

end.


Znalazłem rozwiązanie!!
Zamieszczam wam rozwiązanie:
Kod:
function TForm1.Decode(const S: String): StrArray;
var
  SA: StrArray;
  I,State,C: Integer;
begin
  SetLength(SA, 255);
  C := 0;
  State := $00;

  for I := 0 to Length(S)-1 do
  begin
    case Ord(S[I]) of
      33..38: SA[C] := SA[C] + S[I];
      42..43: SA[C] := SA[C] + S[I];
      45..126 : SA[C] := SA[C] + S[I];
      32: SA[C] := SA[C] + S[I];
      40: C := C + 1;
      41: Break;
      39: begin
        if (State = $00) then State := $01 else State := $00;
      end;
      44: begin
        if (State = $01) then SA[C] := SA[C] + S[I];
        if (State = $00) then C := C+1;
      end;
    end;
  end;

  SetLength(Result, C+1);
  for I := 0 to C do
    Result[I] := SA[I];
end;

Co prawda nie jest jeszcze takie jak sobie to wymarzyłem, ale jeszcze trochę dopracuję i będzie git ;)
 
Ostatnia edycja:
Do góry Bottom