aboutsummaryrefslogtreecommitdiff
path: root/week04/Exercise5.cpp
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/Exercise5.cpp
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/Exercise5.cpp')
-rw-r--r--week04/Exercise5.cpp83
1 files changed, 83 insertions, 0 deletions
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));
+}