summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Codebeispiele/5_ch11
diff options
context:
space:
mode:
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/5_ch11')
-rw-r--r--Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h98
-rw-r--r--Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp59
-rw-r--r--Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp67
-rw-r--r--Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp63
4 files changed, 287 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h b/Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h
new file mode 100644
index 0000000..d689030
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h
@@ -0,0 +1,98 @@
+// Fig. 11.2: tstack1.h
+// Stack class template.
+#ifndef TSTACK1_H
+#define TSTACK1_H
+
+template< class T >
+class Stack {
+
+public:
+ Stack( int = 10 ); // default constructor (stack size 10)
+
+ // destructor
+ ~Stack()
+ {
+ delete [] stackPtr;
+
+ } // end ~Stack destructor
+
+ bool push( const T& ); // push an element onto the stack
+ bool pop( T& ); // pop an element off the stack
+
+ // determine whether Stack is empty
+ bool isEmpty() const
+ {
+ return top == -1;
+
+ } // end function isEmpty
+
+ // determine whether Stack is full
+ bool isFull() const
+ {
+ return top == size - 1;
+
+ } // end function isFull
+
+private:
+ int size; // # of elements in the stack
+ int top; // location of the top element
+ T *stackPtr; // pointer to the stack
+
+}; // end class Stack
+
+// constructor
+template< class T >
+Stack< T >::Stack( int s )
+{
+ size = s > 0 ? s : 10;
+ top = -1; // Stack initially empty
+ stackPtr = new T[ size ]; // allocate memory for elements
+
+} // end Stack constructor
+
+// push element onto stack;
+// if successful, return true; otherwise, return false
+template< class T >
+bool Stack< T >::push( const T &pushValue )
+{
+ if ( !isFull() ) {
+ stackPtr[ ++top ] = pushValue; // place item on Stack
+ return true; // push successful
+
+ } // end if
+
+ return false; // push unsuccessful
+
+} // end function push
+
+// pop element off stack;
+// if successful, return true; otherwise, return false
+template< class T >
+bool Stack< T >::pop( T &popValue )
+{
+ if ( !isEmpty() ) {
+ popValue = stackPtr[ top-- ]; // remove item from Stack
+ return true; // pop successful
+
+ } // end if
+
+ return false; // pop unsuccessful
+
+} // end function pop
+
+#endif
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/ \ No newline at end of file
diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp
new file mode 100644
index 0000000..549cc6b
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp
@@ -0,0 +1,59 @@
+// Fig 11.1: fig11_01.cpp
+// Using template functions.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+// function template printArray definition
+template< class T >
+void printArray( const T *array, const int count )
+{
+ for ( int i = 0; i < count; i++ )
+ cout << array[ i ] << " ";
+
+ cout << endl;
+
+} // end function printArray
+
+int main()
+{
+ const int aCount = 5, bCount = 7, cCount = 6;
+
+ int a[ aCount ] = { 1, 2, 3, 4, 5 };
+ double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
+ char c[ cCount ] = "HELLO"; // 6th position for null
+
+ cout << "Array a contains:" << endl;
+
+ // call integer function-template specialization
+ printArray( a, aCount );
+
+ cout << "Array b contains:" << endl;
+
+ // call double function-template specialization
+ printArray( b, bCount );
+
+ cout << "Array c contains:" << endl;
+
+ // call character function-template specialization
+ printArray( c, cCount );
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/ \ No newline at end of file
diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp
new file mode 100644
index 0000000..725cbe1
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp
@@ -0,0 +1,67 @@
+// Fig. 11.3: fig11_03.cpp
+// Stack class template test program.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::endl;
+
+#include "tstack1.h" // Stack class template definition
+
+int main()
+{
+ Stack< double > doubleStack( 5 );
+ double doubleValue = 1.1;
+
+ cout << "Pushing elements onto doubleStack\n";
+
+ while ( doubleStack.push( doubleValue ) ) {
+ cout << doubleValue << ' ';
+ doubleValue += 1.1;
+
+ } // end while
+
+ cout << "\nStack is full. Cannot push " << doubleValue
+ << "\n\nPopping elements from doubleStack\n";
+
+ while ( doubleStack.pop( doubleValue ) )
+ cout << doubleValue << ' ';
+
+ cout << "\nStack is empty. Cannot pop\n";
+
+ Stack< int > intStack;
+ int intValue = 1;
+ cout << "\nPushing elements onto intStack\n";
+
+ while ( intStack.push( intValue ) ) {
+ cout << intValue << ' ';
+ ++intValue;
+
+ } // end while
+
+ cout << "\nStack is full. Cannot push " << intValue
+ << "\n\nPopping elements from intStack\n";
+
+ while ( intStack.pop( intValue ) )
+ cout << intValue << ' ';
+
+ cout << "\nStack is empty. Cannot pop\n";
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/ \ No newline at end of file
diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp
new file mode 100644
index 0000000..852c4c4
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp
@@ -0,0 +1,63 @@
+// Fig. 11.4: fig11_04.cpp
+// Stack class template test program. Function main uses a
+// function template to manipulate objects of type Stack< T >.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::endl;
+
+#include "tstack1.h" // Stack class template definition
+
+// function template to manipulate Stack< T >
+template< class T >
+void testStack(
+ Stack< T > &theStack, // reference to Stack< T >
+ T value, // initial value to push
+ T increment, // increment for subsequent values
+ const char *stackName ) // name of the Stack < T > object
+{
+ cout << "\nPushing elements onto " << stackName << '\n';
+
+ while ( theStack.push( value ) ) {
+ cout << value << ' ';
+ value += increment;
+
+ } // end while
+
+ cout << "\nStack is full. Cannot push " << value
+ << "\n\nPopping elements from " << stackName << '\n';
+
+ while ( theStack.pop( value ) )
+ cout << value << ' ';
+
+ cout << "\nStack is empty. Cannot pop\n";
+
+} // end function testStack
+
+int main()
+{
+ Stack< double > doubleStack( 5 );
+ Stack< int > intStack;
+
+ testStack( doubleStack, 1.1, 1.1, "doubleStack" );
+ testStack( intStack, 1, 1, "intStack" );
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/ \ No newline at end of file