;*** Wandelt Dezimalzahlen in Brüche um
;*** 9.10.2005 by Triton
;*** http://www.silizium-net.de
Graphics 640,480,16,2
zahl$= Input("Zahl:")
Print "Diese Zahl als Bruch: "+dez2bruch$(zahl$)
WaitKey
End
;---
Function dez2bruch$(zahl$)
If zahl$="0" Then Return "0"
If Int(zahl$) < 0 Then
n=1
zahl$=Right(zahl$, Len(zahl$)-1)
End If
For a1 = 1 To Len(zahl)
nr$ = Mid(zahl$,a1,1)
b=b+1
If Not nr$="," Then zaehler1$=zaehler1$+Mid(zahl$,a1,1)
If nr$="," Then c=b
Next
If c <= Len(zaehler1$) Then nenner1$="1"+String$("0",Len(zaehler1$)-c+1)
If c = 0 Then nenner1$="1"
zaehler2$=Str(Int(zaehler1$)/ggT(Int(zaehler1$),Int(nenner1$)))
nenner2$=Str(Int(nenner1$)/ggT(Int(zaehler1$),Int(nenner1$)))
If n=0 Then bruch$=zaehler2$+"/"+nenner2$
If n=1 Then bruch$="-"+zaehler2$+"/"+nenner2$
Return bruch$
End Function
;---
Function ggT(zahl1,zahl2)
If zahl1 > zahl2 Then b=zahl2
If zahl2 > zahl1 Then b=zahl1
If zahl1 = zahl2 Then b=zahl1
For a = b To 1 Step -1
If zahl1 Mod a = 0 And zahl2 Mod a = 0 Then Return a
Next
End Function
|