标题:[讨论]大数运算的实现
只看楼主
xhwxwk
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-8-24
 问题点数:0 回复次数:1 
[讨论]大数运算的实现

小弟不才,只实现了加、减、乘、整数次乘方以及获取正负号、绝对值,大家看看怎样实现除法、小数次乘方和开方。 '获取正负号 Public Shared Function GetSign(ByVal Number As String) As Int16 If Not IsNumeric(Number) Then Return 0 If Number.Chars(0) = "+" Then Return 1 If Number.Chars(0) = "-" Then Return -1 If Abs(Number) = "0" Then Return 0 Return 1 End Function

'获取绝对值 Public Shared Function Abs(ByVal Number As String) As String If Not IsNumeric(Number) Then Return "0" If Number.Chars(0) = "+" Then Return Number.Substring(1) If Number.Chars(0) = "-" Then Return Number.Substring(1) Dim s As String = Number.TrimStart("0") If s.Chars(0) = "+" Or s.Chars(0) = "-" Then s = s.Substring(1) If Number.IndexOf(".") > -1 Then s = s.TrimEnd("0") If s = "." Or s = "" Then Return "0" Return Number End Function

'比较大小 Public Shared Function Compare(ByVal Number1 As String, ByVal Number2 As String) As Int16 If Not IsNumeric(Number1) Or Not IsNumeric(Number2) Then Exit Function Dim Sign1 As Int16, Sign2 As Int16 Sign1 = GetSign(Number1) Sign2 = GetSign(Number2) Number1 = Abs(Number1) Number2 = Abs(Number2) If Sign1 > Sign2 Then Return 1 ElseIf Sign2 > Sign1 Then Return -1 ElseIf Sign1 = 1 Then Dim j As Integer If Number1.IndexOf(".") > -1 Then j = Number1.IndexOf(".") Else j = Number1.Length Dim k As Integer If Number2.IndexOf(".") > -1 Then k = Number2.IndexOf(".") Else k = Number2.Length If j > k Then Return 1 ElseIf j < k Then Return -1 Else Dim m As String = Number1.Substring(0, j), n As String = Number2.Substring(0, k) Dim c As String = "", d As String = "" If Number1.IndexOf(".") > -1 Then c = Number1.Substring(j + 1) If Number2.IndexOf(".") > -1 Then d = Number2.Substring(k + 1) If m.Length < n.Length Then m = New String("0", n.Length - m.Length) & m Else n = New String("0", m.Length - n.Length) & n End If If c.Length < d.Length Then c = c & New String("0", d.Length - c.Length) Else d = d & New String("0", c.Length - d.Length) End If Dim i As Integer For i = 0 To m.Length - 1 Dim u As Char = m.Chars(i) Dim f As Char = n.Chars(i) If Int32.Parse(u) > Int32.Parse(f) Then Return 1 ElseIf Int32.Parse(u) < Int32.Parse(f) Then Return -1 End If Next For i = 0 To c.Length - 1 Dim u As Char = c.Chars(i) Dim f As Char = d.Chars(i) If Int32.Parse(u) > Int32.Parse(f) Then Return 1 ElseIf Int32.Parse(u) < Int32.Parse(f) Then Return -1 End If Next Return 0 End If ElseIf Sign1 = -1 Then Dim j As Integer If Number1.IndexOf(".") > -1 Then j = Number1.IndexOf(".") Else j = Number1.Length Dim k As Integer If Number2.IndexOf(".") > -1 Then k = Number2.IndexOf(".") Else k = Number2.Length If j > k Then Return -1 ElseIf j < k Then Return 1 Else Dim m As String = Number1.Substring(0, j), n As String = Number2.Substring(0, k) Dim c As String = "", d As String = "" If Number1.IndexOf(".") > -1 Then c = Number1.Substring(j + 1) If Number2.IndexOf(".") > -1 Then d = Number2.Substring(k + 1) If m.Length < n.Length Then m = New String("0", n.Length - m.Length) & m Else n = New String("0", m.Length - n.Length) & n End If If c.Length < d.Length Then c = c & New String("0", d.Length - c.Length) Else d = d & New String("0", c.Length - d.Length) End If Dim i As Integer For i = 0 To m.Length - 1 Dim u As Char = m.Chars(i) Dim f As Char = n.Chars(i) If Int32.Parse(u) > Int32.Parse(f) Then Return -1 ElseIf Int32.Parse(u) < Int32.Parse(f) Then Return 1 End If Next For i = 0 To c.Length - 1 Dim u As Char = c.Chars(i) Dim f As Char = d.Chars(i) If Int32.Parse(u) > Int32.Parse(f) Then Return -1 ElseIf Int32.Parse(u) < Int32.Parse(f) Then Return 1 End If Next Return 0 End If Else Return 0 End If End Function

'加法运算 Public Shared Function Plus(ByVal Number1 As String, ByVal Number2 As String) As String If Not IsNumeric(Number1) Or Not IsNumeric(Number2) Then Exit Function Dim Sign1 As Int16 = GetSign(Number1) Dim Sign2 As Int16 = GetSign(Number2) If Sign1 = 0 And Sign2 = 0 Then Return "0" Number1 = Abs(Number1) Number2 = Abs(Number2) If Compare(Number2, Number1) > 0 Then Dim s As String = Number1 Number1 = Number2 Number2 = s Dim ii As Int16 = Sign1 Sign1 = Sign2 Sign2 = ii End If Dim j As Integer, b As Integer If Number1.IndexOf(".") = -1 Then j = Number1.Length Else j = Number1.IndexOf(".") If Number2.IndexOf(".") = -1 Then b = Number2.Length Else b = Number2.IndexOf(".") Dim m As String = Number1.Substring(0, j), n As String = Number2.Substring(0, b) Dim c As String = "", d As String = "" If Number1.IndexOf(".") > -1 Then c = Number1.Substring(j + 1) If Number2.IndexOf(".") > -1 Then d = Number2.Substring(b + 1) If m.Length < n.Length Then m = New String("0", n.Length - m.Length) & m Else n = New String("0", m.Length - n.Length) & n End If If c.Length < d.Length Then c = c & New String("0", d.Length - c.Length) Else d = d & New String("0", c.Length - d.Length) End If Dim i As Integer Dim ResultA As String = "" Dim ResultB As String = "" Dim Flag As Boolean If Sign1 = Sign2 Then For i = c.Length - 1 To 0 Step -1 Dim t As Char = c.Chars(i) Dim u As Char = d.Chars(i) Dim k As Integer = Int32.Parse(t) + Int32.Parse(u) If Flag Then k = k + 1 : Flag = False If k > 9 Then k = k - 10 : Flag = True ResultA = k.ToString & ResultA Next For i = m.Length - 1 To 0 Step -1 Dim t As Char = m.Chars(i) Dim u As Char = n.Chars(i) Dim k As Integer = Int32.Parse(t) + Int32.Parse(u) If Flag Then k = k + 1 : Flag = False If k > 9 Then k = k - 10 : Flag = True ResultB = k.ToString & ResultB Next If Flag Then ResultB = "1" & ResultB ResultA = ResultA.TrimEnd("0") ResultB = ResultB.TrimStart("0") Dim Result As String = ResultB & IIf(ResultA <> "", ".", "") & ResultA If Result = "" Then Result = "0" Else Result = IIf(Sign1 = -1, "-", "") & Result Return Result Else For i = c.Length - 1 To 0 Step -1 Dim t As Char = c.Chars(i) Dim u As Char = d.Chars(i) Dim k As Integer = Int32.Parse(t) Dim q As Integer = Int32.Parse(u) If Flag Then k = k - 1 : Flag = False If k < q Then k = k + 10 : Flag = True Dim p As Integer = k - q ResultA = p.ToString & ResultA Next For i = m.Length - 1 To 0 Step -1 Dim t As Char = m.Chars(i) Dim u As Char = n.Chars(i) Dim k As Integer = Int32.Parse(t) Dim q As Integer = Int32.Parse(u) If Flag Then k = k - 1 : Flag = False If i > 0 Then If k < q Then k = k + 10 : Flag = True End If Dim p As Integer = k - q ResultB = p.ToString & ResultB Next If Flag Then ResultB = "1" & ResultB ResultA = ResultA.TrimEnd("0") ResultB = ResultB.TrimStart("0") Dim Result As String = ResultB & IIf(ResultA <> "", ".", "") & ResultA If Result = "" Then Result = "0" Else Result = IIf(Sign1 = -1, "-", "") & Result Return Result End If End Function

'减法运算;减去一个数等于加上它的相反数 Public Shared Function Minus(ByVal Number1 As String, ByVal Number2 As String) As String If Not IsNumeric(Number1) Or Not IsNumeric(Number2) Then Exit Function Dim s2 As Int16 = GetSign(Number2) If s2 = -1 Then Number2 = Number2.Substring(1) ElseIf s2 = 1 Then If Number2.Chars(0) = "+" Then Number2 = "-" & Number2.Substring(1) Else Number2 = "-" & Number2 End If Else Return Number1 End If Return Plus(Number1, Number2) End Function

'乘法运算 Public Shared Function Multiply(ByVal Number1 As String, ByVal Number2 As String) As String If Not IsNumeric(Number1) Or Not IsNumeric(Number2) Then Exit Function Dim Sign1 As Int16 = GetSign(Number1), Sign2 = GetSign(Number2) Dim Sign As Int16 If Sign1 = 0 Or Sign2 = 0 Then Return "0" If Sign1 = Sign2 Then Sign = 1 Else Sign = -1 End If Number1 = Abs(Number1) Number2 = Abs(Number2) Dim j As Integer = Number1.IndexOf(".") Dim k As Integer = Number2.IndexOf(".") If j = -1 Then j = 0 Else j = Number1.Length - j - 1 If k = -1 Then k = 0 Else k = Number2.Length - k - 1 Dim m As String = Replace(Number1, ".", ""), n As String = Replace(Number2, ".", "") Dim p As Integer, q As Integer Dim JinWei As Integer, Result As String = "" For p = n.Length - 1 To 0 Step -1 JinWei = 0 Dim r As String = "" For q = m.Length - 1 To 0 Step -1 Dim a As Char = n.Chars(p) Dim b As Char = m.Chars(q) Dim w As Integer = Int32.Parse(a) * Int32.Parse(b) + JinWei : JinWei = 0 If w > 9 Then JinWei = w \ 10 : w = w - (w \ 10) * 10 r = w.ToString & r Next If JinWei > 0 Then r = JinWei.ToString & r r = r & New String("0", n.Length - p - 1) If p < n.Length - 1 Then Result = Plus(Result, r) Else Result = r Next Dim u As Integer = j + k If u > 0 Then Result = Result.Insert(Result.Length - u, ".") Result = Result.TrimStart("0") If u > 0 Then Result = Result.TrimEnd("0") If Result.Chars(Result.Length - 1) = "." Then Result = Result.Substring(0, Result.Length - 1) If Result = "" Then Result = "0" If Result.Chars(0) = "." Then Result = "0" & Result If Sign = -1 And Result <> "0" Then Result = "-" & Result Return Result End Function

'乘方运算 Public Shared Function Power(ByVal Number As String, ByVal PowerTime As Integer) As String If Not IsNumeric(Number) Then Exit Function If PowerTime = 0 Then Return 1 Dim i As Integer Dim Result As String = "1" For i = 1 To Math.Abs(PowerTime) Result = Multiply(Number, Result) Next If PowerTime < 0 Then Result = "1/" & Result Return Result End Function

搜索更多相关主题的帖子: 大数 Number Return Then 
2005-08-28 09:23
jenylexie
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2005-8-2
得分:0 
这觉得有些东本没有必要,程序完全可以再精减

新手,多多关照。QQ274689413 通过验证“\"与我同在
2005-08-30 21:32



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




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

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