【问题标题】:Visual Basic - Adding two binary numbersVisual Basic - 添加两个二进制数
【发布时间】:2014-04-04 13:49:55
【问题描述】:

您好,我是这里的新手,一般来说是编码方面的。我一直在研究将两个二进制数相加的代码,这些部分单独工作,但组合起来不工作。如果有人可以帮助我解决这个问题,我将不胜感激。另外,如果可以缩短它也会有所帮助,谢谢。

Class Form1

    Dim intNum1 As Integer
    Dim intNum2 As Integer
    Dim intNum3 As Integer

    Public Function BinaryToDecimalA(ByRef Binary As String) As Integer
        Dim BinaryNumA As Integer
        Dim BitCountA As Short

        For BitCountA = 1 To Len(Binary)
            BinaryNumA = BinaryNumA + (CDbl(Mid(Binary, Len(Binary) - BitCountA + 1, 1)) * (2 ^ (BitCountA - 1)))
        Next BitCountA
        BinaryToDecimalA = BinaryNumA

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        intNum1 = (BinaryToDecimal((TextBox1.Text)))

    End Sub
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If

        TextBox1.MaxLength = 3
    End Sub
    Public Function BinaryToDecimal(ByRef Binary As String) As Integer
        Dim BinaryNum As Integer
        Dim BitCount As Short

        For BitCount = 1 To Len(Binary)
            BinaryNum = BinaryNum + (CDbl(Mid(Binary, Len(Binary) - BitCount + 1, 1)) * (2 ^ (BitCount - 1)))
        Next BitCount
        BinaryToDecimal = BinaryNum

    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        intNum2 = (BinaryToDecimal((TextBox2.Text)))

    End Sub
    Private Sub TextBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If

        TextBox2.MaxLength = 3
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        intNum1 = Integer.Parse(TextBox1.Text)

        intNum2 = Integer.Parse(TextBox2.Text)

        intNum3 = intNum1 + intNum2

    End Sub

    Private Sub intNum3_TextChanged(sender As Object, e As EventArgs) Handles TextBoxAns.TextChanged

        Dim i As Long, x As Long, bin As String
        Const maxpower = 7
        TextBoxAns.Enabled = False
        bin = ""
        x = Val(intNum3)

        If x > 2 ^ maxpower Then
            MsgBox("Number must be no longer than " & Str$(2 ^ maxpower))
            TextBoxAns.Text = ""
        End If

        If x < 0 Then bin = bin + "1" Else bin = bin + "0"

        For i = maxpower To 0 Step -1
            If x And (2 ^ i) Then
                bin = bin + "1"
            Else
                bin = bin + "0"
            End If
        Next
        TextBoxAns.Text = bin
    End Sub
End Class

【问题讨论】:

  • dim a As Integer = Convert.ToInt32("101010", 2) + Convert.ToInt32("1000101", 2)

标签: vb.net binary addition


【解决方案1】:

这应该有效 - 不是我的代码(参考http://www.bigresource.com/VB-Converting-a-number-to-its-binary-number-uDbSei3kPM.html),但它有效!

公共函数 BinaryAddition(ByVal A As String, ByVal B As String) As String

将 curA 设为货币

将 curB 设为货币

将 curResult 变暗为货币

curA = BinToDec(A)

curB = BinToDec(B)

curResult = (curA + curB) ' Mod (2 ^ 32)

BinaryAddition = DecToBin(curResult)

结束函数

Private Function DecToBin(curDec As Currency) As String

将 strTemp 调暗为字符串

将 i 调暗为整数

i = 31

Do While i >= 0
    If curDec >= (2 ^ i) Then
        strTemp = strTemp & "1"
        curDec = curDec - (2 ^ i)
    Else
        strTemp = strTemp & "0"
    End If

    i = i - 1
Loop

DecToBin = strTemp

结束函数

【讨论】:

  • 感谢您的代码,但我真的需要它基于我上面创建的代码。
【解决方案2】:
Module Module1
    Sub Main()
        Console.WriteLine("Enter the first binary number, then enter the second binary number")
        Dim num As Integer = Convert.ToInt32(Console.ReadLine, 2)
        Console.WriteLine(Convert.ToString(num + Convert.ToInt32(Console.ReadLine, 2), 2))
        Console.ReadLine()
    End Sub
End Module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2018-06-29
    相关资源
    最近更新 更多