-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.cpp
executable file
·174 lines (147 loc) · 2.71 KB
/
Array.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Array.cpp
#include <iostream>
#include <cstdlib>
#include "Array.h"
// Default Constructor
Array::Array()
: mLen(0), mA(NULL)
{
}
// Non-default constructor
Array::Array(int aLen)
{
allocate(aLen);
for (int i = 0; i < length(); i++) {
(*this)[i] = 0;
}
}
// Copy constructor
Array::Array(const Array& other)
{
copy(other);
}
// Assignment operator
Array& Array::operator=(const Array& other)
{
if (this != &other) {
cleanup();
copy(other);
}
return *this;
}
// Destructor
Array::~Array()
{
cleanup();
}
void Array::input(std::istream & s)
{
int len = 0;
std::cout << "Length of array: ";
s >> len;
allocate(len);
for (int i = 0; i < length(); i++) {
std::cout << "A[" << i << "] = ";
s >> (*this)[i];
}
}
void Array::output(std::ostream & s) const
{
s << "{";
for (int i = 0; i < mLen; i++) {
s << " " << (*this)[i];
}
s << " }";
}
const int & Array::operator[](int i) const
{
if (i < 0 || i >= mLen) {
std::cout << "Invalid array index." << std::endl;
exit(1);
}
return mA[i];
}
int & Array::operator[](int i)
{
if (i < 0 || i >= mLen) {
std::cout << "Invalid array index." << std::endl;
exit(1);
}
return mA[i];
}
int Array::length() const
{
return mLen;
}
void Array::allocate(int aLen)
{
if (aLen < 0) {
std::cout << "Negative index." << std::endl;
mLen = 0;
mA = nullptr;
return;
}
setLength(aLen);
mA = new int[aLen];
}
void Array::setLength(int aLen)
{
mLen = aLen;
}
void Array::copy(const Array & other)
{
allocate(other.length());
for (int i = 0; i < length(); i++) {
(*this)[i] = other[i];
}
}
void Array::cleanup()
{
delete [] mA;
}
std::istream& operator>>(std::istream& s, Array& a)
{
a.input(s);
return s;
}
std::ostream& operator<<(std::ostream& s, const Array& a)
{
a.output(s);
return s;
}
bool operator==(const Array& a1, const Array& a2)
{
if (a1.length() != a2.length()) {
return false;
}
for (int i = 0; i < a1.length(); ++i) {
if (a1[i] != a2[i]) {
return false;
}
}
return true;
}
Array operator+(const Array & a1, const Array & a2)
{
if (a1.length() != a2.length()) {
std::cout << "Inconsistent lengths of arrays." << std::endl;
exit(1);
}
Array result(a1);
for (int i = 0; i < result.length(); i++) {
result[i] += a2[i];
}
return result;
}
Array operator+(const Array& a1, int x)
{
Array result(a1);
for (int i = 0; i < result.length(); i++) {
result[i] += x;
}
return result;
}
Array operator+(int x, const Array& a2)
{
return (a2 + x);
}