VB.Net Tutorial/Generics/Generic Stack — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:56, 26 мая 2010
Generic Stack
Module StackTest
Dim doubleElements() As Double = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6}
Dim integerElements() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Dim doubleStack As Stack(Of Double)
Dim integerStack As Stack(Of Integer)
Sub Main()
doubleStack = New Stack(Of Double)(5)
integerStack = New Stack(Of Integer)(10)
TestPushDouble()
TestPopDouble()
TestPushInteger()
TestPopInteger()
End Sub
Sub TestPushDouble()
Try
Console.WriteLine("Pushing elements onto doubleStack")
For Each element As Double In doubleElements
Console.Write("{0:F1} ", element)
doubleStack.Push(element)
Next element
Catch exception As GenericStackException
Console.Error.WriteLine("Message: " & exception.Message)
Console.Error.WriteLine(exception.StackTrace)
End Try
End Sub
Sub TestPopDouble()
Try
Console.WriteLine("Popping elements from doubleStack")
Dim popValue As Double
While True
popValue = doubleStack.Pop()
Console.Write("{0:F1} ", popValue)
End While
Catch exception As GenericStackException
Console.Error.WriteLine()
Console.Error.WriteLine("Message: " & exception.Message)
Console.Error.WriteLine(exception.StackTrace)
End Try
End Sub
Sub TestPushInteger()
Try
Console.WriteLine("Pushing elements onto integerStack")
For Each element As Integer In integerElements
Console.Write("{0} ", element)
integerStack.Push(element) " push onto integerStack
Next element
Catch exception As GenericStackException
Console.Error.WriteLine()
Console.Error.WriteLine("Message: " & exception.Message)
Console.Error.WriteLine(exception.StackTrace)
End Try
End Sub
Sub TestPopInteger()
Try
Console.WriteLine("Popping elements from integerStack")
Dim popValue As Integer
While True
popValue = integerStack.Pop()
Console.Write("{0} ", popValue)
End While
Catch exception As GenericStackException
Console.Error.WriteLine()
Console.Error.WriteLine("Message: " & exception.Message)
Console.Error.WriteLine(exception.StackTrace)
End Try
End Sub
End Module
Public Class Stack(Of E)
Private top As Integer
Private elements() As E
Public Sub New()
MyClass.New(10)
End Sub " New
Public Sub New(ByVal stackSize As Integer)
If stackSize > 0 Then
elements = New E(stackSize - 1) {}
Else
elements = New E(9) {}
End If
top = -1
End Sub " New
Public Sub Push(ByVal pushValue As E)
If top = elements.Length - 1 Then
Throw New GenericStackException(String.Format("Stack is full, cannot push {0}", pushValue))
End If
top += 1
elements(top) = pushValue
End Sub
Public Function Pop() As E
If top = -1 Then
Throw New GenericStackException("Stack is empty, cannot pop")
End If
top -= 1
Return elements(top + 1)
End Function
End Class
Public Class GenericStackException : Inherits ApplicationException
Public Sub New()
MyBase.New("Stack is empty")
End Sub " New
Public Sub New(ByVal exception As String)
MyBase.New(exception)
End Sub
End Class
Pushing elements onto doubleStack 1.1 2.2 3.3 4.4 5.5 6.6 Message: Stack is full, cannot push 6.6 at Stack`1.Push(E pushValue) at StackTest.TestPushDouble() Popping elements from doubleStack 5.5 4.4 3.3 2.2 1.1 Message: Stack is empty, cannot pop at Stack`1.Pop() at StackTest.TestPopDouble() Pushing elements onto integerStack 1 2 3 4 5 6 7 8 9 10 11 Message: Stack is full, cannot push 11 at Stack`1.Push(E pushValue) at StackTest.TestPushInteger() Popping elements from integerStack 10 9 8 7 6 5 4 3 2 1 Message: Stack is empty, cannot pop at Stack`1.Pop() at StackTest.TestPopInteger()