Autor Beitrag
Rage
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Do 08.09.05 14:09 
Hi Leuts, ich möchte ein Programm schreiben mit dem man den ggT (größter gemeinsamer Teiler) berechnen kann. ich habe bis her das geschrieben:

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:
24:
25:
26:
27:
procedure TggT.Button1Click(Sender: TObject);

var zahl1, zahl2, ggT: integer;

begin
 zahl1 := StrToInt(Edit1.text);
 zahl2 := StrToInt(Edit2.text);

 while (ggT>0do begin

  If (zahl1 > zahl2) then
   begin
    ggT := zahl1 mod zahl2;
    zahl1:=zahl2;
    zahl2:=ggT;
    edit3.text:= IntToStr(ggT);
   end
  else
   begin
    ggT := zahl2 mod zahl1;
    zahl1:=zahl2;
    zahl2:=ggT;
    edit3.text:= IntToStr(ggT);
   end;
 end;
end;
end.


das problem ist nur das er mir als ergebnis immer 0 liefert.
dabei habe ich ja eine schleife benutzt die sagt das er meine if anweisung solange ausführt solange der ggT größer als 0 ist. Was ist an meinem Code falsch ?

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
Moderiert von user profile iconChristian S.: Topic aus CLX / Delphi Language (Object-Pascal) verschoben am Do 08.09.2005 um 15:25
Moderiert von user profile iconTino: Titel geändert.


Zuletzt bearbeitet von Rage am Do 08.09.05 14:19, insgesamt 1-mal bearbeitet
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 08.09.05 14:13 
Kannst du den Code erstmal ordentlich einrücken? Man muss bei deinem Quelltext erstmal überlegen, welches end wohin gehört ;). (Bitte per editieren, also keinen neuen Post)

PS: Ich weiß, dass es vermutlich daran lag, das du zuerst nicht den Delphi-Tag im Post drin hattest ;).

//Edit: Fehler gefunden. Du nimmst immer den letzten rest, der nach dem euklidischen Verfahren immer 0 ist. Um den ggT zu bekommen musst du den Rest von dem vorgehenden Durchlauf nehmen.
Rage Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Do 08.09.05 14:21 
ja gut das hab ich mir auch schon gedacht :-)
allerdings weiß ich nicht wie ich das machen soll.
ich habe gedacht das durch das "while (ggT>0)" das problem gelöst wäre.
is aber anscheinend nicht so.
weißt du wie ich das machen kann ?
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 08.09.05 14:29 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
uses Math;
...

var
  Zw: Integer;
  Zahl1: Integer;
  Zahl2: Integer;
  ggT: Integer;
...
Zahl1:=IntToStr(Edit1.Text);
Zw:=IntToStr(Edit1.Text);
Zahl2:=Min(Zw, Zahl1);
Zahl1:=Max(Zw, Zahl2);
ggT:=0;
while not ((Zahl1 mod Zahl2)=0do
begin
  ggT:=(Zahl1 mod Zahl2);
  Zw:=Zahl1;
  Zahl1:=Zahl2;
  Zahl2:=Zw;
end;


PS: Ich hoffe das ich keinen Fehler gemacht habe, wenn ja sage es bitte ;).

//Edit: Soweit ich weiß gibt es aber im DF genug Themen zu dem Thema. ;)
Rage Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Do 08.09.05 14:37 
ahhh dankeschön für deine Hilfe.
ich muss zwar ehrlich zugeben das ich deinen code nicht übernommen habe, aber die eine zeile aus deinem code (while not ((Zahl1 mod Zahl2)=0) do) die hat mir weitergeholfen und jetzt funktioniert auch meine variante mit deiner hilfe.
danke nochma, jetzt kann ich endlich en mittagsschlaf halten :D
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 08.09.05 14:42 
Mein Code dürfte eigentlich sogar schneller sein, da ich keine if-Anweisungen dazwiwschen immer brauche und is übersichtlicher ;).
Land-Gull
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 51

SuSe 9.2, WIN XP
D7 Enter
BeitragVerfasst: Do 08.09.05 14:50 
Ich würde das ganze so lösen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.ggT(Zahl1,Zahl2:integer;var ggT:integer);
var temp: integer;
begin
 repeat

  if Zahl1<Zahl2 then begin   //vertauschen
   temp:=Zahl1;
   Zahl1:=Zahl2;
   Zahl2:=Temp;
  end;
 ggT:=Zahl1 mod Zahl2;
 Zahl1:=Zahl2; Zahl2:=ggT;
 until Zahl2 = 0;
 ggT:=Zahl1;
end;
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Do 08.09.05 14:52 
Da hast du aber wieder eine if-Anweisung in einer Schleife, die man verhindern kann durch logisches denken ;).
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Fr 09.09.05 18:41 
Das geht doch schöner:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function ggT(a,b : integer) : integer;
begin
 if (a=b) or (b=0then
  Result := a
 else
  Result := ggT(b,a mod b);
end;