aboutsummaryrefslogtreecommitdiff
path: root/week04
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-03-20 13:20:09 +0200
committerSyndamia <kamen@syndamia.com>2024-03-20 13:20:09 +0200
commitcdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2 (patch)
treec3dca402c9203b0e06b06574bd6459b3b58d5fbd /week04
parent6b7a20b652bd3388ceaac60c96479419ac2c4859 (diff)
downloadoop-2023-solutions-cdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2.tar
oop-2023-solutions-cdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2.tar.gz
oop-2023-solutions-cdd5b6c28b12a4763c9b1eef9ba45ca85a6ddaa2.zip
[w4] Solved exercises 1-5
Diffstat (limited to 'week04')
-rw-r--r--week04/Exercise1.cpp50
-rw-r--r--week04/Exercise2.cpp20
-rw-r--r--week04/Exercise3.cpp43
-rw-r--r--week04/Exercise4.cpp47
-rw-r--r--week04/Exercise5.cpp83
5 files changed, 243 insertions, 0 deletions
diff --git a/week04/Exercise1.cpp b/week04/Exercise1.cpp
new file mode 100644
index 0000000..09e1286
--- /dev/null
+++ b/week04/Exercise1.cpp
@@ -0,0 +1,50 @@
+#include <cstring>
+
+struct Smartphone {
+private:
+ char brand[129];
+ char model[513];
+ unsigned manufacturingYear;
+ float cameraResolution;
+
+public:
+ const char* GetBrand() {
+ return brand;
+ }
+ void SetBrand(const char* newValue) {
+ if (newValue[0] == '\0' || strlen(newValue) > 128) {
+ throw "Brand cannot be empty string or longer than 128 characters!";
+ }
+ strcpy(brand, newValue);
+ }
+
+ const char* GetModel() {
+ return model;
+ }
+ void SetModel(const char* newValue) {
+ if (newValue[0] == '\0' || strlen(newValue) > 512) {
+ throw "Model cannot be empty string or longer than 512 characters!";
+ }
+ strcpy(model, newValue);
+ }
+
+ unsigned GetManufacturingYear() {
+ return manufacturingYear;
+ }
+ void SetManufacturingYear(unsigned newValue) {
+ if (newValue < 2000 || 2024 < newValue) {
+ throw "Manufacturing year cannot be before 2000 or after 2024!";
+ }
+ manufacturingYear = newValue;
+ }
+
+ float GetCameraResolution() {
+ return cameraResolution;
+ }
+ void SetCameraResolution(float newValue) {
+ if (newValue < 0.0) {
+ throw "Camera resolution cannot be negative!";
+ }
+ cameraResolution = newValue;
+ }
+};
diff --git a/week04/Exercise2.cpp b/week04/Exercise2.cpp
new file mode 100644
index 0000000..4805fdf
--- /dev/null
+++ b/week04/Exercise2.cpp
@@ -0,0 +1,20 @@
+struct Bee {
+ float age;
+ char type;
+};
+
+struct BeeHive {
+private:
+ Bee* roaming;
+ Bee* eggs;
+
+public:
+ BeeHive(int roamingCapacity, int eggCapacity) {
+ roaming = new Bee[roamingCapacity];
+ eggs = new Bee[eggCapacity];
+ }
+ ~BeeHive() {
+ delete[] roaming;
+ delete[] eggs;
+ }
+};
diff --git a/week04/Exercise3.cpp b/week04/Exercise3.cpp
new file mode 100644
index 0000000..c834097
--- /dev/null
+++ b/week04/Exercise3.cpp
@@ -0,0 +1,43 @@
+#include <iostream>
+#include <cstring>
+
+struct Paper {
+private:
+ char contents[1024];
+ unsigned page;
+
+ void copyFrom(const Paper& other) {
+ strncpy(this->contents, other.contents, 1024);
+ page = other.page + 1;
+ }
+
+public:
+ Paper(const char* contents) {
+ strncpy(this->contents, contents, 1024);
+ page = 1;
+ }
+
+ Paper(const Paper& other) {
+ copyFrom(other);
+ }
+};
+
+int main() {
+ int N;
+ std::cin >> N;
+
+ Paper** papers = new Paper*[N];
+
+ char buffer[1024];
+ std::cin.ignore();
+ std::cin.getline(buffer, 1024);
+ papers[0] = new Paper(buffer);
+ for (int i = 1; i < N; i++) {
+ papers[i] = new Paper(*papers[0]);
+ }
+
+ for (int i = 0; i < N; i++) {
+ delete papers[i];
+ }
+ delete[] papers;
+}
diff --git a/week04/Exercise4.cpp b/week04/Exercise4.cpp
new file mode 100644
index 0000000..3716287
--- /dev/null
+++ b/week04/Exercise4.cpp
@@ -0,0 +1,47 @@
+#include <iostream>
+#include <cstring>
+
+struct DynamicString {
+private:
+ char* str;
+ int size;
+
+ void copyFrom(const DynamicString& other) {
+ this->str = new char[other.size + 1];
+ strncpy(this->str, other.str, other.size + 1);
+ this->size = other.size;
+ }
+
+ void free() {
+ delete[] str;
+ }
+
+public:
+ DynamicString(const char* str) {
+ size = strlen(str);
+ this->str = new char[size + 1];
+ strncpy(this->str, str, size + 1);
+ }
+ ~DynamicString() {
+ free();
+ }
+
+ DynamicString& operator=(const DynamicString& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ const char* GetStr() {
+ return str;
+ }
+};
+
+int main() {
+ DynamicString s1("Hello");
+ DynamicString s2("World");
+ s1 = s2;
+ std::cout << s1.GetStr() << std::endl;
+}
diff --git a/week04/Exercise5.cpp b/week04/Exercise5.cpp
new file mode 100644
index 0000000..3dea0c6
--- /dev/null
+++ b/week04/Exercise5.cpp
@@ -0,0 +1,83 @@
+#include <cstring>
+
+struct TVProgram {
+private:
+ char name[1024];
+ unsigned beginTime;
+ unsigned endTime;
+
+ void copyFrom(const TVProgram& other) {
+ strncpy(this->name, other.name, 1024);
+ this->beginTime = other.beginTime;
+ this->endTime = other.endTime;
+ }
+
+public:
+ TVProgram() {
+ name[0] = '\0';
+ this->beginTime = this->endTime = 0;
+ }
+
+ TVProgram(const char* name, unsigned beginTime, unsigned endTime) {
+ strncpy(this->name, name, 1024);
+ this->beginTime = beginTime;
+ this->endTime = endTime;
+ }
+
+ TVProgram(const TVProgram& other) {
+ copyFrom(other);
+ }
+
+ TVProgram& operator=(const TVProgram& other) {
+ if (this != &other) {
+ copyFrom(other);
+ }
+ return *this;
+ }
+};
+
+struct TVChannel {
+private:
+ TVProgram* programs;
+ unsigned size;
+ unsigned lastFreeIndex;
+
+ void free() {
+ delete[] programs;
+ }
+
+ void resize() {
+ TVProgram* biggerArray = new TVProgram[size * 2];
+ for (int i = 0; i < size; i++) {
+ biggerArray[i] = programs[i]; // По копие, извиква се оператор=
+ }
+ delete[] programs;
+ programs = biggerArray;
+ size *= 2;
+ }
+
+public:
+ TVChannel() {
+ size = 1;
+ programs = new TVProgram[size];
+ lastFreeIndex = 0;
+ }
+ ~TVChannel() {
+ free();
+ }
+
+ void AddChannel(TVProgram program) { // По копие, извиква се копиращ конструктор
+ if (lastFreeIndex == size) {
+ resize();
+ }
+
+ programs[lastFreeIndex++] = program; // По копие, извиква се оператор=
+ }
+};
+
+int main() {
+ TVChannel fmitv;
+ fmitv.AddChannel(TVProgram("Object-oriented programming", 900, 1200));
+ fmitv.AddChannel(TVProgram("Geometry", 1315, 1600));
+ fmitv.AddChannel(TVProgram("English", 1600, 1755));
+}