昨天看了大家给我的分离字符串算法,但还是存在一个问题,程序并没有比较相等的时候,如果StrA=StrB,将在while陷入死循环,如果加入if StrA=StrB then... 将马上跳出,因为它只比较了主版本号就认为是相等而不理会下面的。procedure TForm1.Button1Click(Sender: TObject);
var
stra,strb:string;
nowa,nowb:integer;
begin
StrA:='2.0.2.33';
StrB:='2.0.2.1';
try
while true  do
begin
    if Pos('.',StrA)<>0 then
 begin
  NowA:=strtoint(copy(StrA,1,Pos('.',StrA)-1));
  NowB:=strtoint(copy(StrB,1,Pos('.',StrB)-1));
 end
 else
  begin
  NowA:=strtoint(stra);
  NowB:=strtoint(StrB);
  end;
  if NowA>NowB then
  begin
    ShowMessage('A大');
    break;
  end;
  if NowA<NowB then
  begin
    ShowMessage('b大');
    break;
  end;
  StrA:=copy(StrA,Pos('.',StrA)+1,Length(StrA)-Pos('.',StrA));
  StrB:=copy(StrB,Pos('.',StrB)+1,Length(StrB)-Pos('.',StrB));
end;
except
 on exception  do ;
end;
end;

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    stra,strb:string;
    nowa,nowb:integer;
    flag: boolean;
    begin
    StrA:='2.0.2.33';
    StrB:='2.0.2.1';
    flag := false;
    try
    while true  do
    begin
        if Pos('.',StrA)<>0 then
     begin
      NowA:=strtoint(copy(StrA,1,Pos('.',StrA)-1));
      NowB:=strtoint(copy(StrB,1,Pos('.',StrB)-1));
     end
     else
      begin
      NowA:=strtoint(stra);
      NowB:=strtoint(StrB);
      flag := true;
      end;
      if NowA>NowB then
      begin
        ShowMessage('A大');
        break;
      end;
      if NowA<NowB then
      begin
        ShowMessage('b大');
        break;
      end;
      if (NowA = NowB) and flag then 
      begin
        ShowMessage('相等');
        break;
      end;
      StrA:=copy(StrA,Pos('.',StrA)+1,Length(StrA)-Pos('.',StrA));
      StrB:=copy(StrB,Pos('.',StrB)+1,Length(StrB)-Pos('.',StrB));
    end;
    except
     on exception  do ;
    end;
    end;
      

  2.   

    rustle(一日不过三) ,如果StrA和StrB完全相等的话,那照样会陷入死循环。
      

  3.   

    会吗?你试试看我这不会不过我这样修改一下还不够的,要是StrA和StrB的点都不一样的话,会出错的当然如果你能保证不会出现这种情况就无所谓了
      

  4.   

    很奇怪你们为什么用while true do循环,而不用while Pos('.', StrA) <> 0 do
      

  5.   

    请楼主看贴还有验证的时候稍微仔细一点我的程序只在验证以下这种情况的时候有问题  StrA := '2.4.5.6'
      StrB := '2.4.5.6.7'而只要StrA和StrB的结构相同,不论是否完全相等,均没有问题程序在Delphi6中执行通过
    StrA:='2.0.2.33';
    StrB:='2.0.2.33';
    弹出对话框提示相等
      

  6.   

    DelphiBoy2003(我是李逍遥) 
      很奇怪你们为什么用while true do循环,而不用while Pos('.', StrA) <> 0 do原来程序结构很不合理,我只是懒得从头写
      

  7.   

    用while Pos('.', StrA) <> 0 do再把最后一个字串补上就行了,这时Pos('.', StrA)=0
      

  8.   

    呵呵,是我没看清楚你上面的几个flag设置呢。谢谢!