/****************************************************************************/ /* Sample C++ "dynamic stack" tester demonstrating the usage of DStack. */ /* The DStack struct is declared and functions are prototyped in dstack.h.*/ /* The C++ code for the functions is in dstack.cc. */ /* */ /* Compile with: */ /* g++ -o tdstack tdstack.cc dstack.cc */ /* or: */ /* CC -o tdstack tdstack.cc dstack.cc */ /* */ /* Usage: */ /* tdstack */ /* */ /****************************************************************************/ #include #include "dstack.h" int main() { DStack S(4); S.print(); cout << "\n"; S.push( 2.31); S.push(1.19); S.push(6.78); S.push(0.54); S.print(); cout << "\n"; if (!S.full()) S.push(6.7); // this should do nothing, as // stack is already full. S.print(); cout << "\n"; cout << "Popped value is: " << S.pop() << "\n"; S.print(); cout << "\n"; S.push(S.pop() + S.pop()); cout << "Replace top two items with their sum: \n"; S.print(); cout << "\n"; S.pop(); S.pop(); S.print(); cout << "\n"; if (!S.empty()) S.pop(); // this should also do nothing, // as stack is already empty. if (S.num_items() != 0) { cout << "Error: Stack is corrupt!\n"; } // destructor for S automatically called return 0; }