Sub 有理数から整数()
Dim sign As Integer
Dim k As Long
Dim m0 As Integer
Dim n0 As Integer
Dim m As Integer
Dim n As Integer
Set ws = Worksheets("Sheet1")
sign = 1
k = 1
m = ws.Cells(5, 4)
n = ws.Cells(5, 6)
If n = 0 Then
MsgBox "N=0"
End
ElseIf n < 0 Then
n = -n
m = -m
End If
If m = 0 Then
ws.Range("D7") = 0
ws.Range("F7") = 1
ws.Range("D11") = 0
End
End If
If m < 0 Then
m = -m
sign = -sign
End If
m0 = m
n0 = n
Do
If m = n Then
Exit Do
End If
If m > n Then
m = m - n
k = k * 2 + 1
End If
If m < n Then
n = n - m
k = k * 2
End If
Loop
n0 = n0 / m
m0 = sign * m0 / m
k = sign * k
ws.Range("D7") = m0
ws.Range("F7") = n0
ws.Range("D11") = k
End Sub
|
Sub 整数から有理数()
Dim sign As Integer
Dim k As Long
Dim m As Integer
Dim n As Integer
Dim od As Integer
Dim k0 As Long
Set ws = Worksheets("Sheet1")
sign = 1
m = 1
n = 1
k = ws.Cells(15, 4)
If k = 0 Then
ws.Range("D19") = 0
ws.Range("F19") = 1
End
End If
If k < 0 Then
k = -k
sign = -sign
End If
Do
If k = 1 Then
Exit Do
End If
od = k / 2
od = k - od * 2
If od = 0 Then
n = m + n
k = k / 2
Else
m = m + n
k = (k - 1) / 2
End If
Loop
m = sign * m
ws.Range("D19") = m
ws.Range("F19") = n
End Sub
|