From 6d4fa30cfd908e20a2ca2c70864a07007347ce98 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 19 Apr 2022 15:11:17 +0300 Subject: Added work from previous oop practice --- .../08-Practice-12.04.2022/Vector.cpp | 148 +++++++++++++++++++++ .../08-Practice-12.04.2022/Vector.h | 40 ++++++ .../08-Practice-12.04.2022/source.cpp | 6 + 3 files changed, 194 insertions(+) create mode 100644 Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp create mode 100644 Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.h create mode 100644 Year 1/Object-oriented Programming/08-Practice-12.04.2022/source.cpp (limited to 'Year 1/Object-oriented Programming') diff --git a/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp new file mode 100644 index 0000000..073d12d --- /dev/null +++ b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp @@ -0,0 +1,148 @@ +#include "Vector.h" + +void Vector::resize() +{ + capacity *= 2; + int* newArr = new int[capacity]; + for (size_t i = 0; i < size; i++) + { + newArr[i] = arr[i]; + } + free(); + arr = newArr; +} + +void Vector::free() +{ + delete[] arr; +} + +void Vector::copyFrom(const Vector& other) +{ + size = other.size; + capacity = other.capacity; + arr = new int[capacity]; + for (int i = 0; i < size; i++) + { + arr[i] = other.arr[i]; + } +} + +void Vector::swap(const int ind1, const int ind2) { + int temp = arr[ind1]; + arr[ind1] = arr[ind2]; + arr[ind2] = temp; +} + +Vector::Vector() +{ + arr = new int[2]; + capacity = 2; + size = 0; +} + +Vector::Vector(const Vector& other) +{ + copyFrom(other); +} + +Vector& Vector::operator=(const Vector& other) +{ + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Vector::~Vector() +{ + free(); +} + +void Vector::push_back(const int element) +{ + if (size < capacity) { + arr[size] = element; + size++; + } + else + { + resize(); + push_back(element); + } +} + +int Vector::getSize() const +{ + return size; +} + +int& Vector::operator[](const int index) +{ + if (index >= 0 && index < size) { + return arr[index]; + } + throw "Out of range exception"; + +} + +const int& Vector::operator[](int index) const +{ + if (index >= 0 && index < size) { + return arr[index]; + } + throw "Out of range exception"; +} + +void Vector::deleteAt(const int index) { + for (int i = index; i < size; i++) { + arr[i] = arr[i + 1]; + } + pop_back(); +} + +void Vector::pop_back() { + arr[size--] = int(); +} + +int Vector::findItem(const int item) const { + for (int i = 0; i < size; i++) { + if (arr[i] == item) + return i; + } + return -1; +} + +void Vector::clear() { + delete[] arr; + size = capacity = 0; +} + +void Vector::sort() { + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + if (arr[i] > arr[j]) + swap(i, j); + } + } +} + +void Vector::reverse() { + for (int halfSize = size / 2, offset = 0; offset < halfSize; offset++) + swap(offset, size - offset - 1); +} + +std::ostream& operator<<(std::ostream& ostr, const Vector& vec) { + ostr << "<"; + for (int i = 0; i < vec.getSize() - 2; i++) { + ostr << vec[i] << ", "; + } + ostr << vec[vec.getSize() - 1] << ">"; + return ostr; +} + +std::istream& operator<<(std::istream& istr, const Vector& vec) { + + return istr; +} diff --git a/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.h b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.h new file mode 100644 index 0000000..7377c31 --- /dev/null +++ b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.h @@ -0,0 +1,40 @@ +#pragma once +// #include +#include +#include +#include + +class Vector +{ + int* arr = nullptr; + int size; + int capacity; + + void resize(); + void free(); + void copyFrom(const Vector&); + + void swap(const int, const int); + +public: + Vector(); + Vector(const Vector&); + Vector& operator = (const Vector&); + ~Vector(); + + void push_back(const int); + int getSize()const; + int& operator [](const int); + const int& operator [](int)const ; + + void deleteAt(const int); + void pop_back(); + int findItem(const int) const; + void clear(); + void sort(); + void print() const; + void reverse(); +}; + +std::ostream& operator<<(std::ostream&, const Vector&); +std::istream& operator>>(std::istream&, const Vector&); diff --git a/Year 1/Object-oriented Programming/08-Practice-12.04.2022/source.cpp b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/source.cpp new file mode 100644 index 0000000..a4b7741 --- /dev/null +++ b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/source.cpp @@ -0,0 +1,6 @@ +#include "Vector.h" +#include + +int main() { + std::cout << "Hello"; +} -- cgit v1.3.1-23-gd82d