1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <iostream> using namespace std;
class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; }
string getMessage() const { return errorMsg; } };
class StackEmpty : public RuntimeException { public: StackEmpty(const string& err) : RuntimeException(err) {} };
class StackFull : public RuntimeException { public: StackFull(const string& err) : RuntimeException(err) {} };
template <typename E> class ArrayStack { enum { DEF_CAPACITY = 100}; public: ArrayStack(int cap = DEF_CAPACITY); int size() const; bool empty() const; const E& top() const throw(StackEmpty); void push(const E& e) throw(StackFull); void pop() throw(StackEmpty); E* spans2(E*, int); void printArray(E*, int); E peakPricebySpan(E*, int);
~ArrayStack();
private: E* S; int capacity; int t;
};
template <typename E> ArrayStack<E>::ArrayStack(int cap) : S(new E[cap]), capacity(cap), t(-1) { }
template <typename E> int ArrayStack<E>::size() const { return (t + 1); }
template <typename E> bool ArrayStack<E>::empty() const { return (t < 0); }
template <typename E> const E& ArrayStack<E>::top() const throw(StackEmpty) { if (empty()) throw StackEmpty("Top of empty stack"); return S[t]; }
template <typename E> void ArrayStack<E>::push(const E& e) throw(StackFull) { if (size() == capacity) throw StackFull("Push to full stack"); S[++t] = e; }
template <typename E> void ArrayStack<E>::pop() throw (StackEmpty) { if (empty()) throw StackEmpty("Pop from empty stack"); --t; }
template <typename E> E* ArrayStack<E>::spans2(E* X, int n) { E* S = new E[n]; ArrayStack<E> A; for (int i = 0; i <= n - 1; i++) { while (!A.empty() && X[A.top()] <= X[i]) A.pop();
if (A.empty()) S[i] = i + 1; else S[i] = i - A.top();
A.push(i); }
return S; }
template <typename E> void ArrayStack<E>::printArray(E arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << ", "; }
template <typename E> E ArrayStack<E>::peakPricebySpan(E arr[], int n) { int position; E spanVal = arr[0]; for (int i = 1; i < n; i++) { if (spanVal < arr[i]) { spanVal = arr[i]; position = i; } }
return position; }
template <typename E> ArrayStack<E>::~ArrayStack() { delete [] S; }
int main() { const int SIZE = 6; int stockA[SIZE] = {30333, 30423, 30523, 30185, 29634, 30038};
ArrayStack<int> price; int* spanValue = price.spans2(stockA, SIZE);
cout << "The price of the last " << SIZE << " days of the stock are: ";
for (int i = 0; i < SIZE; i++) cout << stockA[i] << ", ";
cout << "\nThe corresponding span values of the stock are: ";
price.printArray(spanValue, SIZE);
cout << "\nThe peak price of the last " << SIZE << " days of the stock is: " << stockA[price.peakPricebySpan(spanValue, SIZE)];
return 0; }
|