标题:VBScript 大数除法 溢出,具体如何修正?
取消只看楼主
yuma
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:银河系
等 级:贵宾
威 望:33
帖 子:1883
专家分:2904
注 册:2009-12-22
结帖率:89.13%
已结贴  问题点数:20 回复次数:1 
VBScript 大数除法 溢出,具体如何修正?
程序代码:
' 大数除法,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 BigNumDiv("12193263113702179556720073558516635681494230864252625391052410", "987654321098765432109876543210")  '显示 123456789012345678901234567890


会的麻烦修正,感谢!
搜索更多相关主题的帖子: 除法 And 大数 If Then 
2023-04-23 21:11
yuma
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:银河系
等 级:贵宾
威 望:33
帖 子:1883
专家分:2904
注 册:2009-12-22
得分:0 
回复 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")

心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2023-04-24 19:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-512020-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053188 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved