From 42f59920cf5d300a54f3dae03911dccbd1a86eea Mon Sep 17 00:00:00 2001 From: angeld55 Date: Wed, 12 May 2021 19:47:03 +0300 Subject: sem 11 + split sem 10 --- Sem. 09/task2/Queue.hpp | 144 --------------------------------------------- Sem. 09/task2/main.cpp | 23 -------- Sem. 10/README.md | 0 Sem. 10/task1/Queue.hpp | 144 +++++++++++++++++++++++++++++++++++++++++++++ Sem. 10/task1/main.cpp | 23 ++++++++ Sem. 11/README.md | 0 Sem. 11/example/Person.cpp | 99 +++++++++++++++++++++++++++++++ Sem. 11/example/Person.h | 35 +++++++++++ Sem. 11/example/main.cpp | 17 ++++++ 9 files changed, 318 insertions(+), 167 deletions(-) delete mode 100644 Sem. 09/task2/Queue.hpp delete mode 100644 Sem. 09/task2/main.cpp create mode 100644 Sem. 10/README.md create mode 100644 Sem. 10/task1/Queue.hpp create mode 100644 Sem. 10/task1/main.cpp create mode 100644 Sem. 11/README.md create mode 100644 Sem. 11/example/Person.cpp create mode 100644 Sem. 11/example/Person.h create mode 100644 Sem. 11/example/main.cpp diff --git a/Sem. 09/task2/Queue.hpp b/Sem. 09/task2/Queue.hpp deleted file mode 100644 index 1f7f854..0000000 --- a/Sem. 09/task2/Queue.hpp +++ /dev/null @@ -1,144 +0,0 @@ -#include - -template -class Queue -{ - T* data; - size_t capacity; - size_t count; - - size_t head; //where to get - size_t tail; //last added element; - - void resize(size_t newCapacity); - - void copyFrom(const Queue& other); - void free(); -public: - Queue(); - - Queue(const Queue& other); - Queue& operator=(const Queue& other); - - void enqueue(const T& obj); - T dequeue(); - - size_t getCount(); - size_t isEmpty(); - - ~Queue(); -}; - -template -Queue::Queue() -{ - capacity = 4; - data = new T[capacity]; - - count = 0; - head = tail = 0; -} - - - -template -void Queue::enqueue(const T& obj) -{ - if (count == capacity) - resize(capacity * 2); - if (count != 0) - tail++; - - tail %= capacity; //so we don't get an out of range exception - - data[tail] = obj; - - count++; -} - -template -T Queue::dequeue() -{ - if (count == 0) - throw std::exception("The collection is empty!"); - - T toReturn = data[head]; - (++head) %= capacity; - - count--; - - return toReturn; -} - -template -size_t Queue::getCount() -{ - return count; -} - -template -size_t Queue::isEmpty() -{ - return getCount() == 0; -} - -template -void Queue::resize(size_t newCapacity) -{ - T* newData = new T[newCapacity]; - - for (int i = 0, ind = head; i < count; i++, (++ind) %= capacity) - newData[i] = data[ind]; - - delete[] data; - capacity = newCapacity; - data = newData; - - head = 0; - tail = count - 1; -} - -template -void Queue::copyFrom(const Queue& other) -{ - data = new T[other.capacity]; - for (int i = 0; i < other.capacity; i++) - data[i] = other.data[i]; - - head = other.head; - tail = other.tail; - - count = other.count; - capacity = other.capacity; - -} - -template -void Queue::free() -{ - delete[] data; -} - -template -Queue::Queue(const Queue& other) -{ - copyFrom(other); - -} - -template -Queue& Queue::operator=(const Queue& other) -{ - if (this != &other) - { - free(); - copyFrom(other); - } - return *this; -} - -template -Queue::~Queue() -{ - free(); -} diff --git a/Sem. 09/task2/main.cpp b/Sem. 09/task2/main.cpp deleted file mode 100644 index e52579a..0000000 --- a/Sem. 09/task2/main.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include "Queue.hpp" -using namespace std; - - - -int main() -{ - Queue q; - - q.enqueue('A'); - q.enqueue('B'); - q.enqueue('C'); - q.enqueue('D'); - cout << q.dequeue() << endl; - q.enqueue('E'); - q.enqueue('F'); - cout << q.dequeue() << endl; - cout << q.dequeue() << endl; - cout << q.dequeue() << endl; - cout << q.dequeue() << endl; - cout << q.dequeue() << endl; -} \ No newline at end of file diff --git a/Sem. 10/README.md b/Sem. 10/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Sem. 10/task1/Queue.hpp b/Sem. 10/task1/Queue.hpp new file mode 100644 index 0000000..1f7f854 --- /dev/null +++ b/Sem. 10/task1/Queue.hpp @@ -0,0 +1,144 @@ +#include + +template +class Queue +{ + T* data; + size_t capacity; + size_t count; + + size_t head; //where to get + size_t tail; //last added element; + + void resize(size_t newCapacity); + + void copyFrom(const Queue& other); + void free(); +public: + Queue(); + + Queue(const Queue& other); + Queue& operator=(const Queue& other); + + void enqueue(const T& obj); + T dequeue(); + + size_t getCount(); + size_t isEmpty(); + + ~Queue(); +}; + +template +Queue::Queue() +{ + capacity = 4; + data = new T[capacity]; + + count = 0; + head = tail = 0; +} + + + +template +void Queue::enqueue(const T& obj) +{ + if (count == capacity) + resize(capacity * 2); + if (count != 0) + tail++; + + tail %= capacity; //so we don't get an out of range exception + + data[tail] = obj; + + count++; +} + +template +T Queue::dequeue() +{ + if (count == 0) + throw std::exception("The collection is empty!"); + + T toReturn = data[head]; + (++head) %= capacity; + + count--; + + return toReturn; +} + +template +size_t Queue::getCount() +{ + return count; +} + +template +size_t Queue::isEmpty() +{ + return getCount() == 0; +} + +template +void Queue::resize(size_t newCapacity) +{ + T* newData = new T[newCapacity]; + + for (int i = 0, ind = head; i < count; i++, (++ind) %= capacity) + newData[i] = data[ind]; + + delete[] data; + capacity = newCapacity; + data = newData; + + head = 0; + tail = count - 1; +} + +template +void Queue::copyFrom(const Queue& other) +{ + data = new T[other.capacity]; + for (int i = 0; i < other.capacity; i++) + data[i] = other.data[i]; + + head = other.head; + tail = other.tail; + + count = other.count; + capacity = other.capacity; + +} + +template +void Queue::free() +{ + delete[] data; +} + +template +Queue::Queue(const Queue& other) +{ + copyFrom(other); + +} + +template +Queue& Queue::operator=(const Queue& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} + +template +Queue::~Queue() +{ + free(); +} diff --git a/Sem. 10/task1/main.cpp b/Sem. 10/task1/main.cpp new file mode 100644 index 0000000..e52579a --- /dev/null +++ b/Sem. 10/task1/main.cpp @@ -0,0 +1,23 @@ +#include +#include "Queue.hpp" +using namespace std; + + + +int main() +{ + Queue q; + + q.enqueue('A'); + q.enqueue('B'); + q.enqueue('C'); + q.enqueue('D'); + cout << q.dequeue() << endl; + q.enqueue('E'); + q.enqueue('F'); + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; + cout << q.dequeue() << endl; +} \ No newline at end of file diff --git a/Sem. 11/README.md b/Sem. 11/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Sem. 11/example/Person.cpp b/Sem. 11/example/Person.cpp new file mode 100644 index 0000000..ae78280 --- /dev/null +++ b/Sem. 11/example/Person.cpp @@ -0,0 +1,99 @@ +#include "Person.h" +#include + +using namespace std; + +Person::Person() : Person("Unknown", 0) +{ + +} +Person::Person(const char* name, int age) : name(nullptr), age(age) +{ + cout << "Constr" << endl; + setName(name); + setAge(age); +} + +Person::Person(const Person& other) +{ + cout << "copy constr" << endl; + copyFrom(other); +} +Person::Person(Person&& other) +{ + cout << "Move constr" << endl; + + name = other.name; + age = other.age; + + other.name = nullptr; +} + +Person& Person::operator=(Person&& other) +{ + + std::cout << "Move op= " << endl; + if (this != &other) + { + free(); + + name = other.name; + other.name = nullptr; + + age = other.age; + + } + return *this; +} + +Person& Person::operator = (const Person& other) +{ + cout << "op =" << endl; + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +Person::~Person() +{ + cout << "Destructor" << endl; + free(); +} + +const char* Person::getName() const +{ + return name; +} + +int Person::getAge() const +{ + return age; +} + +void Person::setName(const char* name) +{ + delete[] this->name; + this->name = new char[strlen(name) + 1]; + strcpy(this->name, name); +} +void Person::setAge(int age) +{ + if (age < 0 || age > 120) + age = 40; + this->age = age; +} + +void Person::copyFrom(const Person& other) +{ + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); + + age = other.age; +} +void Person::free() +{ + delete[] name; + age = 0; +} \ No newline at end of file diff --git a/Sem. 11/example/Person.h b/Sem. 11/example/Person.h new file mode 100644 index 0000000..9a404c4 --- /dev/null +++ b/Sem. 11/example/Person.h @@ -0,0 +1,35 @@ +#pragma once + +class Person +{ +public: + Person(); + Person(const char* name, int age); + + Person(const Person& other); + + Person(Person&& other); //move constr + + Person& operator=(Person&& other); // move op= + + Person& operator=(const Person& other); + ~Person(); + + void setName(const char* name); + void setAge(int age); + + const char* getName() const; + int getAge() const; + + void print(); + + +private: + + void copyFrom(const Person& other); + void free(); + + + char* name; + int age; +}; \ No newline at end of file diff --git a/Sem. 11/example/main.cpp b/Sem. 11/example/main.cpp new file mode 100644 index 0000000..a7a00f6 --- /dev/null +++ b/Sem. 11/example/main.cpp @@ -0,0 +1,17 @@ +#include +#include +#include "Person.h" +using namespace std; + +Person createPerson(const char* name, int age) +{ + Person p(name, age); + + return p; +} + +int main() +{ + Person p2("Petur",18); + p2 = createPerson("Ivan", 23); +} \ No newline at end of file -- cgit v1.3.1-23-gd82d