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

cma123 2004-04-13 02:55:28
文本文件的内容为:
约1万行的ip地址,一个地址占一行。

要求:统计出总行数,并找出出现次数最多的3个ip
...全文
268 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
zswangII 2004-04-14
  • 打赏
  • 举报
回复
to YFLK:
IP"127:183:21:77" -->转成整形量1271832177
请问"127:183:2:177"和"127:183:21:77"有什么区别?~~

to Eastunfail:
你的代码编译不过呀~~

to all:
10000条数据,我的算法在我的机器执行时间为300毫秒左右~~
转换成数值,存放到数据库里处理确实快!~~
请问:转换成数值、存放到数据库里需要多少时间??~~
楼主没出来,这个问题我就不继续了~~
liangjun66 2004-04-14
  • 打赏
  • 举报
回复
总行数:
//其中函数参数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;
严黎斌 2004-04-14
  • 打赏
  • 举报
回复
支持导入到数据库,不就1万数据?一个access表就解决了。
blueshrimp 2004-04-14
  • 打赏
  • 举报
回复
晕。导入到数据库,1秒钟就统计出来了
ztenv 2004-04-14
  • 打赏
  • 举报
回复
我想用线程会更好一点的
Eastunfail 2004-04-14
  • 打赏
  • 举报
回复
刚才没事做,帮你写了一个,带有注释

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;
twlx_0 2004-04-14
  • 打赏
  • 举报
回复
hujinger 2004-04-14
  • 打赏
  • 举报
回复
处理网络流量的问题吧?呵呵
menggirl 2004-04-14
  • 打赏
  • 举报
回复
有那么多的人支着,改行了吧,揭帖吧
YFLK 2004-04-14
  • 打赏
  • 举报
回复
Eastunfail(浴血雏龙)==(恶鱼杀手) 方法效率更高,也适用于大数据量
IP"127:183:21:77" -->转成整形量1271832177
Sort
.....
荣爵 2004-04-14
  • 打赏
  • 举报
回复
Good~~
Eastunfail 2004-04-14
  • 打赏
  • 举报
回复
刚开始是把代码写成几个函数的,后来在记事本中合并成一个函数以减小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;
ghtghtmalone 2004-04-13
  • 打赏
  • 举报
回复
观摩一下
Eastunfail 2004-04-13
  • 打赏
  • 举报
回复
考虑效率问题,推荐读入后转成DWORD类型,排序后在搜索出现次数最多的三个IP
dickeybird888 2004-04-13
  • 打赏
  • 举报
回复
我想用数据库或许更方便一些!

ICMGDCHN 2004-04-13
  • 打赏
  • 举报
回复
本来想进来混点分的,有了强人,就不献丑啦
menggirl 2004-04-13
  • 打赏
  • 举报
回复
to
ffwin() ( )
没法说你,
1W多条,满不满那
madd123 2004-04-13
  • 打赏
  • 举报
回复
ffwin 2004-04-13
  • 打赏
  • 举报
回复
建议将其导入数据库,用一条带聚组的sql语句就出来了,
  • 打赏
  • 举报
回复
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;
加载更多回复(6)

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧