blob: cad7e80a4abfa63521478f8276253089ac9526bc (
plain) (
blame)
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
|
#include "Exercise4.h"
void FloatArray::free() {
delete[] numbers;
}
void FloatArray::copyFrom(const FloatArray& other) {
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
this->numbers = new float[allocated];
for (int i = 0; i < this->lastUnused; i++) {
this->numbers[i] = other.numbers[i];
}
}
void FloatArray::resize() {
allocated *= 2;
float* moreNumbers = new float[allocated];
for (int i = 0; i < lastUnused; i++) {
moreNumbers[i] = numbers[i];
}
delete[] numbers;
numbers = moreNumbers;
}
FloatArray::FloatArray() {
numbers = nullptr;
lastUnused = allocated = 0;
}
FloatArray::~FloatArray() {
free();
}
FloatArray::FloatArray(const FloatArray& other) {
copyFrom(other);
}
FloatArray& FloatArray::operator=(const FloatArray& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
FloatArray::FloatArray(FloatArray&& other) {
this->numbers = other.numbers;
other.numbers = nullptr;
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
}
FloatArray& FloatArray::operator=(FloatArray&& other) {
if (this != &other) {
free();
this->numbers = other.numbers;
other.numbers = nullptr;
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
}
return *this;
}
void FloatArray::Add(float number) {
if (allocated == lastUnused) {
resize();
}
numbers[lastUnused++] = number;
}
FloatArray& FloatArray::operator+=(const FloatArray& other) {
for (int i = 0; i < other.lastUnused; i++) {
this->Add(other.numbers[i]);
}
return *this;
}
FloatArray& FloatArray::operator+=(float number) {
for (int i = 0; i < lastUnused; i++) {
numbers[i] += number;
}
return *this;
}
float& FloatArray::operator[](int index) {
if (index < 0 || lastUnused <= index) {
throw "Invalid index!";
}
return numbers[index];
}
FloatArray operator+(const FloatArray& left, const FloatArray& right) {
FloatArray output;
(output += left) += right;
return output;
}
FloatArray operator+(const FloatArray& left, float right) {
FloatArray output = left; // Копиращ конструктор
output += right;
return output;
}
bool operator==(const FloatArray& left, const FloatArray& right) {
if (left.lastUnused != right.lastUnused) {
return false;
}
for (int i = 0; i < left.lastUnused; i++) {
if (left.numbers[i] != right.numbers[i]) {
return false;
}
}
return true;
}
bool operator!=(const FloatArray& left, const FloatArray& right) {
return !(left == right);
}
std::ostream& operator<<(std::ostream& ostr, const FloatArray& right) {
ostr << right.lastUnused << " ";
for (int i = 0; i < right.lastUnused; i++) {
ostr << right.numbers[i] << " ";
}
return ostr;
}
std::istream& operator>>(std::istream& istr, FloatArray& right) {
int length;
istr >> length;
right.lastUnused = 0;
for (int i = 0; i < length; i++) {
float temp;
istr >> temp;
right.Add(temp);
}
return istr;
}
|