Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Quersummefunktion von Delphi nach C? (https://www.delphipraxis.net/159623-quersummefunktion-von-delphi-nach-c.html)

AlexII 5. Apr 2011 16:13

Quersummefunktion von Delphi nach C?
 
Hi,

hab hier eine Quersummefunktion gefunden die ich nach C umschreiben will, was mir aber nicht gelingt.

Hier der Delphi Code:
Delphi-Quellcode:
{*
* Berechne die Quersumme aus einer Zahl x
* z.B.: Quersumme von 1234 ist 10
*}
function QuerSumme(x:Longint):integer;
begin
  Result := 0;
  while x <> 0 do
  begin
    Result := Result + (x mod 10);
    x := x div 10;
  end;
end;
Und hier in C (weiß nicht wie ich Result nach C umschreiben soll)
Code:
int QuerSumme(int x)
{
    while (x!=0)
    {
   
    }
}

s.h.a.r.k 5. Apr 2011 16:17

AW: Quersummefunktion von Delphi nach C?
 
Du brauchst eine interne Variable und am Schluss machst du ein return xyz.

Code:
int QuerSumme(int x)
{
    int tmp
    while (x!=0)
    {
        tmp += x % 10;
        x /= 10;
    }
    return tmp;
}
War gerad einfach mal so runtergecodet -- auf Richtigkeit prüfen! :)

AlexII 5. Apr 2011 16:36

AW: Quersummefunktion von Delphi nach C?
 
OK ich versuch mal.


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:10 Uhr.

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