aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--week13/Exercise1/IndexArray.cpp51
-rw-r--r--week13/Exercise1/IndexArray.h18
-rw-r--r--week13/Exercise1/String.cpp51
-rw-r--r--week13/Exercise1/String.h18
-rw-r--r--week13/Exercise1/WordString.cpp25
-rw-r--r--week13/Exercise1/WordString.h8
-rw-r--r--week13/Exercise2/Container.hpp75
-rw-r--r--week13/Exercise2/Indexable.hpp25
-rw-r--r--week13/Exercise2/Resizeable.hpp34
-rw-r--r--week13/Exercise2/Vector.hpp35
-rw-r--r--week13/Exercise3/Administrator.h6
-rw-r--r--week13/Exercise3/Administrator.h.gchbin0 -> 2140594 bytes
-rw-r--r--week13/Exercise3/Thread.cpp85
-rw-r--r--week13/Exercise3/Thread.h25
-rw-r--r--week13/Exercise3/ThreadModerator.cpp24
-rw-r--r--week13/Exercise3/ThreadModerator.h9
-rw-r--r--week13/Exercise3/User.cpp61
-rw-r--r--week13/Exercise3/User.h24
-rw-r--r--week13/Exercise3/UserModerator.cpp9
-rw-r--r--week13/Exercise3/UserModerator.h8
20 files changed, 591 insertions, 0 deletions
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 <cstring>
+
+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 <cstring>
+
+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 T>
+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 <class T>
+void Container<T>::free() {
+ delete[] arr;
+}
+
+template <class T>
+void Container<T>::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 <class T>
+Container<T>::Container() {
+ this->arr = nullptr;
+ size = 0;
+}
+
+template <class T>
+Container<T>::~Container() {
+ free();
+}
+
+template <class T>
+Container<T>::Container(const Container<T>& other) {
+ copyFrom(other);
+}
+
+template <class T>
+Container<T>& Container<T>::operator=(const Container<T>& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <class T>
+Container<T>::Container(Container<T>&& other) {
+ this->arr = other.arr;
+ other.arr = nullptr;
+}
+
+template <class T>
+Container<T>& Container<T>::operator=(Container<T>&& 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 T>
+class Indexable : virtual public Container<T> {
+public:
+ T& operator[](unsigned index);
+ const T& operator[](unsigned index) const;
+};
+
+template <class T>
+T& Indexable<T>::operator[](unsigned index) {
+ if (index > this->size) {
+ throw "Index out of range!";
+ }
+ return this->arr[index];
+}
+
+template <class T>
+const T& Indexable<T>::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 T>
+class Resizeable : virtual public Container<T> {
+ void resize();
+
+public:
+ void InsertAt(const T& element, unsigned index);
+};
+
+template <class T>
+void Resizeable<T>::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 <class T>
+void Resizeable<T>::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 T>
+class Vector : public Indexable<T>, public Resizeable<T> {
+public:
+ void pop_back();
+ void pop_front();
+ void push_back(const T& element);
+ void push_front(const T& element);
+};
+
+template <class T>
+void Vector<T>::pop_back() {
+ this->size--;
+}
+
+template <class T>
+void Vector<T>::pop_front() {
+ for (int i = 0; i < this->size - 1; i++) {
+ this->arr[i] = this->arr[i+1];
+ }
+ this->size--;
+}
+
+template <class T>
+void Vector<T>::push_back(const T& element) {
+ this->InsertAt(this->size - 1, element);
+}
+
+template <class T>
+void Vector<T>::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
--- /dev/null
+++ b/week13/Exercise3/Administrator.h.gch
Binary files 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 <cstring>
+
+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 <cstring>
+
+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 <cstring>
+
+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);
+};