From 9c11fababb9b6b194e646fbeb62d141aacf74c43 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 25 May 2024 16:08:17 +0300 Subject: [w13] Added solutions --- week13/Exercise1/IndexArray.cpp | 51 +++++++++++++++++++++ week13/Exercise1/IndexArray.h | 18 ++++++++ week13/Exercise1/String.cpp | 51 +++++++++++++++++++++ week13/Exercise1/String.h | 18 ++++++++ week13/Exercise1/WordString.cpp | 25 +++++++++++ week13/Exercise1/WordString.h | 8 ++++ week13/Exercise2/Container.hpp | 75 +++++++++++++++++++++++++++++++ week13/Exercise2/Indexable.hpp | 25 +++++++++++ week13/Exercise2/Resizeable.hpp | 34 ++++++++++++++ week13/Exercise2/Vector.hpp | 35 +++++++++++++++ week13/Exercise3/Administrator.h | 6 +++ week13/Exercise3/Administrator.h.gch | Bin 0 -> 2140594 bytes week13/Exercise3/Thread.cpp | 85 +++++++++++++++++++++++++++++++++++ week13/Exercise3/Thread.h | 25 +++++++++++ week13/Exercise3/ThreadModerator.cpp | 24 ++++++++++ week13/Exercise3/ThreadModerator.h | 9 ++++ week13/Exercise3/User.cpp | 61 +++++++++++++++++++++++++ week13/Exercise3/User.h | 24 ++++++++++ week13/Exercise3/UserModerator.cpp | 9 ++++ week13/Exercise3/UserModerator.h | 8 ++++ 20 files changed, 591 insertions(+) create mode 100644 week13/Exercise1/IndexArray.cpp create mode 100644 week13/Exercise1/IndexArray.h create mode 100644 week13/Exercise1/String.cpp create mode 100644 week13/Exercise1/String.h create mode 100644 week13/Exercise1/WordString.cpp create mode 100644 week13/Exercise1/WordString.h create mode 100644 week13/Exercise2/Container.hpp create mode 100644 week13/Exercise2/Indexable.hpp create mode 100644 week13/Exercise2/Resizeable.hpp create mode 100644 week13/Exercise2/Vector.hpp create mode 100644 week13/Exercise3/Administrator.h create mode 100644 week13/Exercise3/Administrator.h.gch create mode 100644 week13/Exercise3/Thread.cpp create mode 100644 week13/Exercise3/Thread.h create mode 100644 week13/Exercise3/ThreadModerator.cpp create mode 100644 week13/Exercise3/ThreadModerator.h create mode 100644 week13/Exercise3/User.cpp create mode 100644 week13/Exercise3/User.h create mode 100644 week13/Exercise3/UserModerator.cpp create mode 100644 week13/Exercise3/UserModerator.h (limited to 'week13') diff --git a/week13/Exercise1/IndexArray.cpp b/week13/Exercise1/IndexArray.cpp new file mode 100644 index 0000000..ed7a212 --- /dev/null +++ b/week13/Exercise1/IndexArray.cpp @@ -0,0 +1,51 @@ +#include "IndexArray.h" +#include + +void IndexArray::free() { + delete[] indecies; +} + +void IndexArray::copyFrom(const IndexArray& other) { + this->size = other.size; + this->indecies = new int[size]; + for (int i = 0; i < size; i++) { + this->indecies[i] = other.indecies[i]; + } +} + +IndexArray::IndexArray() { + this->indecies = nullptr; + this->size = 0; +} + +IndexArray::~IndexArray() { + free(); +} + +IndexArray::IndexArray(const IndexArray& other) { + copyFrom(other); +} + +IndexArray& IndexArray::operator=(const IndexArray& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +IndexArray::IndexArray(IndexArray&& other) { + this->indecies = other.indecies; + other.indecies = nullptr; +} + +IndexArray& IndexArray::operator=(IndexArray&& other) { + if (this != &other) { + free(); + + this->indecies = other.indecies; + other.indecies = nullptr; + } + return *this; +} + diff --git a/week13/Exercise1/IndexArray.h b/week13/Exercise1/IndexArray.h new file mode 100644 index 0000000..90c948e --- /dev/null +++ b/week13/Exercise1/IndexArray.h @@ -0,0 +1,18 @@ +#pragma once + +class IndexArray { + void free(); + void copyFrom(const IndexArray& other); + +protected: + int *indecies; + unsigned size; + +public: + IndexArray(); + virtual ~IndexArray(); + IndexArray(const IndexArray& other); + IndexArray& operator=(const IndexArray& other); + IndexArray(IndexArray&& other); + IndexArray& operator=(IndexArray&& other); +}; diff --git a/week13/Exercise1/String.cpp b/week13/Exercise1/String.cpp new file mode 100644 index 0000000..2b41e96 --- /dev/null +++ b/week13/Exercise1/String.cpp @@ -0,0 +1,51 @@ +#include "String.h" +#include + +void String::free() { + delete[] str; +} + +void String::copyFrom(const String& other) { + this->str = new char[strlen(other.str) + 1]; + strcpy(this->str, other.str); +} + +String::String() { + this->str = nullptr; +} + +String::~String() { + free(); +} + +String::String(const String& other) { + copyFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +String::String(String&& other) { + this->str = other.str; + other.str = nullptr; +} + +String& String::operator=(String&& other) { + if (this != &other) { + free(); + + this->str = other.str; + other.str = nullptr; + } + return *this; +} + +String::String(const char* str) { + this->str = new char[strlen(str) + 1]; + strcpy(this->str, str); +} diff --git a/week13/Exercise1/String.h b/week13/Exercise1/String.h new file mode 100644 index 0000000..b2f0ee9 --- /dev/null +++ b/week13/Exercise1/String.h @@ -0,0 +1,18 @@ +#pragma once + +class String { + char *str; + + void free(); + void copyFrom(const String& other); + +public: + String(); + virtual ~String(); + String(const String& other); + String& operator=(const String& other); + String(String&& other); + String& operator=(String&& other); + + String(const char* str); +}; diff --git a/week13/Exercise1/WordString.cpp b/week13/Exercise1/WordString.cpp new file mode 100644 index 0000000..4ba3cca --- /dev/null +++ b/week13/Exercise1/WordString.cpp @@ -0,0 +1,25 @@ +#include "WordString.h" + +bool isBlank(char ch) { + return ch == ' ' || ch == '\n' || ch == '\t'; +} + +WordString::WordString(const char* str) : String(str), IndexArray() { + this->size = 1; + for (int i = 1; str[i] != '\0'; i++) { + if (isBlank(str[i - 1]) && !isBlank(str[i])) { + this->size++; + } + } + + this->indecies = new int[this->size]; + + this->indecies[0] = 0; + unsigned indeciesI = 1; + + for (int i = 1; str[i] != '\0'; i++) { + if (isBlank(str[i - 1]) && !isBlank(str[i])) { + indecies[indeciesI++] = i; + } + } +} diff --git a/week13/Exercise1/WordString.h b/week13/Exercise1/WordString.h new file mode 100644 index 0000000..fc91e3d --- /dev/null +++ b/week13/Exercise1/WordString.h @@ -0,0 +1,8 @@ +#pragma once +#include "String.h" +#include "IndexArray.h" + +class WordString : public String, public IndexArray { +public: + WordString(const char* str); +}; diff --git a/week13/Exercise2/Container.hpp b/week13/Exercise2/Container.hpp new file mode 100644 index 0000000..ace942f --- /dev/null +++ b/week13/Exercise2/Container.hpp @@ -0,0 +1,75 @@ +#pragma once + +template +class Container { + void free(); + void copyFrom(const Container& other); + +protected: + T* arr; + unsigned size; + +public: + Container(); + ~Container(); + Container(const Container& other); + Container& operator=(const Container& other); + Container(Container&& other); + Container& operator=(Container&& other); +}; + +template +void Container::free() { + delete[] arr; +} + +template +void Container::copyFrom(const Container& other) { + this->size = other.size; + this->arr = new T[size]; + for (int i = 0; i < size; i++) { + this->arr[i] = other.arr[i]; + } +} + +template +Container::Container() { + this->arr = nullptr; + size = 0; +} + +template +Container::~Container() { + free(); +} + +template +Container::Container(const Container& other) { + copyFrom(other); +} + +template +Container& Container::operator=(const Container& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template +Container::Container(Container&& other) { + this->arr = other.arr; + other.arr = nullptr; +} + +template +Container& Container::operator=(Container&& other) { + if (this != &other) { + free(); + + this->arr = other.arr; + other.arr = nullptr; + } + return *this; +} diff --git a/week13/Exercise2/Indexable.hpp b/week13/Exercise2/Indexable.hpp new file mode 100644 index 0000000..dd5fb6b --- /dev/null +++ b/week13/Exercise2/Indexable.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "Container.hpp" + +template +class Indexable : virtual public Container { +public: + T& operator[](unsigned index); + const T& operator[](unsigned index) const; +}; + +template +T& Indexable::operator[](unsigned index) { + if (index > this->size) { + throw "Index out of range!"; + } + return this->arr[index]; +} + +template +const T& Indexable::operator[](unsigned index) const { + if (index > this->size) { + throw "Index out of range!"; + } + return this->arr[index]; +} diff --git a/week13/Exercise2/Resizeable.hpp b/week13/Exercise2/Resizeable.hpp new file mode 100644 index 0000000..9b4fe04 --- /dev/null +++ b/week13/Exercise2/Resizeable.hpp @@ -0,0 +1,34 @@ +#pragma once +#include "Container.hpp" + +template +class Resizeable : virtual public Container { + void resize(); + +public: + void InsertAt(const T& element, unsigned index); +}; + +template +void Resizeable::resize() { + this->size++; + T* biggerArr = new T[this->size]; + for (int i = 0; i < this->size - 1; i++) { + biggerArr[i] = this->arr[i]; + } + delete[] this->arr; + this->arr = biggerArr; +} + +template +void Resizeable::InsertAt(const T& element, unsigned index) { + if (index >= this->size) { + throw "Index out of range!"; + } + + resize(); + for (int i = this->size - 1; i > index; i++) { + this->arr[i] = this->arr[i-1]; + } + this->arr[index] = element; +} diff --git a/week13/Exercise2/Vector.hpp b/week13/Exercise2/Vector.hpp new file mode 100644 index 0000000..ba66049 --- /dev/null +++ b/week13/Exercise2/Vector.hpp @@ -0,0 +1,35 @@ +#pragma once +#include "Indexable.hpp" +#include "Resizeable.hpp" + +template +class Vector : public Indexable, public Resizeable { +public: + void pop_back(); + void pop_front(); + void push_back(const T& element); + void push_front(const T& element); +}; + +template +void Vector::pop_back() { + this->size--; +} + +template +void Vector::pop_front() { + for (int i = 0; i < this->size - 1; i++) { + this->arr[i] = this->arr[i+1]; + } + this->size--; +} + +template +void Vector::push_back(const T& element) { + this->InsertAt(this->size - 1, element); +} + +template +void Vector::push_front(const T& element) { + this->InsertAt(0, element); +} diff --git a/week13/Exercise3/Administrator.h b/week13/Exercise3/Administrator.h new file mode 100644 index 0000000..ae34759 --- /dev/null +++ b/week13/Exercise3/Administrator.h @@ -0,0 +1,6 @@ +#pragma once +#include "UserModerator.h" +#include "ThreadModerator.h" + +class Administrator : public UserModerator, public ThreadModerator { +}; diff --git a/week13/Exercise3/Administrator.h.gch b/week13/Exercise3/Administrator.h.gch new file mode 100644 index 0000000..295e5f4 Binary files /dev/null and b/week13/Exercise3/Administrator.h.gch differ diff --git a/week13/Exercise3/Thread.cpp b/week13/Exercise3/Thread.cpp new file mode 100644 index 0000000..6cd13ba --- /dev/null +++ b/week13/Exercise3/Thread.cpp @@ -0,0 +1,85 @@ +#include "Thread.h" +#include + +void Thread::resize() { + this->allocated *= 2; + char** moreMessages = new char*[this->allocated]; + for (int i = 0; i < this->size; i++) { + moreMessages[i] = this->messages[i]; + } + delete[] this->messages; + this->messages = moreMessages; +} + +void Thread::free() { + for (int i = 0; i < size; i++) { + delete[] messages[i]; + } + delete[] messages; +} + +void Thread::copyFrom(const Thread& other) { + this->size = other.size; + this->allocated = other.allocated; + this->messages = new char*[allocated]; + for (int i = 0; i < size; i++) { + this->messages[i] = new char[strlen(other.messages[i] + 1)]; + strcpy(this->messages[i], other.messages[i]); + } +} + +Thread::Thread() { + this->messages = nullptr; + this->size = this->allocated = 0; +} + +Thread::~Thread() { + free(); +} + +Thread::Thread(const Thread& other) { + copyFrom(other); +} + +Thread& Thread::operator=(const Thread& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Thread::Thread(Thread&& other) { + this->size = other.size; + this->allocated = other.allocated; + this->messages = other.messages; + other.messages = nullptr; +} + +Thread& Thread::operator=(Thread&& other) { + if (this != &other) { + free(); + + this->size = other.size; + this->allocated = other.allocated; + this->messages = other.messages; + other.messages = nullptr; + } + return *this; +} + +void Thread::PostMessage(const User& poster, const char* message) { + if (poster.IsBanned()) { + throw "User cannot post!"; + } + if (this->size == this->allocated) { + resize(); + } + + char* newMessage = new char[strlen(poster.GetUsername()) + 1 + strlen(message) + 1]; + strcpy(newMessage, poster.GetUsername()); + strcat(newMessage, " "); // За да знаем къде свършва потребителското име + strcat(newMessage, message); + + this->messages[this->size++] = newMessage; +} diff --git a/week13/Exercise3/Thread.h b/week13/Exercise3/Thread.h new file mode 100644 index 0000000..3b0a588 --- /dev/null +++ b/week13/Exercise3/Thread.h @@ -0,0 +1,25 @@ +#pragma once +#include "User.h" + +class Thread { + char** messages; + unsigned size; + unsigned allocated; + + void resize(); + + void free(); + void copyFrom(const Thread& other); + +public: + Thread(); + virtual ~Thread(); + Thread(const Thread& other); + Thread& operator=(const Thread& other); + Thread(Thread&& other); + Thread& operator=(Thread&& other); + + void PostMessage(const User& poster, const char* message); + + friend class ThreadModerator; +}; diff --git a/week13/Exercise3/ThreadModerator.cpp b/week13/Exercise3/ThreadModerator.cpp new file mode 100644 index 0000000..047b0ec --- /dev/null +++ b/week13/Exercise3/ThreadModerator.cpp @@ -0,0 +1,24 @@ +#include "ThreadModerator.h" +#include + +void ThreadModerator::RemoveMessage(Thread& thread, unsigned index) { + if (index >= thread.size) { + throw "Index out of range!"; + } + + delete[] thread.messages[index]; + for (unsigned i = index; i < thread.size - 1; i++) { + thread.messages[i] = thread.messages[i-1]; + } + thread.size--; +} + +void ThreadModerator::RemoveByUser(Thread& thread, const User& poster) { + unsigned nameLen = strlen(poster.GetUsername()); + for (unsigned i = 0; i < thread.size; i++) { + if (strncmp(thread.messages[i], poster.GetUsername(), nameLen) == 0) { + RemoveMessage(thread, i); + i--; + } + } +} diff --git a/week13/Exercise3/ThreadModerator.h b/week13/Exercise3/ThreadModerator.h new file mode 100644 index 0000000..2872206 --- /dev/null +++ b/week13/Exercise3/ThreadModerator.h @@ -0,0 +1,9 @@ +#pragma once +#include "User.h" +#include "Thread.h" + +class ThreadModerator : virtual public User { +public: + void RemoveMessage(Thread& thread, unsigned index); + void RemoveByUser(Thread& thread, const User& poster); +}; diff --git a/week13/Exercise3/User.cpp b/week13/Exercise3/User.cpp new file mode 100644 index 0000000..e87a665 --- /dev/null +++ b/week13/Exercise3/User.cpp @@ -0,0 +1,61 @@ +#include "User.h" +#include + +void User::free() { + delete[] name; + delete[] password; +} + +void User::copyFrom(const User& other) { + this->name = new char[strlen(other.name) + 1]; + strcpy(this->name, other.name); + this->password = new char[strlen(other.password) + 1]; + strcpy(this->password, other.password); +} + +User::User() { + this->name = this->password = nullptr; +} + +User::~User() { + free(); +} + +User::User(const User& other) { + copyFrom(other); +} + +User& User::operator=(const User& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +User::User(User&& other) { + this->name = other.name; + other.name = nullptr; + this->password = other.password; + other.password = nullptr; +} + +User& User::operator=(User&& other) { + if (this != &other) { + free(); + + this->name = other.name; + other.name = nullptr; + this->password = other.password; + other.password = nullptr; + } + return *this; +} + +bool User::IsBanned() const { + return banned; +} + +const char* User::GetUsername() const { + return name; +} diff --git a/week13/Exercise3/User.h b/week13/Exercise3/User.h new file mode 100644 index 0000000..e742181 --- /dev/null +++ b/week13/Exercise3/User.h @@ -0,0 +1,24 @@ +#pragma once + +class User { + void free(); + void copyFrom(const User& other); + +protected: + char *name; + char *password; + bool banned; + +public: + User(); + virtual ~User(); + User(const User& other); + User& operator=(const User& other); + User(User&& other); + User& operator=(User&& other); + + bool IsBanned() const; + const char* GetUsername() const; + + friend class UserModerator; +}; diff --git a/week13/Exercise3/UserModerator.cpp b/week13/Exercise3/UserModerator.cpp new file mode 100644 index 0000000..51f10e4 --- /dev/null +++ b/week13/Exercise3/UserModerator.cpp @@ -0,0 +1,9 @@ +#include "UserModerator.h" + +void UserModerator::BanUser(User& user) { + user.banned = true; +} + +void UserModerator::UnbanUser(User& user) { + user.banned = false; +} diff --git a/week13/Exercise3/UserModerator.h b/week13/Exercise3/UserModerator.h new file mode 100644 index 0000000..dfed054 --- /dev/null +++ b/week13/Exercise3/UserModerator.h @@ -0,0 +1,8 @@ +#pragma once +#include "User.h" + +class UserModerator : virtual public User { +public: + void BanUser(User& user); + void UnbanUser(User& user); +}; -- cgit v1.2.3