Einzelnen Beitrag anzeigen

grenzgaenger
(Gast)

n/a Beiträge
 
#18

Re: Aus wievielen Ziffern besteht meine Ganzzahl??

  Alt 16. Jan 2008, 21:46
Zitat von 3_of_8:
Um das ganze mal rein in Delphi zu formulieren:

Delphi-Quellcode:
function GetDecimalFigures(AValue: Integer): Integer;
const POWERSOFTEN: array[0..9] of Integer =
  (1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  100000000, 1000000000);
var lo, hi, mid: Integer;
begin
  lo:=0;
  hi:=high(POWERSOFTEN);
  Result:=0;
  AValue:=abs(AValue);

  while Result=0 do
  begin
    mid:=lo+(hi-lo) div 2;
    if POWERSOFTEN[mid]<AValue then
      lo:=mid+1
    else if POWERSOFTEN[mid]>AValue then
      hi:=mid-1
    else begin
      Result:=mid+1;
    end;
    if lo>=hi then Result:=hi+1;
  end;
end;

Das ganze benutzt - wie vorher vorgeschlagen - ein Konstantenarray, dazu binäre Suche.
das ganze geht doch auch etwas eleganter ...

Delphi-Quellcode:
function GetIntLength(const Zahl: integer; Count: integer): integer;
var
 tmp: integer;
begin
 tmp := zahl div 10;
 if tmp>0 then
  result := GetIntLength(tmp, count + 1)
 else
  result := count + 1;
end;
  Mit Zitat antworten Zitat