求一逻辑代码

helloha2013 2009-05-14 12:51:07
现有字符串
str:='56.27.25.63|||57.28.63.55|||21.75.122.533|||'
希望随机生成一个ip地址,但不能包含str中已有的地址。

求这样的代码
...全文
34 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovelymelon 2009-05-14
  • 打赏
  • 举报
回复
首先通过
var
ss:TStrings;
begin
ss:=TStringList.Create;
ss.clear;
extractstrings(['|||'],[],pchar(str),ss)
end;
得出字符串中已有IP地址
然后再随机生成IP地址IP
i:=ss.index(ip);
判断i是否等于-1。如果等于-1就说明没在str中

helloha2013 2009-05-14
  • 打赏
  • 举报
回复
请帮忙看一下这个代码:

var strip:string;
var ss:string;

Randomize;
ss:='56.27.25.63|||57.28.63.55|||21.75.122.533|||';

while ss.indexOf(strip)>-1 do ////////////////////这里报错
begin
strip=Format('10.%d.%d.%d',
[Random(255), Random(255), Random(254) + 1])
end

请问该怎么写?
lovelymelon 2009-05-14
  • 打赏
  • 举报
回复
随机生成ip地址代码

procedure TForm1.Button1Click(Sender: TObject);
var
ip,s,s1,s2,s3,s4:string;
i,j:Integer;
b:Boolean;
begin
b:=False;
while b=False do
begin
Randomize;
ip:='';
for j:=1 to 12 do
begin
i:=Random(9);
s:=IntToStr(i);
if (j<>4) and (j<>7) and (j<>10) then
ip:=ip+s
else
ip:=ip+'.'+s;
end;
if Copy(ip,1,3)='000' then
b:=False
else
b:=True;
end;
s1:=Copy(ip,1,3);
s2:=Copy(ip,5,3);
s3:=Copy(ip,9,3);
s4:=Copy(ip,13,3);
ip:=IntToStr(StrToInt(s1))+'.'+IntToStr(StrToInt(s2))+'.'+IntToStr(StrToInt(s3))+'.'+
IntToStr(StrToInt(s4));
self.Label1.Caption:=ip;
end;

helloha2013 2009-05-14
  • 打赏
  • 举报
回复
好的谢谢你

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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