IamOverlord
دوشنبه 13 شهریور 1391, 20:04 عصر
سلام...
ببینید من در مورد چهار عمل اصلی روی اعداد غیر اعشاری مشکلی ندارم. اما مسئله در مورد محاسبات با ارقام اعشاری خیلی زیاد هست. قبلا یه ماژولی سعی کردم بنویسم ولی بهینه و درست-حسابی نشد. تو این فکرم که اون رو بذارم و اگه قراره رو چیزی کار کنیم رو همون کار کنیم.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
''  MODULE NAME : BIG NUMBERS                         ''
''  PROGRAMMER : OVERLORD.SANDKING ( MASOOD LAPECHI)  ''
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
Public Function ToBigNumber(ByVal Number) As String
    If Number = 0 Then
        res = "0.0"
    Else
        If Number > 0 Then
            res = Trim(Str(Number))
            If Mid(res, 1, 1) = "." Then res = "0" + res
            res = "+" + res
            For i = 1 To Len(res)
                If Mid(res, i, 1) = "." Then f = 1
            Next i
            If f <> 1 Then res = res + ".0"
        Else
            res = Trim(Str(Number))
            For i = 1 To Len(res)
                If Mid(res, i, 1) = "." Then f = 1
            Next i
            If f <> 1 Then res = res + ".0"
        End If
    End If
    ToBigNumber = res
End Function
Public Function Compare(ByVal strNumber1 As String, ByVal strNumber2 As String)
    n1 = strNumber1
    n2 = strNumber2
    If Mid(n1, 1, 1) = Mid(n2, 1, 1) Then
        For i = 1 To Len(n1)
            If Mid(n1, i, 1) = "." Then n1dpp = i
        Next i
        For i = 1 To Len(n2)
            If Mid(n2, i, 1) = "." Then n2dpp = i
        Next i
        n1adpc = Len(n1) - n1dpp - 1
        n1bdpc = n1dpp - 1
        n2adpc = Len(n2) - n2dpp - 1
        n2bdpc = n2dpp - 1
        If n1adpc < n2adpc Then
            n1 = n1 + String(n2adpc - n1adpc, "0")
        Else
            n2 = n2 + String(n1adpc - n2adpc, "0")
        End If
        If n1bdpc < n2bdpc Then
            n1 = Mid(n1, 1, 1) + String(n2bdpc - n1bdpc, "0") + Mid(n1, 2, Len(n1) - 1)
        Else
            n2 = Mid(n2, 1, 1) + String(n1bdpc - n2bdpc, "0") + Mid(n2, 2, Len(n2) - 1)
        End If
        For i = 1 To Len(n1)
            If Mid(n1, i, 1) = "." Then dpp = i
        Next i
        n1wdp = Mid(n1, 1, dpp - 1) + Mid(n1, dpp + 1, Len(n1) - dpp)
        n2wdp = Mid(n2, 1, dpp - 1) + Mid(n2, dpp + 1, Len(n2) - dpp)
        res = 0
        For i = Len(n1wdp) To 2 Step -1
            If Val(Mid(n1wdp, i, 1)) < Val(Mid(n2wdp, i, 1)) Then res = 1
            If Val(Mid(n1wdp, i, 1)) > Val(Mid(n2wdp, i, 1)) Then res = 2
        Next i
    Else
        If Mid(n1, 1, 1) = "+" Then
            res = 2
        Else
            If Mid(n2, 1, 1) = "+" Then
                res = 1
            Else
                If Mid(n1, 1, 1) = "0" Then
                    res = 2
                Else
                    res = 1
                End If
            End If
        End If
    End If
    Compare = res
End Function
Public Function Sum(ByVal strNumber1 As String, ByVal strNumber2 As String) As String
    n1 = strNumber1
    n2 = strNumber2
    If Mid(n1, 1, 1) = "+" And Mid(n2, 1, 1) = "+" Then
        For i = 1 To Len(n1)
            If Mid(n1, i, 1) = "." Then n1dpp = i
        Next i
        For i = 1 To Len(n2)
            If Mid(n2, i, 1) = "." Then n2dpp = i
        Next i
        n1adpc = Len(n1) - n1dpp - 1
        n1bdpc = n1dpp - 1
        n2adpc = Len(n2) - n2dpp - 1
        n2bdpc = n2dpp - 1
        If n1adpc < n2adpc Then
            n1 = n1 + String(n2adpc - n1adpc, "0")
        Else
            n2 = n2 + String(n1adpc - n2adpc, "0")
        End If
        If n1bdpc < n2bdpc Then
            n1 = Mid(n1, 1, 1) + String(n2bdpc - n1bdpc, "0") + Mid(n1, 2, Len(n1) - 1)
        Else
            n2 = Mid(n2, 1, 1) + String(n1bdpc - n2bdpc, "0") + Mid(n2, 2, Len(n2) - 1)
        End If
        For i = 1 To Len(n1)
            If Mid(n1, i, 1) = "." Then dpp = i
        Next i
        n1wdp = Mid(n1, 1, dpp - 1) + Mid(n1, dpp + 1, Len(n1) - dpp)
        n2wdp = Mid(n2, 1, dpp - 1) + Mid(n2, dpp + 1, Len(n2) - dpp)
        For i = Len(n1wdp) To 2 Step -1
            n3wdp = Trim(Str((Val(Mid(n1wdp, i, 1)) + Val(Mid(n2wdp, i, 1)) + r) Mod 10)) + n3wdp
            r = (Val(Mid(n1wdp, i, 1)) + Val(Mid(n2wdp, i, 1)) + r) \ 10
        Next i
        If r <> 0 Then n3wdp = Trim(Str(r)) + n3wdp
        n3wdp = "+" + n3wdp
        If r <> 0 Then
            n3 = Mid(n3wdp, 1, dpp) + "." + Mid(n3wdp, dpp + 1, Len(n3wdp) - dpp)
        Else
            n3 = Mid(n3wdp, 1, dpp - 1) + "." + Mid(n3wdp, dpp, Len(n3wdp) - dpp + 1)
        End If
        While Mid(n3, Len(n3), 1) = "0" And Mid(n3, Len(n3) - 1, 1) <> "."
            n3 = Mid(n3, 1, Len(n3) - 1)
        Wend
        Sum = n3
        Exit Function
    End If
    If Mid(n1, 1, 1) = "-" And Mid(n2, 1, 1) = "-" Then
        n3 = Sum("+" + Mid(n1, 2, Len(n1) - 1), "+" + Mid(n2, 2, Len(n2) - 1))
        Sum = "-" + Mid(n3, 2, Len(n3) - 1)
        Exit Function
    End If
    If Mid(n1, 1, 1) = "+" And Mid(n2, 1, 1) = "-" Then
        n3 = Subtract(n1, "+" + Mid(n2, 2, Len(n2) - 1))
        Sum = n3
        Exit Function
    End If
    If Mid(n1, 1, 1) = "-" And Mid(n2, 1, 1) = "+" Then
        n3 = Subtract(n2, "+" + Mid(n1, 2, Len(n1) - 1))
        Sum = n3
        Exit Function
    End If
    If Mid(n1, 1, 1) = "0" Then Sum = n2 Else Sum = n1
End Function
Public Function Subtract(ByVal strNumber1 As String, ByVal strNumber2 As String) As String
    n1 = strNumber1
    n2 = strNumber2
    If Mid(n1, 1, 1) = "+" And Mid(n2, 1, 1) = "+" Then
        If Compare(n1, n2) <> 1 Then
            For i = 1 To Len(n1)
                If Mid(n1, i, 1) = "." Then n1dpp = i
            Next i
            For i = 1 To Len(n2)
                If Mid(n2, i, 1) = "." Then n2dpp = i
            Next i
            n1adpc = Len(n1) - n1dpp - 1
            n1bdpc = n1dpp - 1
            n2adpc = Len(n2) - n2dpp - 1
            n2bdpc = n2dpp - 1
            If n1adpc < n2adpc Then
                n1 = n1 + String(n2adpc - n1adpc, "0")
            Else
                n2 = n2 + String(n1adpc - n2adpc, "0")
            End If
            If n1bdpc < n2bdpc Then
                n1 = Mid(n1, 1, 1) + String(n2bdpc - n1bdpc, "0") + Mid(n1, 2, Len(n1) - 1)
            Else
                n2 = Mid(n2, 1, 1) + String(n1bdpc - n2bdpc, "0") + Mid(n2, 2, Len(n2) - 1)
            End If
            For i = 1 To Len(n1)
                If Mid(n1, i, 1) = "." Then dpp = i
            Next i
            n1wdp = Mid(n1, 1, dpp - 1) + Mid(n1, dpp + 1, Len(n1) - dpp)
            n2wdp = Mid(n2, 1, dpp - 1) + Mid(n2, dpp + 1, Len(n2) - dpp)
            For i = Len(n1wdp) To 2 Step -1
                If Val(Mid(n1wdp, i, 1)) < Val(Mid(n2wdp, i, 1)) + r Then
                    n3wdp = Trim(Str(Val(Mid(n1wdp, i, 1)) + 10 - (Val(Mid(n2wdp, i, 1)) + r))) + n3wdp
                    r = 1
                Else
                    n3wdp = Trim(Str(Val(Mid(n1wdp, i, 1)) - (Val(Mid(n2wdp, i, 1)) + r))) + n3wdp
                    r = 0
                End If
            Next i
            If r <> 0 Then n3wdp = "9" + n3wdp
            n3wdp = "+" + n3wdp
            If r <> 0 Then
                n3 = Mid(n3wdp, 1, dpp) + "." + Mid(n3wdp, dpp + 1, Len(n3wdp) - dpp)
            Else
                n3 = Mid(n3wdp, 1, dpp - 1) + "." + Mid(n3wdp, dpp, Len(n3wdp) - dpp + 1)
            End If
        Else
            n3 = Subtract(n2, n1)
            If Mid(n3, 1, 1) = "+" Then n3 = "-" + Mid(n3, 2, Len(n3) + 1)
        End If
        While Mid(n3, 2, 1) = "0" And Mid(n3, 3, 1) <> "."
            n3 = Mid(n3, 1, 1) + Mid(n3, 3, Len(n3) - 2)
        Wend
        While Mid(n3, Len(n3), 1) = "0" And Mid(n3, Len(n3) - 1, 1) <> "."
            n3 = Mid(n3, 1, Len(n3) - 1)
        Wend
        For i = 1 To Len(n3)
            If Mid(n3, i, 1) <> "+" And Mid(n3, i, 1) <> "-" And Mid(n3, i, 1) <> "." And Mid(n3, i, 1) <> "0" Then f = 1
        Next i
        If f = 0 Then n3 = "0.0"
        Subtract = n3
        Exit Function
    End If
    If Mid(n1, 1, 1) = "-" And Mid(n2, 1, 1) = "-" Then
        n3 = Subtract("+" + Mid(n2, 2, Len(n2) - 1), "+" + Mid(n1, 2, Len(n1) - 1))
        Subtract = n3
        Exit Function
    End If
    If Mid(n1, 1, 1) = "+" And Mid(n2, 1, 1) = "-" Then
        n3 = Sum(n1, "+" + Mid(n2, 2, Len(n2) - 1))
        Subtract = n3
        Exit Function
    End If
    If Mid(n1, 1, 1) = "-" And Mid(n2, 1, 1) = "+" Then
        n3 = Sum(n1, "-" + Mid(n2, 2, Len(n2) - 1))
        Subtract = n3
        Exit Function
    End If
    If Mid(n2, 1, 1) = "0" Then
        Subtract = n1
    Else
        If Mid(n2, 1, 1) = "+" Then
            Subtract = "-" + Mid(n2, 2, Len(n2) - 1)
        Else
            Subtract = "+" + Mid(n2, 2, Len(n2) - 1)
        End If
    End If
End Function
Public Function Multiply(ByVal strNumber1 As String, ByVal strNumber2 As String) As String
    Dim N() As String
    n1 = strNumber1
    n2 = strNumber2
    If n1 = "0.0" Or n2 = "0.0" Then
        Multiply = "0.0"
        Exit Function
    End If
    For i = 1 To Len(n1)
        If Mid(n1, i, 1) = "." Then n1dpp = i
    Next i
    For i = 1 To Len(n2)
        If Mid(n2, i, 1) = "." Then n2dpp = i
    Next i
    n1adpc = Len(n1) - n1dpp
    n2adpc = Len(n2) - n2dpp
    n1wdp = Mid(n1, 1, n1dpp - 1) + Mid(n1, n1dpp + 1, Len(n1) - n1dpp)
    n2wdp = Mid(n2, 1, n2dpp - 1) + Mid(n2, n2dpp + 1, Len(n2) - n2dpp)
    ReDim N(Len(n2wdp) - 2) As String
    For i = 2 To Len(n2wdp)
        r = 0
        For j = Len(n1wdp) To 2 Step -1
            N(i - 2) = Trim(Str((Val(Mid(n2wdp, Len(n2wdp) + 2 - i, 1)) * Val(Mid(n1wdp, j, 1)) + r) Mod 10)) + N(i - 2)
            r = (Val(Mid(n2wdp, Len(n2wdp) + 2 - i, 1)) * Val(Mid(n1wdp, j, 1)) + r) \ 10
        Next j
        If r <> 0 Then N(i - 2) = Trim(Str(r)) + N(i - 2)
        f = 0
        For j = 1 To Len(N(i - 2))
            If Mid(N(i - 2), j, 1) <> "0" Then f = 1
        Next j
        If f = 0 Then
            N(i - 2) = "0.0"
        Else
            N(i - 2) = "+" + N(i - 2) + String(i - 2, "0") + ".0"
        End If
    Next i
    n3 = "0.0"
    For i = 1 To Len(n2wdp) - 1
        n3 = Sum(n3, N(i - 1))
        'Form1.List1.AddItem n3
    Next i
    n3adpc = n1adpc + n2adpc
    n3 = Mid(n3, 1, Len(n3) - 2)
    'n3 = Mid(n3, 2, Len(n3) - 1)
    If n3adpc > Len(n3) Then n3 = String(n3adpc - Len(n3) + 2, "0") + Mid(n3, 2, Len(n3) - 1)
    n3 = Mid(n3, 1, Len(n3) - n3adpc) + "." + Mid(n3, Len(n3) - n3adpc + 1, n3adpc)
    If Mid(n3, 2, 1) = "." Then
        n3 = Mid(n3, 1, 1) + "0." + Mid(n3, 3, Len(n3) - 3 + 1)
    End If
    If Mid(n1, 1, 1) = Mid(n2, 1, 1) Then
        n3 = "+" + Mid(n3, 2, Len(n3) - 1)
    Else
        n3 = "-" + Mid(n3, 2, Len(n3) - 1)
    End If
    While Mid(n3, 2, 1) = "0" And Mid(n3, 3, 1) <> "."
        n3 = Mid(n3, 1, 1) + Mid(n3, 3, Len(n3) - 2)
    Wend
    While Mid(n3, Len(n3), 1) = "0" And Mid(n3, Len(n3) - 1, 1) <> "."
        n3 = Mid(n3, 1, Len(n3) - 1)
    Wend
    Multiply = n3
End Function
Public Function Divide(ByVal strNumber1 As String, ByVal strNumber2 As String) As String
    res = OneDiv(strNumber2)
    MsgBox strNumber1
    MsgBox res
    res = Multiply(strNumber1, res)
    Divide = res
End Function
Public Function OneDiv(ByVal strNumber As String) As String
    divisor = strNumber
    If divisor = "+1.0" Or divisor = "-1.0" Then
        res = divisor
    Else
        If Mid(divisor, 1, 1) = "-" Then f = 1
        divisor = "+" + Mid(divisor, 2, Len(divisor) - 1)
        For i = 1 To Len(divisor)
            If Mid(divisor, i, 1) = "." Then divisordpp = i
        Next i
        divisoradpc = Len(divisor) - divisordpp
        If Right(divisor, 2) <> ".0" Then
            divisor = Multiply(divisor, "+1" + String(divisoradpc, "0") + ".0")
            g = 1
        End If
        divided = "+1.0"
        For i = 1 To 50
            divided = Multiply(divided, "+10.0")
            If divided = "0.0" Then Exit For
            For j = 1 To 10
                If Compare(Multiply("+" + Trim(Str(Val(j))) + ".0", divisor), divided) = 2 Then
                    k = j - 1
                    Exit For
                End If
            Next j
            res = res + Trim(Str(Val(k)))
            divided = Subtract(divided, Multiply(divisor, "+" + Trim(Str(Val(k))) + ".0"))
        Next i
        res = "+0." + res
        If g = 1 Then res = Multiply(res, "+1" + String(divisoradpc - 0, "0") + ".0")
        If f = 1 Then res = "-" + Mid(divisor, 2, Len(divisor) - 1)
        While Mid(res, 2, 1) = "0" And Mid(res, 3, 1) <> "."
            res = Mid(res, 1, 1) + Mid(res, 3, Len(res) - 2)
        Wend
    End If
    If res = "+0.0" Then res = "0.0"
    OneDiv = res
End Function
Public Function Power(ByVal strNumber1 As String, ByVal strNumber2 As String) As String
    res = "+1.0"
    strI = "+1.0"
    While Compare(strI, strNumber2) < 2
        res = Multiply(res, strNumber1)
        strI = Sum(strI, "+1.0")
    Wend
    ' ...
    Power = res
End Function
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.