文本文件的内容为:
约1万行的ip地址,一个地址占一行。要求:统计出总行数,并找出出现次数最多的3个ip

解决方案 »

  1.   

    var
      J: Integer;
      I: Integer;
    begin
      with TStringList.Create do try
        LoadFromFile('input.txt'); //载入文件
        Sort; //排序
        J := 1;
        for I := Count - 1 downto 0 do begin
          if (I >= 1) and (Strings[I] = Strings[I - 1]) then begin
            Inc(J);
            Delete(I);
          end else begin
            Strings[I] := Format('(%5d)%s', [J, Strings[I]]);
            J := 1;
          end;
        end;
        Sort; //排序//取最后三行
        SaveToFile('C:\temp\output.txt');
      finally
        Free;
      end;
    end;
      

  2.   

    var
      J: Integer;
      I: Integer;
    begin
      with TStringList.Create do try
        LoadFromFile('input.txt'); //载入文件
        Sort; //排序
        J := 1;
        for I := Count - 1 downto 0 do begin
          if (I >= 1) and (Strings[I] = Strings[I - 1]) then begin
            Inc(J);
            Delete(I);
          end else begin
            Strings[I] := Format('(%5d)%s', [J, Strings[I]]);
            J := 1;
          end;
        end;
        Sort; //排序//取最后三行
        SaveToFile('C:\temp\output.txt');
      finally
        Free;
      end;
    end;
      

  3.   

    建议将其导入数据库,用一条带聚组的sql语句就出来了,
      

  4.   

    to 
    ffwin() ( ) 
    没法说你,
    1W多条,满不满那
      

  5.   

    考虑效率问题,推荐读入后转成DWORD类型,排序后在搜索出现次数最多的三个IP
      

  6.   

    Eastunfail(浴血雏龙)==(恶鱼杀手) 方法效率更高,也适用于大数据量
    IP"127:183:21:77" -->转成整形量1271832177
    Sort
    .....
      

  7.   

    刚才没事做,帮你写了一个,带有注释function StrToIP(IP:String):DWORD;
    asm
      mov edi,10
      mov esi,IP
      xor ecx,ecx
      xor ebx,ebx
      @nextNumber://next number in IP ddress
      cmp ecx,4//IPv4 :)
      je @end //finished  push ecx
      xor ecx,ecx
      xor eax,eax
      @next: //next digit
      mov cl,byte ptr [esi]
      cmp cl,0
      je @exit
      cmp cl,'.'
      je @exitLoop //finished processing one number
      sub cl,'0'
      mul edi
      add eax,ecx
      inc esi
      jmp @next
      @exitLoop:
      pop ecx
      shl ebx,8
      add ebx,eax
      inc ecx
      inc esi
      jmp @nextPart
      @end:
      //reverse order
      mov ecx,ebx
      shr ecx,16//high bytes
      and ebx,$ffff//low bytes
      //exchange
      xchg cl,ch
      xchg bl,bh
      shl ebx,16
      or ebx,ecx//combinate
      mov eax,ebx
    end;
      

  8.   

    支持导入到数据库,不就1万数据?一个access表就解决了。
      

  9.   

    总行数:
    //其中函数参数s为文本文件的绝对地址:
    在程序中调用举例:
    var
     s :string;
    if form1.OpenDialog1.Execute then
    begin
     s :=form1.OpenDialog1.FileName;
     edit1.text :=inttostr(hangshu(s));
    end;
    *********************************************
    function TForm1.hangshu(s: string): integer;
     var
     fff :textfile;
     line :string;
     n :integer;
    begin
     n :=0;
     assignfile(fff,s);
     reset(fff);
     while not eof(fff) do
     begin
     readln(fff,line);
     if trim(line)<> '' then
     begin
     n :=n+1;
     end;
     end;
     closefile(fff);
     result :=n;
    end;
      

  10.   

    to YFLK:
    IP"127:183:21:77" -->转成整形量1271832177
    请问"127:183:2:177"和"127:183:21:77"有什么区别?~~to Eastunfail:
    你的代码编译不过呀~~to all:
    10000条数据,我的算法在我的机器执行时间为300毫秒左右~~
    转换成数值,存放到数据库里处理确实快!~~
    请问:转换成数值、存放到数据库里需要多少时间??~~
    楼主没出来,这个问题我就不继续了~~
      

  11.   

    刚开始是把代码写成几个函数的,后来在记事本中合并成一个函数以减小CALL带来的EIP入栈开销,改跳转标签的时候有几个忘了改:)下面的是正确的代码:function StrToIP(IP:String):DWORD;
    asm
    mov edi,10
    mov esi,IP
    xor ecx,ecx
    xor ebx,ebx
    @nextNumber://next number in IP ddress
    cmp ecx,4//IPv4 :)
    je @end //finished
    push ecx
    xor ecx,ecx
    xor eax,eax
    @next: //next digit
    mov cl,byte ptr [esi]
    cmp cl,0
    je @exitLoop
    cmp cl,'.'
    je @exitLoop //finished processing one number
    sub cl,'0'
    mul edi
    add eax,ecx
    inc esi
    jmp @next
    @exitLoop:
    pop ecx
    shl ebx,8
    add ebx,eax
    inc ecx
    inc esi
    jmp @nextNumber
    @end:
    //reverse order
    mov ecx,ebx
    shr ecx,16//high bytes
    and ebx,$ffff//low bytes
    //exchange
    xchg cl,ch
    xchg bl,bh
    shl ebx,16
    or ebx,ecx//combinate
    mov eax,ebx
    end;