12-10-2022, 12:35 PM
I'm sorry for being so bored I made a months-long topic necro.
It seems "zahl1" and "zahl2" are changed within the functions you called. That's why you had to save them into "d1" and "d2" before the first time function "ggt" is called. If you didn't expect the values changed within the function you have to declare local variables in "ggt" to take in "zahl1" and "zahl2". Now I'm not sure if function "kgV" should be LONG or SINGLE.
Otherwise I took the liberty of modifying this program to get away from Visual-Basic-style declarations, although QB64(PE) ignores them. Also make sure the functions return LONG integer.
I ask because I know next to no German: for input of "23" then "32" the results are "1" and "736", yes?
It seems "zahl1" and "zahl2" are changed within the functions you called. That's why you had to save them into "d1" and "d2" before the first time function "ggt" is called. If you didn't expect the values changed within the function you have to declare local variables in "ggt" to take in "zahl1" and "zahl2". Now I'm not sure if function "kgV" should be LONG or SINGLE.
Otherwise I took the liberty of modifying this program to get away from Visual-Basic-style declarations, although QB64(PE) ignores them. Also make sure the functions return LONG integer.
I ask because I know next to no German: for input of "23" then "32" the results are "1" and "736", yes?
Code: (Select All)
'https://www.lernhelfer.de/schuelerlexikon/mathematik/artikel/euklidischer-algorithmus
'Groester gemeinsamer Teiler und kleintes gemeinsames Vielfaches
'GGT und kgV nach Euklid - 4. August 2022
Option _Explicit
'Declare Function ggt&(zahl1 As Long, zahl2 as Long)
'Declare Function kgV&(zahl1 As Long, zahl2 As Long, ggtErgebnis as Long)
Dim zahl1, zahl2 As Long
Dim d1, d2 As Long
Print
Print "Berechnet den GGT und das kgV nach Euklid"
Print
Input "Geben Sie die erste Zahl ein : ", zahl1
Input "Geben Sie die zweite Zahl ein: ", zahl2
'd1/2 = dummy - Zuweisung als Wert (Value)
'Sonst wird per Referenz auf zahl1/2 zugegriffen,
'und natuerlich ihr veraenderter Wert uebergeben
d1 = zahl1
d2 = zahl2
Print: Print
Print Using "Der gemeinsame Teiler von ### und ### ist: ###"; zahl1, zahl2, ggt(zahl1, zahl2)
Print: Print
'Nur mit den dummies funktioniert es
Print Using "Das kleinste gemeinsame Vielfache von ### und ### ist: ###,###"; d1, d2, kgV(d1, d2, ggt(zahl1, zahl2))
End 'Hauptprogramm
Function ggt& (zahl1 As Long, zahl2 As Long)
Dim temp As Long
While (zahl1 > 0)
If (zahl1 < zahl2) Then
temp = zahl1: zahl1 = zahl2: zahl2 = temp
End If
zahl1 = zahl1 - zahl2
Wend
ggt& = zahl2
End Function
Function kgV& (zahl1 As Long, zahl2 As Long, ggtErgebnis As Long)
Dim ergebnis As Long
ergebnis = ((zahl1 * zahl2) / ggtErgebnis)
kgV& = ergebnis
End Function