现在的位置: 首页 > 综合 > 正文

顺序查找

2018年02月06日 ⁄ 综合 ⁄ 共 648字 ⁄ 字号 评论关闭
type
  PStringArray = array of String;

// sequential search
function SequenSearch(aStrs: PStringArray; aCount: Integer;
            const aName: string): Integer;
var
  i: Integer;
begin
  for i := 0 to pred(aCount) do
    if CompareText(aStrs[i], aName) = 0 then
    begin
      Result := i;
      Exit;
    end;
    Result := -1;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  str: String;
  value: Integer;
  i: Integer;
  PStringArr: PStringArray;
begin
  str := Edit1.Text;
  SetLength(PStringArr, ListBox1.Items.Count);
  for i := 0 to ListBox1.Items.Count - 1 do
  begin
    PStringArr[i] := ListBox1.Items[i];
  end;
  value := SequenSearch(PStringArr, ListBox1.Items.Count, str);
  if value <> -1 then
    ShowMessage('Congregate you! You find it')
  else
    ShowMessage('Sorry! You don''t find it');
 end;

抱歉!评论已关闭.