题型:综合题 题类:模拟题 难易度:困难
浙江省普通高校招生信息技术选考模拟卷(二)
⑴把格雷码第1位G1作为解码后二进制码的第1位B1,即:B1= G1;
⑵从第2位起,将格雷码每一位与左边相邻一位的解码值作异或运算,结果作为该位格 雷码的解码值,即Bi=Gi⊕Bi-1。例如,某格雷码1101110,可解码成普通二进制码 1001011, 如图所示。
异或运算(⊕)规则:如果 a、b 两个值不相同,则 a⊕b 结果为 1;如果 a、b 两个值 相同,则 a⊕b 结果为 0,即:0 ⊕ 0 = 0,1 ⊕ 0 = 1,0 ⊕ 1 = 1,1 ⊕ 1 = 0。
小明编写了一个格雷码解码的VB程序,功能如下:在文本框Text1中输入格雷码,单击 “解码”按钮 Command1,程序对输入的格雷码进行转换处理,转换后的二进制码用十六进 制表示并显示在文本框 Text2 中。程序运行界面如图所示。
Const max = 16
Function btoh(s As String) As String
Dim i As Integer, value As Integer, ss As String
Dim c1 As Integer, c2 As Integer, c3 As Integer, c4 As Integer ss = ""
If Len(s) Mod 4 <> 0 Then
s = Mid("0000", 1, 4 - n) + s '四位一组分组,最后一组不足 4 位补 0
For i = 1 To Len(s) Step 4
c1 = Val(Mid(s, i, 1))
c2 = Val(Mid(s, i + 1, 1))
c3 = Val(Mid(s, i + 2, 1))
c4 = Val(Mid(s, i + 3, 1))
value = c1 * 2 ^ 3 + c2 * 2 ^ 2 + c3 * 2 + c4
If value <= 9 Then
ss = ss + Chr(Asc("0") + value)
Else
End If
btoh =ss
Next i
End Function
Private Sub Command1_Click() Dim i As Integer
Dim s As String, b As String Dim g(1 To max) As String
s = Text1.Text: length = Len(s)
For i = 1 To length
g(i) = Mid(s, i, 1)
Next i
b = g(1)
For i = 2 To length
If g(i) =Then b = b + "0" Else b = b + "1"
Next i
Text2.Text =
End Sub
试题篮