int max(int x, int y) { return (x > y)? x : y; } double max(double x, double y) { return (x > y)? x : y; } max(17, 42) max( , 2

int max(int x, int y) { return (x > y) ? x : y; } double max(double x, double y) { return (x > y) ? x : y; } max(17, 42) max(3.14159, 2.71828) templat...
Author: Lawrence Lyons
0 downloads 0 Views 95KB Size
int max(int x, int y) { return (x > y) ? x : y; } double max(double x, double y) { return (x > y) ? x : y; } max(17, 42) max(3.14159, 2.71828) template ValueType max(ValueType x, ValueType y) { return (x > y) ? x : y; } max('A', 'Z') char max(char x, char y) { return (x > y) ? x : y; } max("cat", "dog") char *max(char *x, char *y) { return (x > y) ? x : y; } max(string("cat"), string("dog"))

/* * File: stack.h * ------------* This interface exports the Stack class, which implements a collection * that processes values in a last-in/first-out (LIFO) order. */ #ifndef _stack_h #define _stack_h /* * Class: Stack * ----------------------* This class models a linear structure called a stack in which values are * added and removed only from one end. This discipline gives rise to a * last-in/first-out behavior (LIFO) that is the defining feature of * stacks. The fundamental stack operations are push (add to top) and pop * (remove from top). */ template class Stack { public: /* * Constructor: Stack * Usage: Stack stack; * -----------------------------* Initializes a new empty stack. */ Stack(); /* * Destructor: ~Stack * Usage: (usually implicit) * ------------------------* Frees any heap storage associated with this stack. */ ~Stack(); /* * Method: size * Usage: int n = stack.size(); * ---------------------------* Returns the number of values in this stack. */ int size(); /* * Method: isEmpty * Usage: if (stack.isEmpty()) . . . * --------------------------------* Returns true if this stack contains no elements. */ bool isEmpty();

/* * Method: clear * Usage: stack.clear(); * --------------------* Removes all elements from this stack. */ void clear(); /* * Method: push * Usage: stack.push(value); * ------------------------* Pushes the specified value onto this stack. */ void push(ValueType value); /* * Method: pop * Usage: ValueType top = stack.pop(); * ----------------------------------* Removes the top element from this stack and returns it. This * method signals an error if called on an empty stack. */ ValueType pop(); /* * Method: peek * Usage: ValueType top = stack.peek(); * -----------------------------------* Returns the value of top element from this stack, without removing * it. This method signals an error if called on an empty stack. */ ValueType peek(); #include "stackpriv.h" }; #include "stackimpl.cpp" #endif

/* * File: stackpriv.h * ----------------* This file contains the private section for the array-based * implementation of the Stack class. */ private: static const int INITIAL_CAPACITY = 10; /* Instance variables */ ValueType *array; /* A dynamic array of the elements */ int count; /* The number of elements on the stack */ int capacity; /* The allocated size of the array */ /* Private method prototypes */ void expandCapacity(); /* Make it illegal to copy stacks */ Stack(const Stack & value) { } const Stack & operator=(const Stack & rhs) { return *this; }

/* * File: error.h * ------------* This file defines a simple function for reporting errors. */ #ifndef _error_h #define _error_h /* * Function: error * Usage: error(msg); * -----------------* Writes the string msg to the cerr stream and then exits the program * with a standard status code indicating failure. The usual pattern for * using error is to enclose the call to error inside an if statement that * checks for a particular condition, which might look something like this: * * if (divisor == 0) error("Division by zero"); */ void error(std::string msg); #endif

/* * File: error.cpp * --------------* This file implements the error.h interface. */ #include #include #include "error.h" using namespace std; /* * Implementation notes: error * --------------------------* This function writes out the error message to the cerr stream and * then exits the program. The EXIT_FAILURE constant is defined in * to represent a standard failure code. */ void error(string msg) { cerr

Suggest Documents