Autor Beitrag
MILEZ
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Sa 26.04.08 15:14 
Hallohalo!
Ich bin grade dabei einen Taschenrechner zu programmieren mit Hilfe von Delphi.
Nur ich komme bei der Fakultät nicht weiter.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.FakultaetClick(Sender: TObject);
  Var a,b:integer;
  begin
  a:=strtoint(Edit1.Text);
  repeat
  b:=a*(a-1);
  a:=a-1;
  until a=1;
  Edit3.Text:=inttostr(b);
  end;

Schließlich wird die Variable b jedes mal wieder überschrieben. -.-

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
Sirke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 208
Erhaltene Danke: 2



BeitragVerfasst: Sa 26.04.08 15:27 
Versuchs mal hiermit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
b := a;
repeat
  b := b * ( a - 1 );
  a := a - 1;
until( a = 1 );
Timosch
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1314

Debian Squeeze, Win 7 Prof.
D7 Pers
BeitragVerfasst: Sa 26.04.08 15:32 
Ich finde die Version aus dem Delphi-Treff ja immer noch am elegantesten:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
Function Fakultaet(Zahl : Int64): Int64;
begin
  If Zahl = 0 then
    result := 1
  else
    result := Zahl * Fakultaet(Zahl - 1);
end;

Ach ja: :welcome: in der EE!

_________________
If liberty means anything at all, it means the right to tell people what they do not want to hear. - George Orwell
MILEZ Threadstarter
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Sa 26.04.08 15:36 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
b := a;
repeat
  b := b * ( a - 1 );
  a := a - 1;
until( a = 1 );

Da kann doch auch nicht gehen, oder irre ich mich jetzt?
Dann bleibt doch b die ganze Zeit über eine feste Zahl (zum Beispiel 8).

Beispiel:
ausblenden Delphi-Quelltext
1:
2:
b := 8 * (8-1)   \\8*7=56
a := 7

dann gehts aber so weiter:
b := 8 * (7-1)und nicht 7*6
oder irre ich mich?

P.S.: Danke Timosch ;)

Moderiert von user profile iconAXMD: Delphi-Tags hinzugefügt
GericasS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Sa 26.04.08 15:41 
@ Milez :

Füge bitte mal Delphi-Tags hinzu dann kann man es besser lesen =)

LG

GericasS

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Sa 26.04.08 15:56 
Nebenbei in deiner Version kannst du einfach alle Fakultäten bis 20 speichern, denn ab da ist es zu groß für integer (für int 64 dann ca. 40).
Du solltest also im Sinne der Benutzerfreundlichkeit deines Rechners eine Version einbauen, die große Integer benutzt oder Fließkommazahlen.
Timosch
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1314

Debian Squeeze, Win 7 Prof.
D7 Pers
BeitragVerfasst: Sa 26.04.08 16:46 
user profile iconMILEZ hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
b := a;
repeat
  b := b * ( a - 1 );
  a := a - 1;
until( a = 1 );

Da kann doch auch nicht gehen, oder irre ich mich jetzt?
Dann bleibt doch b die ganze Zeit über eine feste Zahl (zum Beispiel 8).

Beispiel:
ausblenden Delphi-Quelltext
1:
2:
b := 8 * (8-1)   \\8*7=56
a := 7

dann gehts aber so weiter:
b := 8 * (7-1)und nicht 7*6
oder irre ich mich?

P.S.: Danke Timosch ;)

Moderiert von user profile iconAXMD: Delphi-Tags hinzugefügt

Du irrst dich.
Nehmen wir z.B. mal 4!. Dann wäre der Ablauf wie folgt:
b=4*(4-1)=12
a=3
nächster Durchlauf
b=12*(3-1)024
a=2
nächster Durchlauf
b=24*(2-1)=24
a=1
fertig. Ergebnis lautet 24

_________________
If liberty means anything at all, it means the right to tell people what they do not want to hear. - George Orwell
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Sa 26.04.08 17:52 
Man kann auch vorwärts zählen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
Function Fakultaet (Zahl : Integer) : Double;
Var
  i : Integer;

Begin
  Result := 1.0;
  For i:=2 to Zahl Do Result := Result * i;
End;

Irgendwie einfacher, oder?

_________________
Na denn, dann. Bis dann, denn.
huuuuuh
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 665
Erhaltene Danke: 19

win xp, (win vista), win 7
VS 2008 Express Edition, VS 2010 Express Edition, VS 2010 Professionell
BeitragVerfasst: Sa 26.04.08 17:59 
is ne typische schulaufgabe...
wir sollten es rekursiv lösen...
Timosch
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1314

Debian Squeeze, Win 7 Prof.
D7 Pers
BeitragVerfasst: Sa 26.04.08 18:16 
user profile iconhuuuuuh hat folgendes geschrieben:
is ne typische schulaufgabe...
wir sollten es rekursiv lösen...

Trifft auf meine/alzaimars Lösung (die ausm Delphi-Treff) zu.

_________________
If liberty means anything at all, it means the right to tell people what they do not want to hear. - George Orwell