永发信息网

vb作业:自动生成一组数据,从小到大排序,并用二分法查找

答案:1  悬赏:40  手机版
解决时间 2021-04-14 06:09
  • 提问者网友:锁深秋
  • 2021-04-13 16:27
vb作业:自动生成一组数据,从小到大排序,并用二分法查找
最佳答案
  • 五星知识达人网友:执傲
  • 2021-04-13 17:20
我以前做的有一个;
Dim a(9) As Integer
Dim CountC As Integer
Dim FindRes As Boolean
Dim CC As Integer
Dim Po As Integer
Private Sub Command1_Click()
    Load Shape1(10)
    Shape1(10).Width = 300
    Shape1(10).Left = 1000
    Shape1(10).Height = Val(Text1.Text)
    Shape1(10).Top = 7000 - Shape1(10).Height
    Shape1(10).FillStyle = 0
    Shape1(10).FillColor = RGB(0, 255, 0)
    Shape1(10).Visible = True
    
    Load Labl(10)
    Labl(10).Left = 1000
    Labl(10).Width = 615
    Labl(10).Height = 255
    Labl(10).Top = 7000
    Labl(10).Caption = Shape1(10).Height
    Labl(10).Visible = True
    
    Load LaI(10)
    LaI(10).Left = 1080
    LaI(10).Width = 615
    LaI(10).Height = 255
    LaI(10).Top = 7000 - Shape1(10).Height - 255
    LaI(10).Caption = "?"
    LaI(10).Visible = True
    
    mx = 0
    my = 0
    
    FindRes = False
    
    Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
    Randomize
    For i = 0 To 9
        a(i) = Int(Rnd * 2000 + 1000)
    Next i
    For i = 0 To 9
        For j = 0 To 8 - i
            If a(j) < a(j + 1) Then
                temp = a(j + 1)
                a(j + 1) = a(j)
                a(j) = temp
            End If
        Next j
    Next i
    Shape1(0).Width = 300
    Shape1(0).Left = 3000
    Shape1(0).Height = a(0)
    Shape1(0).Top = 7000 - a(0)
    Shape1(0).FillStyle = 0
    Shape1(0).FillColor = RGB(96, 96, 96)
    Shape1(0).Visible = True
    
    For i = 1 To 9
        Shape1(i).Width = 300
        Shape1(i).Left = 3000 + 750 * i
        Shape1(i).Height = a(i)
        Shape1(i).Top = 7000 - a(i)
        Shape1(i).FillStyle = 0
        Shape1(i).FillColor = RGB(96, 96, 96)
        Shape1(i).Visible = True
    Next i
    
    Labl(0).Left = 3000
    Labl(0).Width = 615
    Labl(0).Height = 255
    Labl(0).Top = 7000
    Labl(0).Caption = a(0)
    Labl(0).Visible = True
    
    For i = 1 To 9
        Labl(i).Left = 3000 + 750 * i
        Labl(i).Width = 615
        Labl(i).Height = 255
        Labl(i).Top = 7000
        Labl(i).Caption = a(i)
        Labl(i).Visible = True
    Next i
    LaI(0).Left = 3080
    LaI(0).Width = 615
    LaI(0).Height = 255
    LaI(0).Top = 7000 - a(0) - 255
    LaI(0).Caption = 0
    LaI(0).Visible = True
    
    For i = 1 To 9
        LaI(i).Left = 3080 + 750 * i
        LaI(i).Width = 615
        LaI(i).Height = 255
        LaI(i).Top = 7000 - a(i) - 255
        LaI(i).Caption = i
        LaI(i).Visible = True
    Next i
End Sub

Private Sub Form_Load()
    Randomize
    For i = 0 To 9
        a(i) = Int(Rnd * 2000 + 1000)
    Next i
    For i = 0 To 9
        For j = 0 To 8 - i
            If a(j) < a(j + 1) Then
                temp = a(j + 1)
                a(j + 1) = a(j)
                a(j) = temp
            End If
        Next j
    Next i
    Shape1(0).Width = 300
    Shape1(0).Left = 3000
    Shape1(0).Height = a(0)
    Shape1(0).Top = 7000 - a(0)
    Shape1(0).FillStyle = 0
    Shape1(0).FillColor = RGB(96, 96, 96)
    Shape1(0).Visible = True
    
    For i = 1 To 9
        Load Shape1(i)
        Shape1(i).Width = 300
        Shape1(i).Left = 3000 + 750 * i
        Shape1(i).Height = a(i)
        Shape1(i).Top = 7000 - a(i)
        Shape1(i).FillStyle = 0
        Shape1(i).FillColor = RGB(96, 96, 96)
        Shape1(i).Visible = True
    Next i
    
    Labl(0).Left = 3000
    Labl(0).Width = 615
    Labl(0).Height = 255
    Labl(0).Top = 7000
    Labl(0).Caption = a(0)
    Labl(0).Visible = True
    
    For i = 1 To 9
        Load Labl(i)
        Labl(i).Left = 3000 + 750 * i
        Labl(i).Width = 615
        Labl(i).Height = 255
        Labl(i).Top = 7000
        Labl(i).Caption = a(i)
        Labl(i).Visible = True
    Next i
    
    LaI(0).Left = 3080
    LaI(0).Width = 615
    LaI(0).Height = 255
    LaI(0).Top = 7000 - a(0) - 255
    LaI(0).Caption = 0
    LaI(0).Visible = True
    
    For i = 1 To 9
        Load LaI(i)
        LaI(i).Left = 3080 + 750 * i
        LaI(i).Width = 615
        LaI(i).Height = 255
        LaI(i).Top = 7000 - a(i) - 255
        LaI(i).Caption = i
        LaI(i).Visible = True
    Next i
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Form3.Show
End Sub

Private Sub Timer1_Timer()
    If Shape1(10).Top > Shape1(0).Top - Shape1(10).Height - 600 Then
        Labl(10).Top = Labl(10).Top - 50
        LaI(10).Top = LaI(10).Top - 50
        Shape1(10).Top = Shape1(10).Top - 50
    Else
        If Shape1(10).Left < Shape1(0).Left + 375 Then
            mx = mx + 50
            Labl(10).Left = Labl(10).Left + 50
            LaI(10).Left = LaI(10).Left + 50
            Shape1(10).Left = Shape1(10).Left + 50
        End If
    End If
    If Shape1(10).Top <= Shape1(0).Top - Shape1(10).Height - 200 And Shape1(10).Left >= Shape1(0).Left + 375 Then
        Timer1.Enabled = False
        Timer2.Enabled = True
        CountC = 1
        'MsgBox "第一步完成"
    End If
End Sub

Private Sub Timer2_Timer()
    If Shape1(10).Height >= Shape1(0).Height Then
        Shape1(10).Width = 300
        Shape1(10).Left = 2250
        Shape1(10).Height = Val(Text1.Text)
        Shape1(10).Top = 7000 - Shape1(10).Height
        Shape1(10).FillStyle = 0
        Shape1(10).FillColor = RGB(0, 255, 0)
        Shape1(10).Visible = True
        
        Labl(10).Left = 2250
        Labl(10).Width = 615
        Labl(10).Height = 255
        Labl(10).Top = 7000
        Labl(10).Caption = Shape1(10).Height
        Labl(10).Visible = True
        
        LaI(10).Left = 2330
        LaI(10).Width = 615
        LaI(10).Height = 255
        LaI(10).Top = 7000 - Shape1(10).Height - 255
        LaI(10).Caption = "?"
        LaI(10).Visible = True
        
        Timer2.Enabled = False
        MsgBox "我是最大的"
    ElseIf Shape1(10).Height <= Shape1(9).Height Then
        Shape1(10).Width = 300
        Shape1(10).Left = 3000 + 10 * 750
        Shape1(10).Height = Val(Text1.Text)
        Shape1(10).Top = 7000 - Shape1(10).Height
        Shape1(10).FillStyle = 0
        Shape1(10).FillColor = RGB(0, 255, 0)
        Shape1(10).Visible = True
        
        Labl(10).Left = 3000 + 10 * 750
        Labl(10).Width = 615
        Labl(10).Height = 255
        Labl(10).Top = 7000
        Labl(10).Caption = Shape1(10).Height
        Labl(10).Visible = True
        
        LaI(10).Left = 3000 + 10 * 750 + 80
        LaI(10).Width = 615
        LaI(10).Height = 255
        LaI(10).Top = 7000 - Shape1(10).Height - 255
        LaI(10).Caption = "?"
        LaI(10).Visible = True
        
        Timer2.Enabled = False
        MsgBox "我是最小的"
    Else
        If Shape1(10).Height <= Shape1(CountC - 1).Height And Shape1(10).Height > Shape1(CountC).Height Then
            Label2.Caption = "?:我比" & CountC - 1 & "小,同时也比" & CountC & "大,我的插入位置在这儿"
            Label2.Left = Shape1(10).Left + 320
            Label2.Top = Shape1(10).Top + Shape1(10).Height / 2
            FindRes = True
        Else
            Label2.Caption = "?:我比" & CountC - 1 & "小,同时比" & CountC & "小,我的插入位置不在这儿"
            Label2.Left = Shape1(10).Left + 320
            Label2.Top = Shape1(10).Top + Shape1(10).Height / 2
        End If
        If FindRes = False Then
            CountC = CountC + 1
            Timer2.Enabled = False
            Timer3.Enabled = True
        End If
        If FindRes = True Then
            CC = CountC
            Timer2.Enabled = False
            Timer4.Enabled = True
        End If

    End If
    
End Sub

Private Sub Timer3_Timer()
    If Shape1(10).Left < Shape1(CountC - 1).Left + 375 Then
            Labl(10).Left = Labl(10).Left + 50
            LaI(10).Left = LaI(10).Left + 50
            Shape1(10).Left = Shape1(10).Left + 50
            Label2.Left = Label2.Left + 50
    ElseIf Shape1(10).Top <= Shape1(CountC - 1).Top - Shape1(10).Height - 600 Then
        Labl(10).Top = Labl(10).Top + 50
        LaI(10).Top = LaI(10).Top + 50
        Shape1(10).Top = Shape1(10).Top + 50
        Label2.Top = Label2.Top + 50
    Else
    
        Timer3.Enabled = False
        Timer2.Enabled = True
    End If
End Sub

Private Sub Timer4_Timer()
    Debug.Print "mmmm " & CC
    If CC = CountC Then
        Label2.Caption = "?:我需要将位置插入在这儿,请后面的往后挪一位,谢谢!"
    Else
        Label2.Caption = CC & ":有新人加入,请" & CC + 1 & "往后挪一位"
        Label2.Top = Shape1(CC).Top - 600
        Label2.Left = Label2.Left + 750
    End If
    CC = CC + 1
    
    If CC >= 9 Then
        CC = CountC
        Po = 0
        Timer4.Enabled = False
        Timer5.Enabled = True
    End If
End Sub

Private Sub Timer5_Timer()
    If Po < 10 - CountC Then
        Shape1(9 - Po).Left = Shape1(9 - Po).Left + 750
        Labl(9 - Po).Left = Labl(9 - Po).Left + 750
        LaI(9 - Po).Left = LaI(9 - Po).Left + 750
    Else
        Shape1(10).Top = 7000 - Shape1(10).Height
        Shape1(10).Left = Shape1(CountC - 1).Left + 750
        Labl(10).Top = 7000
        Labl(10).Left = Shape1(CountC - 1).Left + 750
        LaI(10).Top = 7000 - Shape1(10).Height - 255
        LaI(10).Left = Shape1(CountC - 1).Left + 750
        MsgBox "插入完毕!"
        Timer5.Enabled = False
    End If
    Po = Po + 1
End Sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯