TKddRec和TtestRec为自定义结构体类型procedure TForm1.Button5Click(Sender: TObject);
var
  i,j,k,m,intisoland,intk,intRecCount:integer;
  s,sNeighbors,sAntiNeighbors:TStringList; //s:所有记录的id号,sNeighbors:该点最邻近点集合,sAntiNeighbors:以该点为最邻近点集合
  inttop,intvalue,intbuttom:real;
  d,dis:real;                              //d:给定域值 dis:两条记录间的距离
  dsthostcount,arrdsthostcount:TKddRec;
  f:TextFile;
  strText,strid,strText1,strIntrusion:String;           //strText1:每个聚类中所含有的记录的id号
  disd0,disd1,disd2:Real; //相同字段相减
  onePoint:^TtestRec;                      //一条记录的五元集
  recStack:TStack;
  intID,intNeiID,intAntiNeiID,Index,intCount,intsum:Integer;
  onerecord:array[0..1000]of TKddRec;     //记录数组
  testRecord:array[0..1000]of TtestRec;   //每个记录的最近邻相关信息
begin
 try
 //取得所有记录的id号
  s:=TStringList.Create;
  d:=strtofloat(edit2.Text);
  intisoland:=0;
  adosel.Close;
  adosel.SQL.Clear;
  adosel.SQL.Text:='select id from '+Trim(edit1.Text);
  adosel.Open;
  intRecCount:=adosel.RecordCount;
  for i:=0 to intRecCount-1 do
  begin
    testRecord[i].Dir:=adosel.Fields.Fields[0].AsInteger;
    if i<>adosel.RecordCount-1 then
      strid:=strid+adosel.Fields.Fields[0].AsString+','
    else
      strid:=strid+adosel.Fields.Fields[0].AsString;
    adosel.Next;
  end;
  s.DelimitedText:=strid;
  s.Delimiter:=',';
  adosel.close;
  adosel.SQL.Clear;
  adosel.SQL.Text:='select * from '+Trim(edit1.Text);
  adosel.Open;
  adosel.First;
  for i:=0 to adosel.RecordCount-1 do
  begin
    intvalue:=adosel.FieldByName('dst_host_count').AsFloat;
    inttop:=abs(intvalue)+d;   //最大值
    intbuttom:=abs(intvalue)-d;//最小值
    dsthostcount.rdid:=adosel.FieldByName('id').AsInteger; //0
    dsthostcount.element_con[0]:=adosel.FieldByName('duration').AsFloat;
    dsthostcount.element_con[1]:=adosel.FieldByName('count').AsFloat;
    dsthostcount.element_con[2]:=adosel.FieldByName('srv_count').AsFloat;
    dsthostcount.element_sym[0]:=adosel.FieldByName('land').AsInteger;
    dsthostcount.element_sym[1]:=adosel.FieldByName('logged_in').AsInteger;
    //dsthostcount.element_sym[2]:=adosel.FieldByName('root_shell').AsInteger;
    dsthostcount.element_sym[3]:=adosel.FieldByName('is_host_login').AsInteger;
    dsthostcount.element_sym[4]:=adosel.FieldByName('is_guest_login').AsInteger;
    dsthostcount.ele_proto:=adosel.FieldByName('protocol_type').AsString;      
    dsthostcount.ele_serv:=adosel.FieldByName('service').AsString;            
    dsthostcount.ele_flag:=adosel.FieldByName('flag').AsString;           
    adoavg.Close;
    adoavg.SQL.Clear;
    adoavg.SQL.Text:='select * from '+Trim(edit1.Text)+' where dst_host_count>='''+floattostr(intbuttom)+''' and dst_host_count<='''+floattostr(abs(inttop))+'''';
    //showmessage(adoavg.SQL.Text);
    adoavg.Open;
    adoavg.First;
    for j:=0 to adoavg.RecordCount-1 do
    begin
      arrdsthostcount.rdid:=adoavg.FieldByName('id').AsInteger;
      arrdsthostcount.element_con[0]:=adoavg.FieldByName('duration').AsFloat;
      arrdsthostcount.element_con[1]:=adoavg.FieldByName('count').AsFloat;
      arrdsthostcount.element_con[2]:=adoavg.FieldByName('srv_count').AsFloat;
      arrdsthostcount.element_sym[0]:=adoavg.FieldByName('land').AsInteger;
      arrdsthostcount.element_sym[1]:=adoavg.FieldByName('logged_in').AsInteger;
      //arrdsthostcount.element_sym[2]:=adoavg.FieldByName('root_shell').AsInteger;
      arrdsthostcount.element_sym[3]:=adoavg.FieldByName('is_host_login').AsInteger;
      arrdsthostcount.element_sym[4]:=adoavg.FieldByName('is_guest_login').AsInteger;
      arrdsthostcount.ele_proto:=adoavg.FieldByName('protocol_type').AsString;
      arrdsthostcount.ele_serv:=adoavg.FieldByName('service').AsString;
      arrdsthostcount.ele_flag:=adoavg.FieldByName('flag').AsString;
      if arrdsthostcount.rdid<>dsthostcount.rdid then
      begin
        disd0:=abs(dsthostcount.element_con[0]-arrdsthostcount.element_con[0]);
        disd1:=abs(dsthostcount.element_con[1]-arrdsthostcount.element_con[1]);
        disd2:=abs(dsthostcount.element_con[2]-arrdsthostcount.element_con[2]);        if (disd0<=d) and (disd1<=d) and (disd2<=d)   then
        begin
          //算距离
          for k:=0 to 19 do
          begin
            dis:=dis+(dsthostcount.element_con[k]-arrdsthostcount.element_con[k])*(dsthostcount.element_con[k]-arrdsthostcount.element_con[k]);
          end;
          if arrdsthostcount.ele_proto<>dsthostcount.ele_proto then
            dis:=dis+1/9;
          if arrdsthostcount.ele_serv<>dsthostcount.ele_serv then
            dis:=dis+(1/14)*(1/14);
          if arrdsthostcount.ele_flag<>dsthostcount.ele_flag then
            dis:=dis+1/9;
          for k:=1 to 4 do
          begin
            if arrdsthostcount.element_sym[k]<>dsthostcount.element_sym[k] then
              dis:=dis+1/4;
          end;
          dis:=Sqrt(dis);
          //记录信息
          if dis<=d then
          begin
            for m:=0 to adosel.RecordCount-1 do
            begin
              if testRecord[m].Dir=dsthostcount.rdid then
              begin //判断是否为最短距离
                if (testRecord[m].Distance>dis)or(testRecord[m].Distance=0)  then
                  testRecord[m].Distance:=dis;
                if testRecord[m].Neighbors<>'' then
                  testRecord[m].Neighbors:=testRecord[m].Neighbors+','+inttostr(arrdsthostcount.rdid)
                else
                  testRecord[m].Neighbors:=inttostr(arrdsthostcount.rdid);
              end;
              if testRecord[m].Dir=arrdsthostcount.rdid then
              begin
                if testRecord[m].AntiNeighbors<>'' then
                  testRecord[m].AntiNeighbors:=testRecord[m].AntiNeighbors+','+inttostr(dsthostcount.rdid)
                else
                  testRecord[m].AntiNeighbors:=inttostr(dsthostcount.rdid);
              end;
            end;
          end;
        end;
      end;
      adoavg.Next;
    end;
    adosel.Next;
  end;
 except
    on E: Exception do showmessage(E.Message);
 end;
end;