From: Ruslan R. Nurislamov <hornet@civt.kazan.su>
Organization: Computer Center of the Kazan State University
Решение наверняка неоптимальное, но вроде работает.
-$-
With наилучшими wishes
= HORNET =
---
* Origin: Gated from Relcom by Palantiri NewsGate (2:5030/27.0)
{> Cut here. FileName= STRCMP.PAS }
{ Сравнение строк по шаблону }
{ written by Ruslan Nurislamov }
Var W,S : String;
Function StringCmp(S,Mask:String):Boolean;
Var I,J : Integer;
Ok : Boolean;
St,Msk : String;
Begin
I:=1;
{ Begin optimize mask ('?*','*?','**' secuences must be converted to '*') }
While True Do
Begin
J:=Length(Mask);
While I<Length(Mask) Do
Begin
If (Mask[I]='?') And (Mask[I+1]='*') Then
Delete(Mask,I,1);
If (Mask[I]='*') And (Mask[I+1]='?') And (I<Length(Mask)) Then
Delete(Mask,I+1,1);
If (Mask[I]='*') And (Mask[I+1]='*') And (I<Length(Mask)) Then
Delete(Mask,I,1);
Inc(I);
End;
If J=Length(Mask) Then Break;
I:=1;
End;
{ End of optimize mask convertion }
Ok:=True;
I:=1;
J:=1;
While TRUE Do
Begin
Case Mask[I] Of
{ Begin compareing string with current mask sign }
{ Star sign : any match any simbols }
'*':
Begin
Msk:=Copy(Mask,I+1,Length(Mask)-I+1);
St:=Copy(S,J,Length(S)-J+1);
While (St<>'') And (NOT StringCmp(St,Msk)) Do Delete(St,1,1);
If St='' Then Ok:=False Else J:=Pos(St,S);
End;
{ Question sign : one match any simbol }
'?':
Begin
If (I=Length(Mask)) And (J<Length(S)) Then Ok:=False;
If J>Length(S) Then Ok:=False;
Inc(J);
End;
{ Compare simbols }
Else If Mask[I]<>S[J] Then Ok:=False Else Inc(J);
{ End compareing string with current mask sign }
End;
{ Result of compareing }
If J-1>Length(S) Then Ok:=False;
If Not Ok Then Break;
Inc(I);
If I>Length(Mask) Then Break;
End;
StringCmp:=Ok;
End;
Begin
Write('Mask :');
ReadLn(W);
Write('String :');
ReadLn(S);
WriteLn('Result :',StringCmp(S,W));
End.
|