protected: void grow(int newmincap = -1) { int newcap if (newmincap < 0 || newmincap < 2 * cap) { newcap = cap * 2; if (newcap == 0) newcap = 1; } else newcap = newmincap; int * newdata = new int[newcap]; for (int i = 0; i < num; i += 1) newdata[i] = data[i]; delete [] data; cap = newcap; data = newdata; } public: void growtoinclude(int pos) { grow(pos + 1); } void push(int d) { if (num == cap) grow(); data[num] = d; num += 1; } int pop() { assert(num > 0); num -= 1; return data[num]; } // user enters 2 3 4 * * * // or 2 3 4 * bool empty() { return num == 0; } int size() { return num; } /* stack operations vectorofints stack; user enters number N: stack.push(N); user enters operator "-": check stack.size() >= 2 error message for user stop evaluating this expression reached end of formula: check stack.size() == 1 error and no result if not stack.pop() and print answer */ /* used to being able to say: A[i] = x; and x = A[i]; but now we need A.set(i, x); and x = A.get(i); */ int & at(int i) { assert(i >= 0 && i < num); return data[i]; } /* now it becomes A.at(i) = x; and x = A.at(i); */ int & operator[](int pos) { assert(i >= 0 && i < num); return data[i]; } /* can now say A[i] = x; translates into A.operator[](i) = x; and x = A[i]; translates into x = A.operator[](i); */