AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Exakte Addition langer Zahlen

Ein Thema von ginimausi · begonnen am 19. Mär 2009 · letzter Beitrag vom 25. Mär 2009
Antwort Antwort
mr_emre_d
(Gast)

n/a Beiträge
 
#1

Re: Exakte Addition langer Zahlen

  Alt 19. Mär 2009, 17:46
OK. Ich poste es auch ... Sie hats ganz bestimmt schon von den vorigen Posts Copy&Pasted...

Delphi-Quellcode:
function StrAddition( var Str1, Str2: String ): String;
const
  VN: set of char = [ '0'..'9' ];
var
  i, Carry: Integer;
  function ValidNumbers: Boolean;
  var
    i: Integer;
  begin
    Result := False;
    for i := 1 to Length( Str1 ) do
      if not ( Str1[i] in VN ) then
        Exit;
    for i := 1 to Length( Str2 ) do
      if not ( Str2[i] in VN ) then
        Exit;
    Result := not Result;
  end;
  procedure EquateStrLengths; // macht aus 1234 v 12 -> 1234 v 0012
  var
    L, S: PString;
    B: String;
  begin
    if Length( Str1 ) = Length( Str2 ) then
      Exit;
    L := @Str1;
    S := @Str2;
    if Length(Str1) < Length(Str2) then
    begin
      L := @Str2;
      S := @Str1;
    end;
    SetLength( B, Length( L^ ) - Length( S^ ) );
    FillChar( B[1], Length( L^ ) - Length( S^ ), '0' );
    S^ := B + S^;
  end;
begin
  Result := 'Error';
  if not ValidNumbers then
    Exit;
  EquateStrLengths;
  SetLength( Result, Length( Str1 ) );
  Carry := 0;
  for i := Length(Str1) downto 1 do
  begin
    Result[i] := IntToStr( ( StrToInt( Str1[i] ) + StrToInt( Str2[i] ) + Carry ) mod 10 )[1];
    Carry := ( StrToInt( Str1[i] ) + StrToInt( Str2[i] ) + Carry ) div 10;
  end;
  if Carry > 0 then
    Result := IntToStr(Carry) + Result;
end;
Demo Dino im Anhang

MfG
Angehängte Dateien
Dateityp: rar straddit_105.rar (161,1 KB, 7x aufgerufen)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:11 Uhr.
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