diff options
| author | Syndamia <kamen@syndamia.com> | 2024-04-22 15:43:49 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-04-22 15:43:49 +0300 |
| commit | f3406754705e508f57768d3d0d8e457ff5b7620c (patch) | |
| tree | 9f8743208732c21d5dcb51302feb29058edd8e58 /week08/Exercise4.cpp | |
| parent | f7ed9a8a6c31b17d54d2f37cc0f12b64f8e4b6d2 (diff) | |
| download | oop-2023-solutions-f3406754705e508f57768d3d0d8e457ff5b7620c.tar oop-2023-solutions-f3406754705e508f57768d3d0d8e457ff5b7620c.tar.gz oop-2023-solutions-f3406754705e508f57768d3d0d8e457ff5b7620c.zip | |
[w8] Added exercise descriptions and solutions to ex 1-7
Diffstat (limited to 'week08/Exercise4.cpp')
| -rw-r--r-- | week08/Exercise4.cpp | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/week08/Exercise4.cpp b/week08/Exercise4.cpp new file mode 100644 index 0000000..28b3e2d --- /dev/null +++ b/week08/Exercise4.cpp @@ -0,0 +1,141 @@ +#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) { + 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; +} |
