Autor Beitrag
XpreZz
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Mo 29.11.04 17:45 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
var
   z1, z2, z3, z4, z5, z6, z7, z8, z9, z10, z11, z12, pz : byte;
begin
  z1 := StrToInt (EDT_Z1.Text);
  z2 := StrToInt (EDT_Z2.Text);
  z3 := StrToInt (EDT_Z3.Text);
  z4 := StrToInt (EDT_Z4.Text);
  z5 := StrToInt (EDT_Z5.Text);
  z6 := StrToInt (EDT_Z6.Text);
  z7 := StrToInt (EDT_Z7.Text);
  z8 := StrToInt (EDT_Z8.Text);
  z9 := StrToInt (EDT_Z9.Text);
  z10 := StrToInt (EDT_Z10.Text);
  z11 := StrToInt (EDT_Z11.Text);
  z12 := StrToInt (EDT_Z12.Text);
  pz := (z1+z2+z3+z4+z5+z6+z7+z8+z9+z10+z11+z12);
  EDT_Ausgang.Text := IntToStr (pz);
end;


z1 bis z12 sind jeweils die Eingaben in das Edit Feld 1-12. Nun möchte ich von pz (also dem Ergebnis der langen Addition)
die Quersumme haben. Also kommt zum Beispiel 71 bei pz raus wäre die Quersumme von 71 ---> 8. Aber wie lautet der Befehl für Quersumme ?

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Mo 29.11.04 17:55 
hallo xprezz!

ich göaube nicht, dass delphi die funktionalität function Quersumme(wert)kennt :wink:

nachdem du den betrag schon wieder in einen string gewandelt hast, was gibt es da einfacheres, als eine stelle nach der anderen in eine zahl umzuwandlen und diese addieren?

mfg, stefan
Raphael O.
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1596


VS 2013
BeitragVerfasst: Mo 29.11.04 18:01 
bruteforce (gibt sicherlich elegantere methoden)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
intvar:=0;
for i:=1 to length(stringvar) do
begin
  intvar:=intvar+strtoint(stringvar[i]);
end;
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 29.11.04 18:08 
ausblenden Easy-Helper
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Function Quersumme(i : integer): integer;
var
p: pchar;
begin
 result := 0;
 p := Pchar(IntToStr(i));
 while (p^ <> #0do
 begin
   result := result + strtoint(p^);
   Inc(p);
 end;
end;
XpreZz Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Mo 29.11.04 18:09 
Zitat:
nachdem du den betrag schon wieder in einen string gewandelt hast, was gibt es da einfacheres, als eine stelle nach der anderen in eine zahl umzuwandlen und diese addieren?


Und wie wandel ich nun pz wieder in Ziffern um, die ich anschließend simpel addieren kann ?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 29.11.04 18:15 
Quersumme über den String berechnen...unmöglich sowas :motz: ! So gehts richtig:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
quersumme:=0;
while zahl > 0 do
begin
  quersumme := quersumme + (zahl MOD 10);
  zahl:=zahl DIV 10;
end;

_________________
We are, we were and will not be.
Raphael O.
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1596


VS 2013
BeitragVerfasst: Mo 29.11.04 18:25 
Gausi hat folgendes geschrieben:
So gehts richtig:

was heißt hier richtig? ;)
schneller vielleicht, aber im endeffekt kommt das gleiche raus

*duck und weg*
XpreZz Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Mo 29.11.04 18:52 
Also sollte das konkret in meinem Fall so aussehen ?!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
var   
   z1, z2, z3, z4, z5, z6, z7, z8, z9, z10, z11, z12, pz : byte;   
begin   
  z1 := StrToInt (EDT_Z1.Text);   
  z2 := StrToInt (EDT_Z2.Text);   
  z3 := StrToInt (EDT_Z3.Text);   
  z4 := StrToInt (EDT_Z4.Text);   
  z5 := StrToInt (EDT_Z5.Text);   
  z6 := StrToInt (EDT_Z6.Text);   
  z7 := StrToInt (EDT_Z7.Text);   
  z8 := StrToInt (EDT_Z8.Text);   
  z9 := StrToInt (EDT_Z9.Text);   
  z10 := StrToInt (EDT_Z10.Text);   
  z11 := StrToInt (EDT_Z11.Text);   
  z12 := StrToInt (EDT_Z12.Text);   
  pz := (z1+z2+z3+z4+z5+z6+z7+z8+z9+z10+z11+z12);   
  EDT_Ausgang.Text := IntToStr (pz)
  quersumme:=0;   
  while zahl > 0 do   
  begin   
  quersumme := quersumme + (zahl MOD 10);   
  zahl:=zahl DIV 10;   
  end;


Oder wie muss ich das im Konkretem Beispiel bei mir einbauen ?

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 29.11.04 19:01 
Ich würd das ganz anders machen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
  PZ, X, Quersumme: Integer;
begin
  PZ := 0;

  for X := 1 to 12 do
    PZ := PZ + StrToInt(TEdit(FindComponent('EDT_Z' + IntToStr(X))).Text);

  EDT_Ausgang.Text := IntToStr(PZ);
  
  Quersumme := 0;      
  while PZ > 0 do      
  begin      
    Quersumme := Quersumme + (PZ MOD 10);      
    PZ := PZ DIV 10;      
  end;
end;
Goolix
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 83



BeitragVerfasst: Mo 29.11.04 19:18 
zu7nächst einmal würd ich deine variablen in ein array verfrachten

ausblenden Delphi-Quelltext
1:
Z:array [1..12of byte;					


anstatt:
ausblenden Delphi-Quelltext
1:
2:
var    
   z1, z2, z3, z4, z5, z6, z7, z8, z9, z10, z11, z12, pz : byte;


die zahlen einzulesen ginge auch leicher:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
var i:integer;
begn

For i:=1 to 12 do
z[i]:=  strtoint((FindComponent('EDT_Z'+IntToStr(i)) as TEdit).text);


<-- korrigiert mich, wenn die schweibweise falsch ist, habs nicht getestet...


und da ich gausi's (wahrscheinlcih bessere variante) nicht verstehe ;-), hie eine zugegeben etwas unständliche variuante:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
 i, n, pz:integer;
 p:string;
begin
  for i:=1 to 12 do begin
   p:=z[i];
    for n:=1 to length(p) do
     pz:=pz + strtoint(p[n])
  end;


hmm... mein lehrer würde mich bei den sprüngen von string zu integern killen... ist aber sehr confortabel ;-)



Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 29.11.04 19:20 
Ha ich war schneller und hab gezeigt, dass man net mal ein Array braucht.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 29.11.04 19:31 
Geht doch Mit Rekursion in nem 6-Zeiler:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function Quersumme(Value:Integer): Integer;
Begin
    Result := 0;
    If Value <> 0 Then
        Result := (Value mod 10) + Quersumme(Value div 10);
end;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.