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
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.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(11875)
Hello! Python by Anthony Briggs(9365)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9337)
The Mikado Method by Ola Ellnestam Daniel Brolund(9300)
Dependency Injection in .NET by Mark Seemann(8853)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7845)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7306)
Grails in Action by Glen Smith Peter Ledbrook(7291)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7111)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5951)
Kotlin in Action by Dmitry Jemerov(4633)
Practical Vim (for Kathryn Amaral) by Drew Neil(3724)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3590)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3390)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3236)
Learning Java by Patrick Niemeyer & Daniel Leuck(2867)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2831)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(2584)
Mastering Java 9 by Dr. Edward Lavieri(2569)