将n个数分成偶数和奇数两部分,其中偶数在前奇数在后,要求分别对偶数和奇数进行升序排序。程序运行时,在文本框Text1中输入n,单击处理按钮Command1,分别在Text2、Text3中显示结果。程序运行效果图如图所示。
实现上述功能的VB代码如下,但加框处代码有错,请改正。
Dim a(1 To 100) As Integer
Sub sort(left As Integer,right As Integer)
Dim i As Integer,j As Integer,t As Integer
For i=1 To right-left
For j= '{#blank#}1{#/blank#}
If a(j)>a(j+1) Then
t=a(j):a(j)=a(j+1):a(j+1)=t
End If
Next j
Next i
End Sub
Private Sub Command1_Click()
Dim p As Integer,q As Integer
Dim i As Integer,j As Integer,n As Integer,x As Integer
Randomize
n=Val(Text1.Text):Text2.Text=″ ″:Text3.Text=″ ″
p=0
'{#blank#}2{#/blank#}
For i=1 To n
x=Int(Rnd*100)
Text2.Text=Text2.Text+Str(x)
If x Mod 2=0 Then
p=p+1
a(p)=x
Else
q=q-1
a(q)=x
End If
Next i
Call sort(1,p)
Call sort(p+1,n)
For i=1 To n
Text3.Text=Text3.Text+Str(a(i))
Next i
End Sub