Website Summary Files Commits Branches @commit @diff @stats
path: root / Week04
diff options
context:
space:
mode:
Commite5c06fbc864205a9736f07f9237ace80617ca471 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-03-16 22:41:31 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-03-16 22:41:31 +0200
Filesf916efca600b104f3489a8501bffa448f11dfe79 /Week04
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.tar
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.tar.gz
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.zip
Parent5624cd9b63bec13fc464a3d7820d703abd8de113 (diff)
feat(Week04): Add solutions
7 files changed, 311 insertions, 0 deletions
-rw-r--r--Week04/Exercise01/Train.cpp24
-rw-r--r--Week04/Exercise01/Train.h107
-rw-r--r--Week04/Exercise02/IntArray.cpp27
-rw-r--r--Week04/Exercise02/IntArray.h44
-rw-r--r--Week04/Exercise03/String.cpp81
-rw-r--r--Week04/Exercise03/String.h25
-rw-r--r--Week04/README.md3
diff --git a/Week04/Exercise01/Train.cpp b/Week04/Exercise01/Train.cpp
new file mode 100644
index 0000000..5ff2db1
--- /dev/null
+++ b/Week04/Exercise01/Train.cpp
@@ -0,0 +1,24 @@
+#include "Train.h"
+#include <iostream>
+
+void Train::ArriveAtStation(unsigned passengersGettingOff, unsigned passengersGettingOn) {
+ if (passengersGettingOff > passengersOnBoard)
+ passengersOnBoard = 0;
+ else
+ passengersOnBoard -= passengersGettingOff;
+
+ passengersOnBoard += passengersGettingOn;
+ earnings += passengersGettingOn * ticketPrice;
+
+ passedStops++;
+ upcomingStops--;
+}
+
+void Train::Print() {
+ std::cout << "Carriages: " << carriages << ' '
+ << "Seats: " << passengersOnBoard << '/' << seatsAvailable << ' '
+ << "Stops: " << passedStops << " < " << upcomingStops << ' '
+ << "Ticket: " << ticketPrice << ' '
+ << "Earnings: " << earnings
+ << std::endl;
+}
diff --git a/Week04/Exercise01/Train.h b/Week04/Exercise01/Train.h
new file mode 100644
index 0000000..88322a2
--- /dev/null
+++ b/Week04/Exercise01/Train.h
@@ -0,0 +1,107 @@
+#ifndef _TRAIN
+#define _TRAIN
+
+class Train {
+ unsigned carriages;
+ unsigned seatsAvailable;
+ unsigned passengersOnBoard;
+ unsigned passedStops;
+ unsigned upcomingStops;
+ double ticketPrice;
+ double earnings;
+
+ // Излишно, но е добре да следваме "шаблона"
+ void free() {}
+ void copyFrom(const Train& other) {
+ carriages = other.carriages;
+ seatsAvailable = other.seatsAvailable;
+ passengersOnBoard = other.passengersOnBoard;
+ passedStops = other.passedStops;
+ upcomingStops = other.upcomingStops;
+ ticketPrice = other.ticketPrice;
+ earnings = other.earnings;
+ }
+
+public:
+ // "Шаблон" за голямата четворка
+ Train() {
+ carriages = seatsAvailable = passengersOnBoard = passedStops = upcomingStops = ticketPrice = earnings = 0;
+ }
+ ~Train() {
+ free();
+ }
+ Train(const Train& other) {
+ copyFrom(other);
+ }
+ Train& operator=(const Train& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ Train(unsigned carriages, unsigned seatsAvailable, unsigned upcomingStops, double ticketPrice) {
+ this->carriages = carriages;
+ this->seatsAvailable = seatsAvailable;
+ this->passengersOnBoard = 0;
+ this->passedStops = 0.0;
+ this->upcomingStops = upcomingStops;
+ this->ticketPrice = ticketPrice;
+ this->earnings = 0.0;
+ }
+
+ void ArriveAtStation(unsigned passengersGettingOff, unsigned passengersGettingOn);
+ void Print();
+
+ unsigned GetCarriages() {
+ return carriages;
+ }
+ void SetCarriages(unsigned carriages) {
+ this->carriages = carriages;
+ }
+
+ unsigned GetSeatsAvailable() {
+ return seatsAvailable;
+ }
+ void SetSeatsAvailable(unsigned seatsAvailable) {
+ this->seatsAvailable = seatsAvailable;
+ }
+
+ unsigned GetPassengersOnBoard() {
+ return passengersOnBoard;
+ }
+ void SetPassengersOnBoard(unsigned passengersOnBoard) {
+ this->passengersOnBoard = passengersOnBoard;
+ }
+
+ unsigned GetPassedStops() {
+ return passedStops;
+ }
+ void SetPassedStops(unsigned passedStops) {
+ this->passedStops = passedStops;
+ }
+
+ unsigned GetUpcomingStops() {
+ return upcomingStops;
+ }
+ void SetUpcomingStops(unsigned upcomingStops) {
+ this->upcomingStops = upcomingStops;
+ }
+
+ double GetTicketPrice() {
+ return ticketPrice;
+ }
+ void SetTicketPrice(double ticketPrice) {
+ this->ticketPrice = ticketPrice;
+ }
+
+ double GetEarnings() {
+ return earnings;
+ }
+ void SetEarnings(double earnings) {
+ this->earnings = earnings;
+ }
+};
+
+#endif /* _TRAIN */
diff --git a/Week04/Exercise02/IntArray.cpp b/Week04/Exercise02/IntArray.cpp
new file mode 100644
index 0000000..2dc0521
--- /dev/null
+++ b/Week04/Exercise02/IntArray.cpp
@@ -0,0 +1,27 @@
+#include "IntArray.h"
+
+int IntArray::At(unsigned index) {
+ if (index < capacity)
+ return elems[index];
+ return 0;
+}
+
+void IntArray::Resize(unsigned capacity) {
+ int* newElems = new int[capacity];
+
+ unsigned copyCount = this->capacity < capacity ? this->capacity : capacity;
+ for (unsigned i = 0; i < copyCount; ++i)
+ newElems[i] = elems[i];
+
+ delete[] elems;
+ elems = newElems;
+ this->capacity = capacity;
+}
+
+void IntArray::Concat(const IntArray& endElements) {
+ unsigned oldCapacity = capacity;
+ Resize(capacity + endElements.capacity);
+
+ for (unsigned i = oldCapacity; i < capacity; ++i)
+ elems[i] = endElements.elems[i - oldCapacity];
+}
diff --git a/Week04/Exercise02/IntArray.h b/Week04/Exercise02/IntArray.h
new file mode 100644
index 0000000..0ed99c9
--- /dev/null
+++ b/Week04/Exercise02/IntArray.h
@@ -0,0 +1,44 @@
+#ifndef _INTARRAY
+#define _INTARRAY
+
+class IntArray {
+ int* elems;
+ unsigned capacity;
+
+ /* Как трябва да изглежда голяма четворка: */
+
+ void free() {
+ delete[] elems;
+ }
+ void copyFrom(const IntArray& other) {
+ capacity = other.capacity;
+
+ elems = new int[capacity];
+ for (unsigned i = 0; i < capacity; ++i)
+ elems[i] = other.elems[i];
+ }
+public:
+ IntArray() {
+ elems = nullptr;
+ capacity = 0;
+ }
+ ~IntArray() {
+ free();
+ }
+ IntArray(const IntArray& other) {
+ copyFrom(other);
+ }
+ IntArray& operator=(const IntArray& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ int At(unsigned index);
+ void Resize(unsigned capacity);
+ void Concat(const IntArray& endElements);
+};
+
+#endif /* _INTARRAY */
diff --git a/Week04/Exercise03/String.cpp b/Week04/Exercise03/String.cpp
new file mode 100644
index 0000000..4fe49f4
--- /dev/null
+++ b/Week04/Exercise03/String.cpp
@@ -0,0 +1,81 @@
+#include "String.h"
+#include "string.h"
+
+void String::free() {
+ delete[] str;
+}
+void String::copyFrom(const String& other) {
+ length = other.length;
+
+ // Не забравяме за терминиращата нула!
+ str = new char[length + 1];
+ for (unsigned i = 0; i <= length; ++i)
+ str[i] = other.str[i];
+}
+
+String::String() {
+ length = 0;
+ str = new char[1];
+ str[0] = '\0';
+}
+
+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(const char* str) {
+ length = strlen(str);
+
+ this->str = new char[length + 1];
+ for (unsigned i = 0; i <= length; ++i)
+ this->str[i] = str[i];
+}
+
+unsigned String::GetLength() {
+ return length;
+}
+
+const char* String::GetStr() {
+ return str;
+}
+
+String String::Substring(unsigned startIndex, unsigned endIndex) {
+ if (endIndex >= length)
+ endIndex = length - 1;
+
+ String ret;
+ // Не забравяме за терминиращата нула!
+ ret.length = endIndex - startIndex + 2;
+
+ ret.str = new char[ret.length];
+ for (unsigned i = 0; i <= ret.length; ++i)
+ ret.str[i] = this->str[i + startIndex];
+
+ return ret;
+}
+
+String String::Concat(const String& rightHalf) {
+ String ret;
+ // Не забравяме за терминиращата нула!
+ ret.length = length + rightHalf.length + 1;
+
+ ret.str = new char[ret.length];
+ for (unsigned i = 0; i < length; ++i)
+ ret.str[i] = this->str[i];
+ for (unsigned i = 0; i <= rightHalf.length; ++i)
+ ret.str[i + length] = this->str[i];
+
+ return ret;
+}
diff --git a/Week04/Exercise03/String.h b/Week04/Exercise03/String.h
new file mode 100644
index 0000000..e7d1983
--- /dev/null
+++ b/Week04/Exercise03/String.h
@@ -0,0 +1,25 @@
+#ifndef _STRING
+#define _STRING
+
+class String {
+ char* str;
+ unsigned length;
+
+ void free();
+ void copyFrom(const String& other);
+public:
+ String();
+ ~String();
+ String(const String& other);
+ String& operator=(const String& other);
+
+ String(const char* str);
+
+ unsigned GetLength();
+ const char* GetStr();
+
+ String Substring(unsigned startIndex, unsigned endIndex);
+ String Concat(const String& rightHalf);
+};
+
+#endif /* _STRING */
diff --git a/Week04/README.md b/Week04/README.md
new file mode 100644
index 0000000..2627dfb
--- /dev/null
+++ b/Week04/README.md
@@ -0,0 +1,3 @@
+# Задачи
+
+<https://syndamia.com/teaching/oop-2025/week04/>