vb 控制台程序
通过输入带符号位的8位二进制数计算 原码 反码 补码
Module Module1 Sub Main() Dim i,j,a,b,c,d,e,f,g,h,k,m As String Dim l_long As String = 1 Console.WriteLine("原码反码补码计算器(8位进制数的 原码反码补码计算)") Console.WriteLine("请输入一个带符号位的8位二进制数(符号位用数字表示0正 1负)") i = Console.ReadLine() '判断符号位是正还是负 j = i.Substring(0,1) If j = 0 Then '整数的原码反码补码相同 Console.WriteLine("源码:{0}反码:{1}补码:{2}",i,i) Else a = i.Substring(0,1) b = i.Substring(1,1) c = i.Substring(2,1) d = i.Substring(3,1) e = i.Substring(4,1) f = i.Substring(5,1) g = i.Substring(6,1) h = i.Substring(7,1) '负数的反码计算 If b = 1 Then b = 0 Else : b = 0 b = 1 End If If c = 1 Then c = 0 Else : c = 0 c = 1 End If If d = 1 Then d = 0 Else : d = 0 d = 1 End If If e = 1 Then e = 0 Else : e = 0 e = 1 End If If f = 1 Then f = 0 Else : f = 0 f = 1 End If If g = 1 Then g = 0 Else : g = 0 g = 1 End If If h = 1 Then h = 0 Else : h = 0 h = 1 End If m = a + b + c + d + e + f + g + h '负数的补码计算 If h = 0 Then h = 1 Else h = 0 If g = 0 Then g = 1 Else g = 0 If f = 0 Then f = 1 Else f = 0 If e = 0 Then e = 1 Else e = 0 If d = 0 Then d = 1 Else d = 0 If c = 0 Then c = 1 Else c = 0 If b = 0 Then b = 1 Else b = 0 If a = 0 Then a = 1 Else a = 0 End If End If End If End If End If End If End If End If '判读 是否会溢出(超出最大数 超出的部分用l_long代替输出) If a = 1 Then k = a + b + c + d + e + f + g + h Else k = l_long + "," + a + b + c + d + e + f + g + h End If Console.WriteLine("源码:{0}反码:{1}补码:{2}",m,k) End If Console.ReadLine() End Sub End Module
运行效果:
原文链接:https://www.f2er.com/vb/259511.html