Autor Beitrag
friesi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: So 11.08.02 20:13 
Hab eine kleine frage :)

Durch eine Berechnung bekomme ich eine summe (logisch :) )
diese Summe ist z.b. 241

jetzt will ich jede Ziffer mit sich selber multiplizieren und dann alle addieren!

also so:
2*2 + 4*4 + 1*1

nur wie mache ich das er die Summe (241) "teilt" also das ich mit jeder ziffer rechnen kann?!
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: So 11.08.02 20:21 
Tag auch

wandle doch die Zahl in string um ließ dann jedes Zeichen nacheinander aus wandle sie dann wieder in zahlen um und multipliziere und addiere sie dann

mal sehen ich mach mal nen bsp code...
friesi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: So 11.08.02 20:23 
ein bsp code wäre fein :)
ab mit string noch nicht wirklich gearbeitet! :lol:
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: So 11.08.02 20:30 
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
function Berechne(Zahl: integer): integer;
var
  str: string;
  cnt: LongInt;
  ziffer: LongInt;
begin
  str:= IntToStr(zahl);
  Result:= 0;
  for cnt:= 1 to Length(str) do begin
    ziffer:= StrToInt(str[cnt]);
    Result:= (ziffer * ziffer) + Result;
  end;
end;


teste mal obs so funktioniert

ausblenden Quelltext
1:
Showmessage(IntToStr(Berechne(241)));					
friesi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: So 11.08.02 20:47 
mhm so gehts :) aber es kommt die falsche zahl raus :?
also 21 kommt raus 33 müsste :) ..
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: So 11.08.02 21:21 
uhi :oops:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function Berechne(Zahl: integer): integer;
var
  str: string;
  cnt: LongInt;
  ziffer: LongInt;
begin
  str:= IntToStr(zahl);
  ziffer:= StrToInt(str[1]);
  Result:= ziffer * ziffer;
  for cnt:= 2 to Length(str) do begin
    ziffer:= StrToInt(str[cnt]);
    Result:= (Result + ziffer) * ziffer;
  end;
end;


und jetzt?
friesi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: So 11.08.02 21:29 
nun gehts :)
hättest du vielleicht noch lust mir eben zu erklären was du gemacht hast :) bzw was die Funktion macht ?!

damit ich da nicht ganz doof stehe *gg*
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: So 11.08.02 21:43 
nunja wie ich schon geschrieben habe...

- ich wandle die zahl (241) in ein string um
- da ein string wie ein array of char ist kann ich jedes zeichen über ein index ansprechen
- ich wandle dann jedes zeichen in eine zahl um (ziffer)
- diese addiere und multipliziere ich dann nach deiner aufgabe
- mit der for schleife gehe ich dann jede ziffer durch und mache die berechnung
- result ist ein variable die den rückgabewert der funktion entspricht

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
zahl = 241
str = '241'
// erste ziffer ermitteln und brechnen
ziffer = 2
result = 2*2 (4)

schleife (die anderen ziffern berechnen):
cnt = 2 | ziffer = 4 | result = (4+4) * 4 (32)
cnt = 3 | ziffer = 1 | result = (32 + 1) * 1 (33)
schleife ende


wenn de noch fragen hast, dann frag ich glaub ich hab das nen bissel besch**** erklärt
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: So 11.08.02 21:52 
so gehts auch

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
function Berechne(Zahl: integer): integer;
var
  str: string;
  cnt: LongInt;
  ziffer: LongInt;
begin
  str:= IntToStr(zahl);
  Result:= 0;
  for cnt:= 1 to Length(str) do begin
    ziffer:= StrToInt(str[cnt]);
    Result:= (Result + ziffer) * ziffer;
  end;
end;


ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
zahl = 241
str = '241'
result = 0

schleife
  cnt = 1 | ziffer = 2 | result = (0 + 2) * 2 [4]
  cnt = 2 | ziffer = 4 | result = (4 + 4) * 4 [32]
  cnt = 3 | ziffer = 1 | result = (32 + 1) * 1 [33]
schleife ende
friesi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 44

Windows XP SP2
VS 2005
BeitragVerfasst: So 11.08.02 23:18 
ok verstanden :)

nun noch eine zweite frage :)

Wenn ich nun 3 Variablen habe und in jeder eine Zahl steht!
ich nun aber aus allen zahlen eine zahl machen möchte! wie bekomme ich das bin?!
also z.b.:
ausblenden Quelltext
1:
2:
3:
rest:= 1;
rest2:= 6; 
rest3:= 4;


nun soll die zahl 164 gebildet werden.

geht das mit array?
Aber wie :D :?:
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: So 11.08.02 23:31 
geht genauso doof wie vorher

ja du kannst deine reste in ein array speichern

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var
  ziffern: array[1..5] of LongInt;
  zahl: LongInt;
begin
  ziffern[1]:= 5;
  ziffern[2]:= 3;
  ziffern[3]:= 4;
  ziffern[4]:= 1;
  ziffern[5]:= 9;
  Zahl:= Berechne(ziffern);
end;

zahl beinhaltet dann eben die zahl 53419
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function Berechne(ziffern: array of LongInt): LongInt;
var
  cnt: LongInt;
  str: string;
begin
  str:= '';
  for cnt:= Low(ziffern) to High(ziffern) do begin
    str:= str + IntToStr(ziffern[cnt]);
  end;
  result:= StrToInt(str);
end;


die function high gibt den höchsten index eines arrays zurück, in dem fall 5

die function low gibt den kleinsten index eines arrays zurück, in dem fall 1