//--------------------------------------------------------- // A stack class. // // D. Searls // REPLACE THIS LINE WITH YOUR NAME!! //--------------------------------------------------------- #include using namespace std; template class stack { private: public: //----------------------------------------------------- // Default Constructor // // Constructs an empty stack. //----------------------------------------------------- stack() { } //----------------------------------------------------- // Destructor // // Remove all of the items from the stack //----------------------------------------------------- ~stack() { } //----------------------------------------------------- // push // // Push the specified item onto the top of the stack. // // Postconditions: size() will be incremented by 1 and // top() will be equal to item. // // In Parameter: item //----------------------------------------------------- void push(const itemType& item) { } //----------------------------------------------------- // pop // // Remove and discard the item on the top of the stack. // // Precondition: empty() is false. // // Postcondition: size() will be decremented by 1. //----------------------------------------------------- void pop() { } //----------------------------------------------------- // size // // Return the number of items in the stack. //----------------------------------------------------- int size() { } //----------------------------------------------------- // empty // // Return true if the stack is empty and false // otherwise. //----------------------------------------------------- bool empty() { } //----------------------------------------------------- // top // // Return the item on the top of the stack. // // Precondition: empty() is false. //----------------------------------------------------- itemType& top() { } }; //----------------------------------------------- // Function: factorial // // Returns: n! // // In Parameter: n //----------------------------------------------- int factorial(int n) { stack intStack; int fact; while (n > 0) { intStack.push(n); n--; } fact = 1; while (!intStack.empty()) { fact = fact * intStack.top(); intStack.pop(); } return fact; } //----------------------------------------------- // Function: power // // Preconditions: p is nonnegative // // Returns: b^p // // In Parameter: b, p //----------------------------------------------- double power(double b, int p) { stack dblStack; double result; while (p > 0) { dblStack.push(b); p--; } result = 1; while (!dblStack.empty()) { result = result * dblStack.top(); dblStack.pop(); } return result; } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { cout <<"Factorials\n\n"; for(int i = 0; i < 13; i++) { cout << i << "! = " << factorial(i) << endl; } cout << endl << endl; cout << "Powers of 2\n\n"; cout << fixed << setprecision(0); for (int i = 0; i < 21; i++) { cout << "2^" << i << " = " << power(2, i) << endl; } cout << endl; return 0; }