0
Dim n, front, rear As Integer
Dim x As Integer
Dim arr() As Integer

Public Function init()
  n = InputBox("Enter size :")
  ReDim arr(n) As Integer
  front = 0
  rear = -1
End Function

Public Function insert(x As Integer)
  If rear = n-1 Then
    MsgBox "queue FULL !!!", vbOKOnly, "QUEUE"
  Else
    rear = rear + 1
    arr(rear) = x
    MsgBox x, vbOKOnly, "INSERTED"
  End If
End Function

Public Function delete() As Integer
  If rear + 1 = front Then
    MsgBox "queue Empty !!!", vbOKOnly, "QUEUE"
  Else
    x = arr(front)
    front = front + 1
    Return x
  End If
End Function

Private Sub inser_Click()
  If rear < n Then
    x = InputBox("Enter element :")
    Call insert(x)
  Else
    MsgBox "queue FULL !!!", vbOKOnly, "QUEUE"
  End If
End Sub

Private Sub del_Click()
  x = delete()
  MsgBox x, vbOKOnly, "DELETED"
End Sub

Private Sub Exit_Click()
  End
End Sub

Private Sub Form_Load()
  Call init
End Sub

これはVB6の私のコードです。「compliererrorExpected:Endofstatement」という行のinsert関数でエラーが発生しますReturn x

もう1つのエラーは、キューの要素を削除しようとすると、「0DELETED」と表示されることです。

4

1 に答える 1

6

Return ステートメントを使用して関数から値を返そうとしていますが、これは VB6 では有効ではありません。VB6 では、戻り値を関数の名前に割り当てることによって、関数の値を返します。

したがって、delete関数については、次のように記述します。

Public Function delete() As Integer
    If rear + 1 = front Then
        MsgBox "queue Empty !!!", vbOKOnly, "QUEUE"
    Else
        x = arr(front)
        front = front + 1
        delete = x    ' <-- returning x here.
    End If
End Function

他の関数を見てください。明示的に値をまったく返していません。

VB6 で Subs と Functions がどのように機能するかの概要を説明しているthisを参照すると役立つ場合があります。

于 2012-05-10T17:18:33.447 に答える