Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2007-10

Re: fpc-devel Digest, Vol 40, Issue 28

2 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

L
Show quoted text 22 lines
> Rather.. > > var > i: integer; > begin > i:= StrToInt(s); > if (s = '0') or (i 0) then > writeln('S is an integer, and i is now: ', i) > else > writeln('S is not an integer: ', s); > end; > > Alternatively: > > function IsInteger(s: string; i: integer); > begin > result:= false; > if (s = '0') or (i 0) then result:= true; s can be 00 000 0000 etc. as well as +0 +00 etc.

And '-0' '-00'

Army of critical programmer brains can help,

program test; {$mode objfpc} {$H+}
function IsInteger(s: string; i: integer): boolean;
var idx: integer;
begin
  result:= true;
  if i  0 then exit;
  for idx:= 1 to length(s) do
  begin
    if (idx = 1) then
      if (s[idx] = '-') or (s[idx] = '+') then
        continue;
    if (s[idx]  '0') then result:= false;
  end;
  // could also check for invalid length..
  // not sure if 00000000000000000 is acceptable
end;
function StrToInt(s: string): integer;
var dummy: integer;
begin
  val(s, result, dummy);
end;
var tmp: string;
    IsInt: boolean;
    someint: integer;
begin
  tmp:= '0000';
  someint:= StrToInt(tmp);
  IsInt:= IsInteger(tmp, someint);
  writeln('Should be true: ', IsInt);
  tmp:= '0';
  someint:= StrToInt(tmp);
  IsInt:= IsInteger(tmp, someint);
  writeln('Should be true: ', IsInt);
  tmp:= '-0000';
  someint:= StrToInt(tmp);
  IsInt:= IsInteger(tmp, someint);
  writeln('Should be true: ', IsInt);
  tmp:= '+0000';
  someint:= StrToInt(tmp);
  IsInt:= IsInteger(tmp, someint);
  writeln('Should be true: ', IsInt);
  tmp:= '000ab';
  someint:= StrToInt(tmp);
  IsInt:= IsInteger(tmp, someint);
  writeln('Should be false: ', IsInt);
  tmp:= 'x';
  someint:= StrToInt(tmp);
  IsInt:= IsInteger(tmp, someint);
  writeln('Should be false: ', IsInt);
  tmp:= '0';
  someint:= StrToInt(tmp);
  IsInt:= IsInteger(tmp, someint);
  writeln('Should be true: ', IsInt);
  readln;
end.

What about +-+000 and ++-+0 though?

And does StrToInt in the current sysutils check this?

Reply by email

L
What about +-+000 and ++-+0 though? And does StrToInt in the current sysutils check this?

An unhandled exception occurred at $0040A462 :
EConvertError : "+-000" is an invalid integer
$0040A462
$0040172F

To answer my own question: that one is invalid according to current sysutils laws.

According to the laws of mathematicians or scientists? I am not sure..

Anyone know?

Reply by email