试题

试题 试卷

logo

题型:填空题 题类:常考题 难易度:困难

算法与程序设计

删除重复字符的VB程序,程序功能如下:在文本框Text1中输入字符串,单击“删除重复字符”按钮,在文本框Text2中输出处理后的结果。程序运行效果如图所示,实现上述功能的VB代码如下,但加框处代码有错,请改正。

Private Sub Command1_Click()

x = Text1.Text

n = Len(x)

s=s+Mid(x,1,1)

For i = 2 To n

flag =0

For j = 1 To Len(s)

If  Then '

flag = 1: Exit For

End If

Next j

If  Then '

s = s + Mid(x, i, 1)

End If

Next i

Text2.Text = s

End Sub

举一反三
【加试题】数据的有损压缩:对多媒体数据进行有损压缩时,通常是以少量数据丢失作为代价的,比如对某8 bit量化的数据压缩为6bit量化,其算法可简单表示为:逐一删除各个字节的后2 bit,将删除除后的6 bit数据首尾相连,然后重新按照8bit一组构建字节,以十六进制显示。

例如,压缩前的原始数据为:

1111 1100 0101 1010 1011 0000  可表示为十六进制:FC 5A B0

  删除每字节的后2 bit:

1111 1100 0101 1010 1011 0000

  首尾相连构建新的数据:

11 1111 0101 1010 1100 表示为十六进制:... F5 AC

 自此,原数据进行了有损压缩。程序运行界面如下所示:

实现上述算法的VB程序如下,但程序中加框处有误,请改正。

Dim a(1 To 100) As Integer

Const code = "0123456789ABCDEF"

Const n = 100

Private Sub FormLoad()‘ 以随机数模拟读取原始数据

  s = ""

  Randomize

  For i = 1 To n

  a(i) = Int(Rnd * 16)

    Text1.Text = Text1.Text +  ①

  Next i

End Sub

Private Sub Command1Click()

For i = 1 To n

   t = t + HtoB(a(i))

Next i

For j = 1 To n * 4 Step 8'每8位一组,取前6个字符

   s = s + Mid(t, j, 6)

Next j

For i = 1 To 4 * (n - 1) Step 4'每4位一组转为十六进制显示

    Text2.Text = Text2.Text + BtoH(Mid(s, i, 4))

Next i

End Sub

Function HtoB(x As Integer) As String'十六进制转为二进制函数

 t = ""

 m = 0

Do While   ②

   r = x Mod 2

   m = m + 1

   t = Trim(r) + t'trim()用于删除前导空格

   x = x \ 2

 Loop

 HtoB = Mid("0000", 1, 4 - m) + t

End Function

Function BtoH(y As String) As String'二进制转为十六进制函数

 t1 = Val(Mid(y, 4, 1))

 t2 = Val(Mid(y, 3, 1))

 t3 = Val(Mid(y, 2, 1))

 t4 = Val(Mid(y, 1, 1))

 s = t1 + t2 * 2 + t3 * 4 + t4 * 8

 BtoH = Mid(code, s + 1, 1)

End Function
①  {#blank#}1{#/blank#}② {#blank#}2{#/blank#}

【加试题】小王设计了一个算法试图求出平面坐标系中最左点出发,从左往右途经所有点到达最右点的路径值和,编写了一个VB程序功能如下:在文本框Text1中输入需随机生成的坐标点数量,单击“生成”按钮Command1后,在列表框List1中生成相应数量的坐标点(坐标值为大于等于1小于10的实数 ,保留一位小数),在文本框Text2中输出路径值。程序界面如图所示。

实现上述功能的VB程序如下,但划线部分代码有错,请改正。

Private Sub Command1_Click()

  Dim x(1 To 100) As Single, y(1 To 100) As Single

  Dim tmp As Single, length As Single, sum As Single, x1 As Single, y1 As Single

  Dim i As Integer, j As Integer, k As Integer

Randomize 

List1.Clear

  k = Val(Text1.Text)

  For i = 1 To k

      x(i) = Int(Rnd * 90) / 10 + 1

      y(i) = Int(Rnd * 90) / 10 + 1

      List1.AddItem "X:" + Str(x(i)) + "   " + "Y:" + Str(y(i))

  Next i

  For i = 1 To k - 1

      For j = 1 To k - i

          If x(j) > x(j + 1) Then

              x(i)=x(i) + x(j)

              x(j) = x(i) - x(j)

              x(j) = x(i) + x(j)   ‘①

              tmp = y(j)

              y(j) = y(j + 1)

              y(j + 1) = tmp

          End If

      Next j

  Next i

  sum = 0: length = 0

  For i = 1 To k - 1

      x1 = (x(i) - x(1)) ^ 2     ‘②

 '变量y1计算方法同x1,代码略

      length = Sqr(x1 + y1)

      sum = sum + length

  Next i

  Text2.Text = Str(Int(sum * 10) / 10)

End Sub

①{#blank#}1{#/blank#} ②{#blank#}2{#/blank#} 

返回首页

试题篮