回复 4楼 东海ECS
下面提供了完整的VBS代码,来修正一下吧。
' 大数加法
Function BigNumAdd(x, y)
Dim r, temp1, temp2
Dim i, c, sum
Dim j, k
Dim lenx, leny
lenx = Len(x)
leny = Len(y)
c = 0
r = ""
For i = lenx To 1 Step -1
j = CInt(Mid(x, i, 1))
If leny >= i Then k = CInt(Mid(y, i, 1)) Else k = 0
sum = j + k + c
If sum >= 10 Then
c = 1
sum = sum - 10
Else
c = 0
End If
temp1 = ChrW(48 + sum)
temp2 = r
r = temp1 & temp2
Next
If c > 0 Then r = "1" & r
BigNumAdd = r
End Function
' 大数减法,x > y
Function BigNumSub(x, y)
Dim r, temp1, temp2
Dim i, c, diff
Dim j, k
Dim lenx, leny
lenx = Len(x)
leny = Len(y)
c = 0
r = ""
For i = lenx To 1 Step -1
j = CInt(Mid(x, i, 1))
If leny >= i Then k = CInt(Mid(y, i, 1)) Else k = 0
diff = j - k - c
If diff < 0 Then
c = 1
diff = diff + 10
Else
c = 0
End If
temp1 = ChrW(48 + diff)
temp2 = r
r = temp1 & temp2
Next
While Len(r) > 1 And Left(r, 1) = "0"
r = Right(r, Len(r) - 1)
Wend
BigNumSub = r
End Function
' 大数乘法
Function BigNumMul(x, y)
Dim r, temp, temp1, temp2
Dim lenx, leny
Dim i, j, k, carry, sum
lenx = Len(x)
leny = Len(y)
temp = String(lenx + leny, "0")
For i = leny To 1 Step -1
j = CInt(Mid(y, i, 1))
carry = 0
For k = lenx To 1 Step -1
sum = j * CInt(Mid(x, k, 1)) + carry + CInt(Mid(temp, i + k, 1))
carry = sum \ 10
temp1 = ChrW(48 + (sum Mod 10))
temp2 = Mid(temp, 1, i + k - 1) & temp1 & Mid(temp, i + k + 1)
temp = temp2
Next
If carry > 0 Then
temp1 = ChrW(48 + carry)
temp2 = Mid(temp, 1, i + lenx) & temp1 & Mid(temp, i + lenx + 2)
temp = temp2
End If
Next
r = temp
While Len(r) > 1 And Left(r, 1) = "0"
r = Right(r, Len(r) - 1)
Wend
BigNumMul = r
End Function
' 大数除法,x > y
Function BigNumDiv(x, y)
Dim r, quotient, remainder, temp, temp1, temp2
Dim lenx, leny
Dim i, j
lenx = Len(x)
leny = Len(y)
If lenx < leny Then
BigNumDiv = "0"
Exit Function
End If
quotient = ""
r = Mid(x, 1, leny)
For i = leny + 1 To lenx
If r = "0" Then r = Mid(x, i, 1) Else r = r & Mid(x, i, 1)
quotient = quotient & "0"
j = 1
Do While BigNumSub(r, y) <> "" And CInt(BigNumSub(r, y)) >= 0
r = BigNumSub(r, y)
j = j + 1
Loop
quotient = Left(quotient, Len(quotient) - 1) & j - 1
Next
While Len(quotient) > 1 And Left(quotient, 1) = "0"
quotient = Right(quotient, Len(quotient) - 1)
Wend
If Len(quotient) = 0 Then quotient = "0"
While Len(r) > 1 And Left(r, 1) = "0"
r = Right(r, Len(r) - 1)
Wend
remainder = r
BigNumDiv = quotient
End Function
' 大数除法,x > y
Function BigNumDiv(x, y)
Dim r, quotient, remainder, temp, temp1, temp2
Dim lenx, leny
Dim i, j
lenx = Len(x)
leny = Len(y)
If lenx < leny Then
BigNumDiv = "0"
Exit Function
End If
quotient = ""
r = Mid(x, 1, leny)
For i = leny + 1 To lenx
If r = "0" Then r = Mid(x, i, 1) Else r = r & Mid(x, i, 1)
quotient = quotient & "0"
j = 1
Do While BigNumSub(r, y) <> "" And CLng(BigNumSub(r, y)) >= 0
r = BigNumSub(r, y)
j = j + 1
Loop
quotient = Left(quotient, Len(quotient) - 1) & j - 1
Next
While Len(quotient) > 1 And Left(quotient, 1) = "0"
quotient = Right(quotient, Len(quotient) - 1)
Wend
If Len(quotient) = 0 Then quotient = "0"
While Len(r) > 1 And Left(r, 1) = "0"
r = Right(r, Len(r) - 1)
Wend
remainder = r
BigNumDiv = quotient
End Function
MsgBox BigNumAdd("12345678901234567890", "98765432109876543210") ' 显示 111111111111111111100
MsgBox BigNumSub("98765432109876543210", "12345678901234567890") ' 显示 86419753208641975320
MsgBox BigNumMul("123456789012345678901234567890", "987654321098765432109876543210") ' 显示 12193263113702179556720073558516635681494230864252625391052410
MsgBox BigNumDiv("98765432109876543210", "12345678901234567890")