| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 965 人关注过本帖
标题:[讨论]小弟写了一个大数运算的类,只可以做加法、乘法、减法(除法没搞定) ...
只看楼主 加入收藏
xhwxwk
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-8-24
收藏
 问题点数:0 回复次数:0 
[讨论]小弟写了一个大数运算的类,只可以做加法、乘法、减法(除法没搞定)那位大虾

小弟写了一个大数运算的类,只可以做加法、乘法、减法(除法没搞定)那位大虾帮我研究一下除法运算。另外,也帮我把算法精简一下。 Public Class BigNumberCalculate '获取正负号 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 Try If Double.Parse(Number) = 0 Then Return 0 Finally End Try 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) Try If Double.Parse(Number) = 0 Then Return "0" Finally End Try 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 Number2 = 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 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 Sign = -1 And Result <> "0" Then Result = "-" & Result Return Result End Function End Class

搜索更多相关主题的帖子: 减法 除法 乘法 加法 大数 
2005-08-26 08:43
快速回复:[讨论]小弟写了一个大数运算的类,只可以做加法、乘法、减法(除法没搞 ...
数据加载中...
 
   



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

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