Einzelnen Beitrag anzeigen

Benutzerbild von 3_of_8
3_of_8

Registriert seit: 22. Mär 2005
Ort: Dingolfing
4.129 Beiträge
 
Turbo Delphi für Win32
 
#16

Re: Aus wievielen Ziffern besteht meine Ganzzahl??

  Alt 16. Jan 2008, 18:43
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.
Manuel Eberl
„The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.“
- Terry Pratchett
  Mit Zitat antworten Zitat