C# 2012 for Programmers (5th Edition) (Deitel Developer Series) by Deitel Paul J. & Deitel Harvey M

C# 2012 for Programmers (5th Edition) (Deitel Developer Series) by Deitel Paul J. & Deitel Harvey M

Author:Deitel, Paul J. & Deitel, Harvey M. [Deitel, Paul J.]
Language: eng
Format: epub
Publisher: Pearson Education
Published: 2013-09-25T00:00:00+00:00


Click here to view code image

* * *

1 // Fig. 18.9: StackTest.cs

2 // Testing generic class Stack.

3 using System;

4 using System.Collections.Generic;

5

6 class StackTest

7 {

8 // create arrays of doubles and ints

9 private static double[] doubleElements =

10 new double[] { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };

11 private static int[] intElements =

12 new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

13

14 private static Stack< double > doubleStack; // stack stores doubles

15 private static Stack< int > intStack; // stack stores int objects

16

17 public static void Main( string[] args )

18 {

19 doubleStack = new Stack< double >( 5 ); // stack of doubles

20 intStack = new Stack< int >( 10 ); // stack of ints

21

22 // push doubles onto doubleStack

23 TestPush( "doubleStack", doubleStack, doubleElements );

24 // pop doubles from doubleStack

25 TestPop( "doubleStack", doubleStack );

26 // push ints onto intStack

27 TestPush( "intStack", intStack, intElements );

28 // pop ints from intStack

29 TestPop( "intStack", intStack );

30 } // end Main

31

32 // test Push method

33 private static void TestPush< T >( string name, Stack< T > stack,

34 IEnumerable< T > elements )

35 {

36 // push elements onto stack

37 try

38 {

39 Console.WriteLine( "\nPushing elements onto " + name );

40

41 // push elements onto stack

42 foreach ( var element in elements )

43 {

44 Console.Write( "{0} ", element );

45 stack.Push( element ); // push onto stack

46 } // end foreach

47 } // end try

48 catch ( FullStackException exception )

49 {

50 Console.Error.WriteLine();

51 Console.Error.WriteLine( "Message: " + exception.Message );

52 Console.Error.WriteLine( exception.StackTrace );

53 } // end catch

54 } // end method TestPush

55

56 // test Pop method

57 private static void TestPop< T >( string name, Stack< T > stack )

58 {

59 // pop elements from stack

60 try

61 {

62 Console.WriteLine( "\nPopping elements from " + name );

63

64 T popValue; // store element removed from stack

65

66 // remove all elements from stack

67 while ( true )

68 {

69 popValue = stack.Pop(); // pop from stack

70 Console.Write( "{0} ", popValue );

71 } // end while

72 } // end try

73 catch ( EmptyStackException exception )

74 {

75 Console.Error.WriteLine();

76 Console.Error.WriteLine( "Message: " + exception.Message );

77 Console.Error.WriteLine( exception.StackTrace );

78 } // end catch

79 } // end TestPop

80 } // end class StackTest



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.