只输出整数部分和余数的快速除法程序,几百位的整数可以瞬间完成,最大不知道算到多少位:
(其中的乘法是模仿手工计算的速度慢,所以还有很大的提升空间)(不知道有没有错误的情况,欢迎试用和批评指导!)(修改了一下,关键是移动小数点有一类错误,已经改好,谁知道还有没有错误的类型?)
Private Sub Command1_Click() '快速除法程序
Dim a, b
a = Trim(Text1): b = Trim(Text2): b3 = b: a3 = a
If Len(b) = 1 Then
X1 = Mid(b, 1, 1): X2 = 1 / X1 - 0.01
Else
X1 = Mid(b, 1, 2): X2 = 10 / X1 - 0.01
End If
x = Mid(X2, 1, 4)
Y = 0: x3 = 0
sb = Len(a3) + Len(b3) - 1 + 10
If Len(a3) = Len(b3) And MBJC(Trim(a3), Trim(b3)) = 0 Then
a1 = 1
ElseIf MBJC(Mid(a3, 1, Len(b3)), Trim(b3)) = 0 And Val(Len(qdhz0(Trim(a3)))) = Val(Len(b3)) Then
a1 = 1 & String(Len(a3) - Len(qdhz0(Trim(a3))), "0")
Else
a = a & String(10, "0"): b = b & String(10 + Len(a3), "0")
x = qdqd0(ydxsd(Trim(x), Val(sb)))
Y1 = 2 & String(Val(sb), "0")
Do While MBJC(MPC(Trim(x), Trim(x3)), 1) >= 0
s3 = s3 + 1
Y = mbc2(Trim(x), MPC(Trim(Y1), mbc2(Trim(b), Trim(x), Val(sb))), Val(sb))
x3 = x
x = Trim(Y)
Loop
a1 = mbc2(Trim(Y), Trim(a), Val(sb))
s = Len(a3) - Len(b3)
a1 = qdqd0(Trim(a1))
If MBJC(Mid(a3, 1, Len(b3)), Trim(b3)) < 0 Then
a1 = tjxsd(Trim(a1), Len(a1) - s)
Else
a1 = tjxsd(Trim(a1), Len(a1) - s - 1)
End If
End If
If InStr(a1, ".") = 0 Then
a1 = a1
Else
a1 = Left(a1, InStr(a1, ".") - 1)
End If
ja = MPC(Trim(a3), MbC(Trim(b3), Trim(a1)))
Do While MBJC(Trim(ja), Trim(b3)) >= 0
ja = MPC(Trim(ja), Trim(b3))
s5 = s5 + 1
Loop
a1 = MPC1(Trim(a1), Trim(s5))
If ja = 0 Then
Text3 = a1
Else
Text3 = a1 & "/" & ja
End If
End Sub
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
End Sub
Private Function mbc2(sa As String, sb As String, sd As String) As String 'chengfa jingdu
Dim ja
If Trim(sa) = 0 Or Trim(sb) = 0 Then
mbc2 = 0
Else
ja = MbC(Trim(sa), Trim(sb))
If Val(Len(ja)) > Val(sd) Then
jb = Left(ja, Val(Len(ja)) - Val(sd))
mbc2 = jb
Else
mbc2 = 0
End If
End If
End Function
Private Function qdhz0(sa As String) As String
a = sa
Do While Right(a, 1) = "0"
a = Left(a, Len(a) - 1)
Loop
If a = "" Then
a = 0
Else
a = a
End If
qdhz0 = a
End Function
Private Function qdqd0(sa As String) As String
a = sa
Do While Left(a, 1) = "0"
a = Mid(a, 2)
Loop
If a = "" Then
a = 0
Else
a = a
End If
qdqd0 = a
End Function
Private Function tjxsd(sa As String, sd As String) As String
If Val(Len(sa)) > Val(sd) Then
tjxsd = Left(sa, Val(Len(sa)) - Val(sd)) & "." & Mid(sa, Val(Len(sa)) - Val(sd) + 1)
Else
If Val(Len(sa)) = Val(sd) Then
tjxsd = "0." & sa
Else
tjxsd = "0." & String(Val(sd) - Val(Len(sa)), "0") & Trim(sa)
End If
End If
End Function
Private Function ydxsd(sa As String, sd As String) As String
If Len(sa) = 1 And Val(sa) = 0 Then
ydxsd = 0
Else
sc = InStr(sa, ".")
If Val(sc) = 0 Then
ydxsd = sa & String(sd, "0")
Else
se = Left(sa, Val(sc) - 1)
sf = Right(sa, Len(sa) - Val(sc))
If Val(Len(sf)) >= Val(sd) Then
ydxsd = se & Mid(sf, 1, sd)
Else
ydxsd = se & sf & String(Val(sd) - Len(sf), "0")
End If
End If
End If
End Function
Public Function MbC(D1 As String, D2 As String) As String '乘法
Dim x, Y '两数长度
x = Len(D1): Y = Len(D2)
Dim a() As Integer
ReDim a(1 To x + Y, 1 To Y)
Dim I, J, C1, C2, CJ, JW
For J = Y To 1 Step -1 'D2
JW = 0 '进位清0
C2 = Mid$(D2, J, 1) '每位数
For I = x To 1 Step -1 'D1
C1 = Mid$(D1, I, 1) '每位数
CJ = C1 * C2 + JW '计算乘积
c = I + J: r = Y + 1 - J
a(c, r) = CJ Mod 10 '本位
JW = CJ \ 10 '进位
Next
a(c - 1, r) = JW
Next
Dim b() As Integer
ReDim b(1 To x + Y)
JW = 0
For I = x + Y To 1 Step -1
Bit = JW
For J = 1 To Y
Bit = Bit + a(I, J)
Next
b(I) = Bit Mod 10
JW = Bit \ 10
Next
If b(1) > 0 Then
MbC = MbC & b(1)
Else
MbC = MbC
End If
For I = 2 To x + Y
MbC = MbC & b(I)
Next
End Function
Public Function MPC(D1 As String, D2 As String) As String ';jianfaqi
Dim x, Y ';两数长度
If Len(D1) >= Len(D2) Then
D4 = String(Len(D1) - Len(D2), "0") & D2
d3 = D1
Else
D4 = D2
d3 = String(Len(D2) - Len(D1), "0") & D1
End If
x = Len(d3): Y = Len(D4)
Dim a() As Integer, B1() As Integer, C1() As Integer, E1() As Integer
ReDim a(1 To x)
ReDim B1(1 To Y)
ReDim C1(1 To x)
ReDim E1(1 To x)
Dim I, J, C2, CJ, JW
For J = Y To 1 Step -1 ';D2
JW = 1 ';yu jie weichuzhi
B1(J) = Mid(D4, J, 1) ';每位数
For I = x To 1 Step -1 ';D1
a(I) = Mid(d3, I, 1) ';每位数
C1(I) = 10 + a(I) - B1(I) - 1 + JW ';计算jia
JW = C1(I) \ 10
E1(I) = C1(I) Mod 10
Next
Next
For r = 1 To x
MPC = MPC & E1(r)
For I = 1 To Len(MPC)
If Not Mid(MPC, I, 1) = "0" Then
Exit For
End If
Next
strtmp = Mid(MPC, I)
If Len(strtmp) = 0 Then
MPC = "0"
Else
MPC = strtmp
End If
Next
End Function
Public Function MPC1(D1 As String, D2 As String) As String 'jiafa
Dim x, Y '两数长度
If Len(D1) >= Len(D2) Then
D4 = String(Len(D1) - Len(D2), "0") & D2
d3 = D1
Else
D4 = D2
d3 = String(Len(D2) - Len(D1), "0") & D1
End If
x = Len(d3): Y = Len(D4)
Dim a() As Integer, B1() As Integer, C1() As Integer, E1() As Integer
ReDim a(1 To x)
ReDim B1(1 To Y)
ReDim C1(1 To x)
ReDim E1(1 To x)
Dim I, J, C2, CJ, JW
For J = Y To 1 Step -1 'D2
JW = 0 '进位清0
B1(J) = Mid$(D4, J, 1) '每位数
For I = x To 1 Step -1 'D1
a(I) = Mid$(d3, I, 1) '每位数
C1(I) = a(I) + B1(I) + JW '计算jia
JW = C1(I) \ 10
E1(I) = C1(I) Mod 10
Next
Next
For r = 1 To x
If JW = 0 Then
MPC1 = MPC1 & E1(r)
Else
jc = jc & E1(r)
MPC1 = JW & jc
End If
Next
End Function
Public Function MBJC(D1 As String, D2 As String) As String ';bijiao
If Len(D1) <= 10 And Len(D2) <= 10 Then
If Val(D1) > Val(D2) Then
MBJC = 1
Else
If Val(D1) = Val(D2) Then
MBJC = 0
Else
MBJC = -1
End If
End If
Else
If Len(D1) > Len(D2) Then
MBJC = 1
Else
If Len(D1) < Len(D2) Then
MBJC = -1
Else
If Len(D1) = Len(D2) Then
Dim x, Y
x = Len(D1) \ 4: Y = Len(D2) \ 4
Dim a() As String, b() As String
ReDim a(4 To 4 * x + 4)
ReDim b(4 To 4 * Y + 4)
If Val(Left(D1, Len(D1) - 4 * x)) > Val(Left(D2, Len(D2) - 4 * Y)) Then
MBJC = 1
Else
If Val(Left(D1, Len(D1) - 4 * x)) < Val(Left(D2, Len(D2) - 4 * Y)) Then
MBJC = -1
Else
For I = 4 To 4 * x Step 4
a(I) = Mid(D1, Len(D1) - I + 1, 4)
b(I) = Mid(D2, Len(D2) - I + 1, 4)
Next
J = 4 * x
Do While a(J) = b(J) And J >= 8
J = J - 4
Loop
If Val(a(J)) - Val(b(J)) > 0 Then
MBJC = 1
Else
If Val(a(J)) - Val(b(J)) < 0 Then
MBJC = -1
Else
MBJC = 0
End If
End If
End If
End If
End If
End If
End If
End If
End Function
[此贴子已经被作者于2021-2-18 22:25编辑过]