类型 |
计算公式 |
结果 |
||
有限小数 |
格式 |
a. b |
(a*x+b)/x |
|
示例 |
3. 14 |
(3*10^ 2+14)/10 2 |
157/50 |
|
纯循环小数 |
格式 |
a. (b) |
(a*(x-1)+b)/(x-1) |
|
示例 |
3. (14) |
(3*(10^ 2-1)+14)/(10^2-1) |
311/99 |
|
混循环小数 |
格式 |
a.b(c) |
(a*(t-1)*x+ (b*t+c-b))/((t-1)*x) |
|
示例 |
3. 14(159) |
(3*(10 3-1)*10 2+(14*10 3+159-14))/(10^ 3-1)*10^2) |
20923/6660 |
|
说明: ()内的数字串表示循环节 x=10 Len(b) t=10' Len(c) |
程序运行时,在文本框Text1中输入小数,单击“转化”按钮,输出对应的分数,界面如图所示。
Private Sub Command1_ Click()
Dim s As String, xS As String, zS As String
Dim fz As Long, fm As Long
Dim i As Integer
Dim x As Integer, y As Integer
s = Text1.Text
i=1
Do While Mid(s, i, 1) <> "."
i=i+1
Loop
zs =
xs=Mid(s, i+1)
If not(f(xs)) Then
fm = power (Len(xs))
fz = val(zs) * fm + vas (xs)
Else
If Mid(xs, 1, 1) = "(" Then
xs = Mid(xs, 2, Len(xs) - 2)
fm = power(Len(xs)) - 1
fz = Val(zs) * fm + Val(xs) .
Else
x=0: y=0: i=1
Do While Mid(xs, i, 1) <> "("
i=i+1
Loop
x=i-1
y=Len(xs)-i-1
fm =
fz= Val (Mid(xs, 1, x)) * power(y)+Val (Mid(xs, i+1, y))-Val (Mid(xs, 1, x))
fz=Val(zs)*fm+fz
End If
End If
Label2. Caption = Str(fz / gcd(fz, fm)) + "/"+ Str(fm / gcd(fz, fm))
End Sub
Function gcd (ByVal m As Long, ByVal n As Long) As Long
Dim r As Long
Do While m Mod n<>0
r= m Mod n
m=n
n=r
Loop
End Function
Function f(s As String) As Boolean '循环节判断
'函数返回True代表循环小数,False 不是循环小数,代码略
End Function
Function power(x As Integer) As Long '返回10的x次方
'代码略
End Function