to: hch_45(んこん) 你这只是判断是否为整型而已. 给你一个函数,如果是数值型的返回true,否则返回falsebool CheckIsFigure(AnsiString Str) { bool flag = true; int k = 0; char *c = Str.c_str(); for (int i = 0 ; i < Str.Length() ; i++) { if (c[i] != '0' && c[i] != '1' && c[i] != '2' && c[i] != '3' && c[i] != '4' && c[i] != '5' && c[i] != '6' && c[i] != '7' && c[i] != '8' && c[i] != '9' && c[i] != '.') { flag = false; break; } if (c[i] == '.') k++; if (k != 0) { flag = false; break; } } return flag; }
非要来个异常呀!!! --------- var i:integer; BFlag:boolean; begin BFlag:=true; i:=1; while i<=length(Edit1.Text) do begin if not (Edit1.Text[i] in ['0'..'9']) then begin BFlag:=false; break; end; inc(i); end; if not BFlag then showmessage('不是数字串');
哦,不好意思,上面是C++ Delphi如下:Function CheckIsFigure(Str:string): Boolean; var i, k : integer; begin Result := true; k := 0; for i := 1 to Length(Str) - 1 do begin if (Str[i] <> '0') and (Str[i] <> '1') and (Str[i] <> '2') and (Str[i] <> '3') and (Str[i] <> '4') and (Str[i] <> '5') and (Str[i] <> '6') and (Str[i] <> '7') and (Str[i] <> '8') and (Str[i] <> != '9') and (Str[i] <> '.') then begin Result := false; Exit; end if (c[i] == '.') then Inc(k); if (k > 1) then begin Result := false; Exit; end; end; end;
上面只能判断整数串,来个完整的:判断是否是数字串 --------------- function IsNumeric (const Value: string): Boolean; var i,DotCount:integer; begin Result:=true; i:=1; DotCount:=0; while i<=length(value) do begin if not (value[i] in ['0'..'9']) then begin if (value[i]='.') then begin inc(DotCount); if (i=1) or (i=length(value)) or (DotCount>1)then begin Result:=false; break; end; end else begin Result:=false; break; end; end; inc(i); end; end;
StrToInt(Edit1.Text);
except
不是
end;是
hch_45(んこん)
你这只是判断是否为整型而已.
给你一个函数,如果是数值型的返回true,否则返回falsebool CheckIsFigure(AnsiString Str)
{
bool flag = true;
int k = 0;
char *c = Str.c_str();
for (int i = 0 ; i < Str.Length() ; i++)
{
if (c[i] != '0' && c[i] != '1' && c[i] != '2' && c[i] != '3' && c[i] != '4'
&& c[i] != '5' && c[i] != '6' && c[i] != '7' && c[i] != '8' && c[i] != '9'
&& c[i] != '.')
{
flag = false;
break;
}
if (c[i] == '.')
k++;
if (k != 0)
{
flag = false;
break;
}
}
return flag;
}
---------
var
i:integer;
BFlag:boolean;
begin
BFlag:=true;
i:=1;
while i<=length(Edit1.Text) do
begin
if not (Edit1.Text[i] in ['0'..'9']) then
begin
BFlag:=false;
break;
end;
inc(i);
end;
if not BFlag then showmessage('不是数字串');
Delphi如下:Function CheckIsFigure(Str:string): Boolean;
var
i, k : integer;
begin
Result := true;
k := 0;
for i := 1 to Length(Str) - 1 do
begin
if (Str[i] <> '0') and (Str[i] <> '1') and (Str[i] <> '2') and (Str[i] <> '3') and (Str[i] <> '4') and (Str[i] <> '5') and (Str[i] <> '6') and (Str[i] <> '7') and (Str[i] <> '8') and (Str[i] <> != '9') and (Str[i] <> '.') then
begin
Result := false;
Exit;
end
if (c[i] == '.') then Inc(k);
if (k > 1) then
begin
Result := false;
Exit;
end;
end;
end;
---------------
function IsNumeric (const Value: string): Boolean;
var
i,DotCount:integer;
begin
Result:=true;
i:=1;
DotCount:=0;
while i<=length(value) do
begin
if not (value[i] in ['0'..'9']) then
begin
if (value[i]='.') then
begin
inc(DotCount);
if (i=1) or (i=length(value)) or (DotCount>1)then
begin
Result:=false;
break;
end;
end
else
begin
Result:=false;
break;
end;
end;
inc(i);
end;
end;