Visual Basic - 请查看我的代码?

时间:2014-04-29 21:36:30

标签: vb.net

分配是对大数字进行加,减,乘和除。

第一个任务是弄清楚加法和减法,因为老师然后希望我们调用加法和除法的加法和减法程序。

我搞砸了,但是我们在阵列演讲中动作太快了,这对我来说没有意义。我希望有人可以查看我的添加代码并给我一些建议。

输入的数字长度为31个字符。

这是我的代码:

 Const Max As Integer = 30 
 Const Zero As String = "0000000000000000000000000000000" 
 Const One As String = "0000000000000000000000000000001" 
 Private Big1(Max), Big2(Max), Result(Max) 


 Private Sub BigAdd(ByVal Big1Str As String, ByVal Big2Str As String, ByRef SumStr As String) 

  Dim x, y, z As Integer 
  Dim One, Two, Sum As Integer 

  Big1Str.Substring(0, Max) 
  Big2Str.Substring(0, Max) 

  For x = Max To 0 Step -1 
    Integer.TryParse(Big1Str.Substring(x, 1), One) 
  Next 

  For y = Max To 0 Step -1 
    Integer.TryParse(Big2Str.Substring(y, 1), Two) 
  Next 

  For z = Max To 1 Step -1 
    SumStr = One + Two 
    If (Sum > 9) Then 
      Sum = Sum - 10 
      ' How to add 1 to the next place??? 
    End If 
  Next 

  ResultText.Text = SumStr 

 End Sub 

我认为我做错了:(希望有人可以帮我修复它!  提前谢谢!!!

3 个答案:

答案 0 :(得分:0)

他......可能不被允许......但是为什么要重新发明轮子你可能会犯一堆错误呢?

http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.add(v=vs.110).aspx

可能必须添加对System.Numerics程序集的引用

Imports System.Numerics

Dim x as BigInteger = BigInteger.Parse(Big1Str )
Dim y as BigInteger = BigInteger.Parse(Big2Str )
Dim result as BigInteger = BigInteger.Add( x, y )
SumStr = result.ToString()

答案 1 :(得分:0)

嗯,因为没有其他人提供,这里有一个选择,这是一个艰难的方式。请注意,此代码并未假定输入的最大长度,因此可以随意输入一千位数字。它不支持负数,也不检查非数字输入,前导或尾随空格,或任何可能在字符串中的其他crud。

Module NumericStrings

    Private Function BigAdd(ByVal big1Str As String, ByVal big2Str As String) As String

        'The result can be as large as the larger input, plus one digit if it carries.
        Dim maxSize = Math.Max(big1Str.Length, big2Str.Length) + 1
        Dim result(0 To maxSize - 1) As Char

        Dim place As Integer
        Dim placesOccupied As Integer = 1

        Dim digitCarry As Integer = 0

        For place = 1 To maxSize
            'Extract the digits at the specified place, treating nonexistent as zero.
            Dim digit1 As Integer = 0
            If place <= big1Str.Length Then digit1 = Asc(big1Str.Chars(big1Str.Length - place)) - Asc("0")
            Dim digit2 As Integer = 0
            If place <= big2Str.Length Then digit2 = Asc(big2Str.Chars(big2Str.Length - place)) - Asc("0")

            'Add, including the carry from the previous place.
            Dim digitSum As Integer
            digitSum = digit1 + digit2 + digitCarry

            'Take note of places occupied, so leading zeros can be excluded from results.
            If digitSum > 0 Then placesOccupied = place

            'Handle the carry out to the next place.
            If digitSum > 9 Then
                digitSum = digitSum - 10
                digitCarry = 1
            Else
                digitCarry = 0
            End If

            'Turn the resulting sum digit into a character and place it into the array.
            result(maxSize - place) = Chr(digitSum + Asc("0"))
        Next

        'Create a string from the array, excluding leading zeros.
        Return New String(result, maxSize - placesOccupied, placesOccupied)
    End Function

    'A quick demonstration.
    Sub Main()
        Console.WriteLine(BigAdd("1", "1")) 'prints "2"
        Console.WriteLine(BigAdd("8", "4")) 'prints "12"
        Console.WriteLine(BigAdd("007", "9")) 'prints "16"
        Console.WriteLine(BigAdd("99999999999999999999999999999999999999999999999999", "1")) 'guess :)
    End Sub
End Module

答案 2 :(得分:0)

有几点。使用查找表消除了大量重复解析为整数。您的代码也应具有验证功能。还应该解决不同长度的字符串:

Const digits As String = "0123456789"
Private Function BigAdd(ByVal Big1Str As String, ByVal Big2Str As String) As String
    Dim temp As String = ""
    If Not IsNumeric(Big1Str) OrElse Not IsNumeric(Big2Str) Then
        Return "Invalid string"
    End If
    Dim max, min As String
    If Big1Str.Length < Big2Str.Length Then
        max = Big2Str
        min = Big1Str
    Else
        max = Big1Str
        min = Big2Str
    End If
    Dim lendiff As Integer = max.Length - min.Length
    Dim carryover As Integer = 0
    Dim temp1 As Integer = 0
    For I = min.Length - 1 To 0 Step -1
        temp1 = digits.IndexOf(min(I)) + digits.IndexOf(max((lendiff) + I)) + carryover
        temp = temp.Insert(0, (temp1 Mod 10).ToString)
        carryover = temp1 \ 10
    Next
    Dim tempI = lendiff
    While carryover = 1
        If tempI = 0 Then
            temp = temp.Insert(0, "1")
            carryover = 0
        Else
            temp1 = ((digits.IndexOf(max(tempI - 1)) + carryover))
            temp = temp.Insert(0, (temp1 Mod 10).ToString)
            carryover = temp1 \ 10
            tempI -= 1
        End If

    End While
    temp = temp.Insert(0, max.Substring(0, tempI))
    Return temp.Trim("0"c)
End Function

通过使用Dictionary(Of Char,Integer)作为查找表而不是字符串,您可以获得几个效率订单,使用stringbuilder构建最终输出比重建字符串要快得多。