求助:大家帮我看看错在哪了? ( 积分: 50 )

  • 主题发起人 主题发起人 panminfu
  • 开始时间 开始时间
P

panminfu

Unregistered / Unconfirmed
GUEST, unregistred user!
function posx(sub:string;str:string;ipos:integer;direction:integer;non_case:boolean):integer;
var
sublen,strlen:integer;
j:integer;
begin
sublen:=length(sub);
strlen:=length(str);

if (sublen>=strlen) or (ipos>=strlen) or (ipos+sublen>=strlen) or (ipos<1) then
posx:=-1;

if (direction<>-1) and (direction<>1) then
posx:=-1;

if (bytetype(str,ipos)=mbtrailbyte) then
ipos:=ipos-direction;

if direction=1 then
begin
if non_case then
begin
while ipos+sublen-1<=strlen do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then
j:=2
else
j:=1;
if uppercase(sub)=uppercase(copy(str,ipos,sublen)) then
posx:=ipos
else
ipos:=ipos+j;
end;
end
else
begin
while ipos+sublen-1<=strlen do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then
j:=2
else
j:=1;
if sub=copy(str,ipos,sublen) then
posx:=ipos
else
ipos:=ipos+j;
end;
end;
end
else //-------往回查找----------
if non_case then
while ipos>=1 do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then{子字符串首尾字节是否为单字节编码}
j:=-2
else
j:=-1;
if uppercase(sub)=uppercase(copy(str,ipos,sublen)) then{不区分大小写就把字符串都转为大写再比较}
posx:=ipos
else
ipos:=ipos+j;
end
else
while ipos>=1 do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then{子字符串首尾字节是否为单字节编码}
j:=-2
else
j:=-1;
if sub=copy(str,ipos,sublen) then
posx:=ipos
else
ipos:=ipos+j;
end;
result:=0; //没找到就返回0
end;
我一执行就陷入死循环,那里的问题?大家看看~0~
 
怎么没人啊,自己顶一下
 
function posx(sub:string;str:string;ipos:integer;direction:integer;non_case:boolean):integer;
var
sublen,strlen:integer;
j:integer;
begin
posx := 0; //没找到就返回0
sublen:=length(sub);
strlen:=length(str);

if (sublen>=strlen) or (ipos>=strlen) or (ipos+sublen>=strlen) or (ipos<1) then
posx:=-1;

if (direction<>-1) and (direction<>1) then
posx:=-1;

if (bytetype(str,ipos)=mbtrailbyte) then
ipos:=ipos-direction;

if direction=1 then
begin
if non_case then
begin
while ipos+sublen-1<=strlen do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then
j:=2
else
j:=1;
if uppercase(sub)=uppercase(copy(str,ipos,sublen)) then
begin
posx:=ipos;
Break;
end
else
ipos:=ipos+j;
end;
end
else
begin
while ipos+sublen-1<=strlen do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then
j:=2
else
j:=1;
if sub=copy(str,ipos,sublen) then
begin
posx:=ipos;
Break;
end
else
ipos:=ipos+j;
end;
end;
end
else //-------往回查找----------
if non_case then
while ipos>=1 do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then{子字符串首尾字节是否为单字节编码}
j:=-2
else
j:=-1;
if uppercase(sub)=uppercase(copy(str,ipos,sublen)) then{不区分大小写就把字符串都转为大写再比较}
begin
posx:=ipos;
Break;
end
else
ipos:=ipos+j;
end
else
while ipos>=1 do
begin
if (bytetype(sub,1)<>mbsinglebyte) and (bytetype(str,ipos)<>mbsinglebyte) then{子字符串首尾字节是否为单字节编码}
j:=-2
else
j:=-1;
if sub=copy(str,ipos,sublen) then
begin
posx:=ipos;
Break;
end
else
ipos:=ipos+j;
end;
end;

找到了循环不退出,不死循环才怪
最后又来一句 result:=0; 变成所有结果均为0
 
To levi:
对不起,这几天没上网,现在给分。
posx:=ipos;执行后不就退出函数了吗?我是菜鸟,请再指教一下
 

Similar threads

后退
顶部