vb 十进制转换成 八进制 二进制原码 二进制补码 二进制反码 程序

vb 十进制转换成 八进制 二进制原码 二进制补码 二进制反码 程序,第1张

VB.NET控制台程序:

Module module3

Sub Main()

Dim i As Integer

Dim num As Integer = -35

Dim R As Integer = 8

Dim arr(33) As Integer

Dim len As Integer

arr = TransToR(num, R)

len = arr(0)

Console.Write(num &" 转换成 " &R &" 进制后:" &vbTab)

If (arr(1) = 1) Then

Console.Write("-")

End If

For i = len + 1 To 2 Step -1

Console.Write(arr(i))

Next

Console.WriteLine()

arr = TransToSourceCode(num)

Console.Write(num &" 转换成32位二进制源码后:" &vbTab)

Console.Write(arr(1))

For i = 32 To 2 Step -1

Console.Write(arr(i))

Next

Console.WriteLine()

arr = TransToReverseCode(num)

Console.Write(num &" 转换成32位磨洞二进制反码后:" &vbTab)

Console.Write(arr(1))

For i = 32 To 2 Step -1

Console.Write(arr(i))

Next

Console.WriteLine()

arr = TransToCompensatoryCode(num)

Console.Write(num &" 转换成32位二进制补码后:" &vbTab)

Console.Write(arr(1))

For i = 32 To 2 Step -1

Console.Write(arr(i))

Next

Console.WriteLine()

End Sub

'将十进制数num转换为R(2<=R<=9)进制数,结果存在arr数组中

'arr(0)存储长度, arr(1)存储符号位:1为负,0为正

Function TransToR(ByVal num As Integer, ByVal R As Integer) As Integer()

Dim arr(33) As Integer

arr(0) = 0 '初始长度

'孙好设置符号位

If (num <0) Then

arr(1) = 1

Else

arr(1) = 0

End If

num = Math.Abs(num)

While num >0

arr(0) = arr(0) + 1

arr(arr(0) + 1) = num Mod R

num \= R

End While

Return arr

End Function

'瞎凯枯将十进制数num转换为二进制原码

Function TransToSourceCode(ByVal num As Integer) As Integer()

Dim arr(33) As Integer

Dim i As Integer

arr = TransToR(num, 2)

'高位补零

For i = arr(0) + 2 To 32

arr(i) = 0

Next

arr(0) = 32

Return arr

End Function

'将十进制数num转换为二进制反码

Function TransToReverseCode(ByVal num As Integer) As Integer()

Dim arr(33) As Integer

Dim i As Integer

arr = TransToSourceCode(num)

If num >= 0 Then

Return arr

End If

'除符号位外各位取反

For i = 2 To 33

arr(i) = 1 - arr(i)

Next

Return arr

End Function

'将十进制数num转换为二进制补码

Function TransToCompensatoryCode(ByVal num As Integer) As Integer()

Dim arr(33) As Integer

Dim i As Integer

Dim c As Integer = 0'进位

arr = TransToReverseCode(num)

If num >= 0 Then

Return arr

End If

'末尾加1

i = 2

arr(i) += 1

Do While True

arr(i) = c + arr(i)

c = arr(i) \ 2

arr(i) = arr(i) Mod 2

i += 1

If c <1 Then

Exit Do

End If

Loop

Return arr

End Function

End Module

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8208703.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存