Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Software-Projekte der Mitglieder (https://www.delphipraxis.net/26-software-projekte-der-mitglieder/)
-   -   TBigUInt - Noch ein Datentyp für große Zahlen (https://www.delphipraxis.net/134608-tbiguint-noch-ein-datentyp-fuer-grosse-zahlen.html)

Blup 27. Mai 2009 09:34

Re: TBigUInt - Noch ein Datentyp für große Zahlen
 
Vorschlag für StrToInt:
Delphi-Quellcode:
uses
  SysConst;

function StrToInt(const S: String): TBigUInt;
var
  n: String;
  i: Integer;
  c: char;
begin
  {Leerzeichen am Anfang und Ende entfernen}
  n := Trim(s);

  if Length(n) = 0 then
    raise EConvertError.CreateResFmt(@SInvalidInteger, [S]);

  Result := 0;
  for i := 1 to Length(n) do
  begin
    c := n[i];
    if (c < '0') or (c > '9') then
      raise EConvertError.CreateResFmt(@SInvalidInteger, [S]);

    Result := Result * 10 + (Ord(c) - Ord('0'));
  end;
end;
Auf jeden Fall sollten auch negative Zahlen unterstützt werden, sonst ist der Typ zu sehr eingeschränkt.

Edit: n[1] durch n[i] ersetzt
:wink: gut das einer aufpasst

gammatester 27. Mai 2009 11:11

Re: TBigUInt - Noch ein Datentyp für große Zahlen
 
Zitat:

Zitat von Blup
Vorschlag für StrToInt:
Delphi-Quellcode:
{...}
c := n[1];
Auf jeden Fall sollten auch negative Zahlen unterstützt werden, sonst ist der Typ zu sehr eingeschränkt.

Du meinst doch bestimmt :wink:
Delphi-Quellcode:
c := n[i];

himitsu 27. Mai 2009 11:34

Re: TBigUInt - Noch ein Datentyp für große Zahlen
 
würde es ein bissl beschleunigen, wenn nicht für jede Stelle mit der großen Zahl gerechnet würde :angel:
(praktisch die Umkehrung des IntToStr)
Delphi-Quellcode:
Result := 0;
i2 := 0;
i3 := 1;
for i := 1 to Length(n) do
begin
  if not (n[i] in ['0'..'9']) then
    raise EConvertError.CreateResFmt(@SInvalidInteger, [S]);
  i2 := i2 * 10 + (Ord(n[i]) - Ord('0'));
  i3 := i3 * 10;
 
  if (i3 = 1000000000) or (i = Length(n)) then
  begin
    Result := Result * i3 + i2;
    i2 := 0;
    i3 := 1;
  end;
end;
[edit] i2 durch i3 ersetzt (siehe gammatester)

gammatester 27. Mai 2009 12:13

Re: TBigUInt - Noch ein Datentyp für große Zahlen
 
Zitat:

Zitat von himitsu
Delphi-Quellcode:
if (i2 = 1000000000) or (i = Length(n)) then {...}

Besser ist wohl :wink:
Delphi-Quellcode:
if (i3 = 1000000000) or (i = Length(n)) then {...}


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:38 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz