-
Notifications
You must be signed in to change notification settings - Fork 1
/
midterm.cpp
155 lines (138 loc) · 3.82 KB
/
midterm.cpp
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
151
152
153
154
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <new>
using namespace std;
template<class T>
class arrayclass {
public:
typedef const T* const_iterator;
arrayclass(int size = 0)
: m_capacity(size), m_size(0), m_array(nullptr) {
if (size < 0) {
ostringstream oss;
oss << "cannot create an arrayclass of cpacity " << size;
throw domain_error(oss.str().c_str());
}
if (size > 0)
m_array = new T[size];
}
arrayclass(const arrayclass<T> & original);
bool full() const { return m_size >= m_capacity; }
bool empty() const { return m_size == 0; }
int get_capacity() const { return m_capacity; };
int get_size() const { return m_size; }
const_iterator begin() { return m_array; }
const_iterator end() { return m_array + m_size; }
T at(int index) const {
if (index >= m_size || index < 0)
throw out_of_range("index out of range");
return m_array[index];
}
~arrayclass() { if (m_array != nullptr) delete[] m_array; }
protected:
T* m_array;
int m_size;
private:
int m_capacity;
};
template<class T>
arrayclass<T>::arrayclass(const arrayclass<T>& original)
: m_array(nullptr), m_size(original.get_size()),
m_capacity(original.get_capacity())
{
m_array = new T[m_capacity];
for (int i = 0; i < m_size; i++) {
m_array[i] = original.at(i);
}
}
template <class T>
class arrayStack : public arrayclass<T> {
public:
arrayStack(int size = 0) : arrayclass(size) {}
arrayStack(const arrayStack & other) : arrayclass(other) {}
void push(T item) {
if (full()) throw domain_error("array is full");
m_array[m_size++] = item;
}
T pop() {
if (m_size != 0)
return m_array[--m_size];
throw domain_error("array is empty");
}
};
int main(void) {
arrayStack<long> **st = nullptr;
arrayStack<long> singleStack(5);
long element = 3L;
int numElements = 2;
int exitCode = EXIT_SUCCESS;
try {
/* ** B CORRECTION ** ( -9 pts )
I previously thought you needed some double pointer
magic to make this work, but I was wrong. You only
need one pointer when declaring an array or pointers.
*/
arrayStack<long> * st[2]{
new arrayStack<long>(3),
new arrayStack<long>(2)
};
/* ** C CORRECTION ** ( -6 pts )
Ranges in the second argument of for loop arguments were
incorrect. Changed them to cover 3, 4, 5 and 6, 7
respectively.
*/
for (long i = 3; i < 6; st[0]->push(i++));
for (long i = 6; i < 8; st[1]->push(i++));
/* ** D CORRECTION ** ( -6 pts )
Previously thought `iterator` was a builtin I could use
to reference to `st[i]->begin()`. Instead, I should've
used `arrayStack<long>::const_iterator`. I also had the
wrong index in the first for loop; it should've been
`i < 2` and not `i < 3`.
Additionally, I just had no idea what to do for the copy
and print. So I tried cheating it by copying to another
stack and then using a for loop to print that stack.
Evidently it didn't work to nobody's surprise. Here the
correct answer can be found on the first page of the mid-
term.
*/
for (short i = 0; i < 2; i++) {
for (arrayStack<long>::const_iterator k = st[i]->begin();
k != st[i]->end(); k++) {
cout << *k << ' ';
}
cout << endl;
while (!st[i]->empty()) {
singleStack.push(st[i]->pop() + 1);
}
while (!singleStack.empty()) {
st[i]->push(singleStack.pop());
}
ostream_iterator<long> outIt(cout, " | ");
copy(st[i]->begin(), st[i]->end(), outIt);
cout << endl;
}
}
/* ** A & E CORRECTION ( -4 pts ) **
Forgot to catch `domain_error`s and fixed index error in other for loop
again.
*/
catch (bad_alloc) {
exitCode = EXIT_FAILURE;
}
catch (domain_error) {
exitCode = EXIT_FAILURE;
}
if (st != nullptr) {
for (int i = 0; i < 2; i++)
delete st[i];
delete[] st;
}
return exitCode;
}