原始碼:http://down.gogobox.com.tw/t2329175/9r2m2
VB2005的物件陣列真是麻煩!
不過連我的書都沒寫!
不過好險這是常見的問題,
網路是有解答的。
原始碼如下:
'另外VB6的原始碼有做更正
Public Class Form1
    Dim Queue() As Label
    Dim Rear, Front, Ran, Qu(23), i As Integer
    Dim Num As Integer
    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        ReDim Queue(23)
        For i As Integer = 0 To 5
            Queue(i) = New Label
            With Queue(i)
                .Left = 50 + 30 * i
                .Top = 60
                .Width = 30
                .Height = 30
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
                .Visible = True
                Me.Controls.Add(Queue(i))
            End With
        Next
        Num = 5
        Qu(4) = 35
        Qu(5) = 128
        Rear = 5
        Front = 4
        Lr.Top = 92
        Lf.Top = 48
        Lastr.Text = "Now,Queue has 2 Datas."
        ShowNow()
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        End
    End Sub

    Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
        If CanAdd(Qu, Num) = True Then
            If Rear = Front And Qu(Rear) = 0 Then
                Ran = 1 + Int(Rnd() * 999)
                Lax.Text = "Add " & Str(Ran)
                Qu(Rear) = Ran
            Else
                Rear = Rear + 1
                If Rear > Num Then Rear = 0
                Ran = 1 + Int(Rnd() * 999)
                Lax.Text = "Add " & Str(Ran)
                Qu(Rear) = Ran
            End If
        Else
            If Num = 23 Then
                Lax.Text = "Queue is full."
            Else
                For i = Num + 1 To Num + 6
                    Queue(i) = New Label
                    With Queue(i)
                        .Left = 50 + 30 * i
                        .Top = 60
                        .Width = 30
                        .Height = 30
                        .BackColor = Color.White
                        .BorderStyle = BorderStyle.FixedSingle
                        .Visible = True
                        Me.Controls.Add(Queue(i))
                    End With
                Next
                Num = Num + 6
                If Front <> 0 Then
                    If Rear > Front And Rear < Num - 6 Then
                        For i = Num - 6 To Rear Step -1
                            Qu(i + 6) = Qu(i)
                            Qu(i) = 0
                        Next
                        Rear = Rear + 6
                    ElseIf Rear < Front Then
                        For i = Num - 6 To Front Step -1
                            Qu(i + 6) = Qu(i)
                            Qu(i) = 0
                        Next
                        Front = Front + 6
                    End If
                End If
                Rear = Rear + 1
                If Rear > Num Then Rear = 0
                Ran = 1 + Int(Rnd() * 999)
                Lax.Text = "Add " & Str(Ran)
                Qu(Rear) = Ran
                End If
        End If
        ShowNow()
    End Sub

    Private Sub Romove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Romove.Click
        If CanRomove(Qu, Num) = True Then
            Lax.Text = "Romove " & Str(Qu(Front))
            Qu(Front) = 0
            If Front <> Rear Then
                Front = Front + 1
                If Front > Num Then Front = 0
            End If
        Else
            Lax.Text = "Queue is empty."
        End If
        ShowNow()
    End Sub

    Sub ShowNow()
        Dim x As Integer
        For i As Int16 = 0 To Num
            Queue(i).TextAlign = ContentAlignment.MiddleCenter
            If Qu(i) <> 0 Then
                Queue(i).Text = Qu(i)
                x = x + 1
            Else
                Queue(i).Text = ""
            End If
        Next
        Lr.Left = 54 + 30 * Rear
        Lf.Left = 50 + 30 * Front
        If x > 1 Then
            Lastr.Text = "Now,Queue has " & Str(x) & " Datas."
        ElseIf x = 1 Then
            Lastr.Text = "Now,Queue has 1 Data."
        ElseIf x = 0 Then
            Lastr.Text = "Now,Queue has none."
        End If
    End Sub
    Function CanAdd(ByVal Queue() As Integer, ByVal big As Integer) As Boolean
        Dim x As Integer
        CanAdd = True
        For i = 0 To big
            If Queue(i) = 0 Then x = x + 1
        Next
        If x < 2 Then CanAdd = False
    End Function
    Function CanRomove(ByVal Queue() As Integer, ByVal big As Integer) As Boolean
        CanRomove = False
        For i = 0 To big
            If Queue(i) <> 0 Then CanRomove = True
        Next
    End Function

End Class


arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()