给定一个整数n,求该整数的原码,反码和补码,用vb语言

2025-02-24 01:59:24
推荐回答(1个)
回答1:

Private Sub Command1_Click()

  Dim a(16) As Integer, x As Integer

  x = CInt(Text1.Text)

  If x >= 0 Then a(16) = 0 Else a(16) = 1

  x = Abs(x)

  For i = 1 To 15

    a(i) = x Mod 2

    x = x \ 2

  Next i

  Text2.Text = ""

  For i = 16 To 1 Step -1

    Text2.Text = Text2.Text & a(i)

  Next i

  

  If a(16) = 0 Then

    Text3.Text = Text2.Text

    Text4.Text = Text2.Text

    Exit Sub

  End If

  

  For i = 1 To 15

    a(i) = 1 - a(i)

  Next i

  Text3.Text = a(16)

  For i = 15 To 1 Step -1

    Text3.Text = Text3.Text & a(i)

  Next i

  

  Text4.Text = a(16)

  a(1) = a(1) + 1

  i = 1

  While a(i) = 2 And i < 15

    a(i) = 0

    a(i + 1) = a(i + 1) + 1

    i = i + 1

  Wend

  For i = 15 To 1 Step -1

    Text4.Text = Text4.Text & a(i)

  Next i

End Sub