Website Summary Files Commits Branches @commit @diff @stats
path: root / Week02
diff options
context:
space:
mode:
Commitf18c44909c604a01bb0265d006a84223ee88d603 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-03-01 18:33:54 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-03-01 18:33:54 +0200
Files513a5b000add253f698901c7d636ed921f6913f2 /Week02
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.tar
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.tar.gz
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.zip
Parentfcf38e62e8160f2c9901bdfd33760afed28beb8d (diff)
feat(Week02): Add solutions
diff --git a/Week02/Exercise01/date.cpp b/Week02/Exercise01/date.cpp
new file mode 100644
index 0000000..ffc051e
--- /dev/null
+++ b/Week02/Exercise01/date.cpp
@@ -0,0 +1,31 @@
+#include <iostream>
+#include "date.h"
+
+const char* monthName(unsigned month) {
+ switch (month) {
+ case 1: return "Jan";
+ case 2: return "Feb";
+ case 3: return "Mar";
+ case 4: return "Apr";
+ case 5: return "May";
+ case 6: return "Jun";
+ case 7: return "Jul";
+ case 8: return "Aug";
+ case 9: return "Sep";
+ case 10: return "Oct";
+ case 11: return "Nov";
+ case 12: return "Dec";
+
+ default: return nullptr;
+ }
+}
+
+void printDate(const Date& d) {
+ std::cout << d.day << '/' << monthName(d.month) << '/' << d.year;
+}
+
+Date readDate() {
+ Date d;
+ std::cin >> d.day >> d.month >> d.year;
+ return d;
+}
diff --git a/Week02/Exercise01/date.h b/Week02/Exercise01/date.h
new file mode 100644
index 0000000..4d2ff1a
--- /dev/null
+++ b/Week02/Exercise01/date.h
@@ -0,0 +1,13 @@
+#ifndef _DATE
+#define _DATE
+
+struct Date {
+ unsigned day;
+ unsigned month;
+ unsigned year;
+};
+
+void printDate(const Date& d);
+Date readDate();
+
+#endif
diff --git a/Week02/Exercise01/main.cpp b/Week02/Exercise01/main.cpp
new file mode 100644
index 0000000..28856ef
--- /dev/null
+++ b/Week02/Exercise01/main.cpp
@@ -0,0 +1,12 @@
+#include <iostream>
+#include "date.h"
+
+int main() {
+ for (unsigned i = 0; i < 3; ++i) {
+ printDate(readDate());
+ std::cout << ' ';
+ }
+ std::cout << std::endl;
+
+ return 0;
+}
diff --git a/Week02/Exercise02/main.cpp b/Week02/Exercise02/main.cpp
new file mode 100644
index 0000000..4d43037
--- /dev/null
+++ b/Week02/Exercise02/main.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+#include "temperature.h"
+
+Temperature readTemperature() {
+ double degrees;
+ std::cin >> degrees;
+ char scale;
+ std::cin >> scale;
+
+ Temperature t;
+ switch (scale) {
+ case 'C': t.fromCelsius(degrees); break;
+ case 'F': t.fromFahrenheit(degrees); break;
+ case 'K': t.fromKelvin(degrees); break;
+ }
+
+ return t;
+}
+
+void printTemperature(const Temperature& t) {
+ std::cout << t.C << " C " << t.F << " F " << t.K << " K" << std::endl;
+}
+
+int main() {
+ Temperature first = readTemperature(), second = readTemperature();
+
+ printTemperature(first.Sum(second));
+
+ return 0;
+}
diff --git a/Week02/Exercise02/temperature.cpp b/Week02/Exercise02/temperature.cpp
new file mode 100644
index 0000000..3e497b9
--- /dev/null
+++ b/Week02/Exercise02/temperature.cpp
@@ -0,0 +1,21 @@
+#include "temperature.h"
+
+void Temperature::fromCelsius(double C) {
+ this->C = C;
+ F = (C * 1.8) + 32;
+ K = C + 273.15;
+}
+
+void Temperature::fromFahrenheit(double F) {
+ fromCelsius((F - 32) / 1.8);
+}
+
+void Temperature::fromKelvin(double K) {
+ fromCelsius(K - 273.15);
+}
+
+Temperature Temperature::Sum(const Temperature& other) {
+ Temperature t;
+ t.fromCelsius(this->C + other.C);
+ return t;
+}
diff --git a/Week02/Exercise02/temperature.h b/Week02/Exercise02/temperature.h
new file mode 100644
index 0000000..799313f
--- /dev/null
+++ b/Week02/Exercise02/temperature.h
@@ -0,0 +1,16 @@
+#ifndef _TEMPERATURE
+#define _TEMPERATURE
+
+struct Temperature {
+ double C;
+ double F;
+ double K;
+
+ void fromCelsius(double C);
+ void fromFahrenheit(double F);
+ void fromKelvin(double K);
+
+ Temperature Sum(const Temperature& other);
+};
+
+#endif
diff --git a/Week02/Exercise03/intarray.cpp b/Week02/Exercise03/intarray.cpp
new file mode 100644
index 0000000..3d8853d
--- /dev/null
+++ b/Week02/Exercise03/intarray.cpp
@@ -0,0 +1,27 @@
+#include "intarray.h"
+
+void IntArray::deleteSelf() {
+ delete[] elems;
+
+ size = 0;
+ elems = nullptr;
+}
+
+int IntArray::sum() {
+ int totalSum = 0;
+ for (unsigned i = 0; i < size; ++i)
+ totalSum += elems[i];
+ return totalSum;
+}
+
+IntArray newIntArray(unsigned size) {
+ IntArray ia;
+ ia.size = size;
+ ia.elems = new int[size];
+
+ return ia;
+}
+
+void deleteIntArray(IntArray& arr) {
+ arr.deleteSelf();
+}
diff --git a/Week02/Exercise03/intarray.h b/Week02/Exercise03/intarray.h
new file mode 100644
index 0000000..9f1c0b0
--- /dev/null
+++ b/Week02/Exercise03/intarray.h
@@ -0,0 +1,15 @@
+#ifndef _INTARRAY
+#define _INTARRAY
+
+struct IntArray {
+ unsigned size;
+ int* elems;
+
+ void deleteSelf();
+ int sum();
+};
+
+IntArray newIntArray(unsigned size);
+void deleteIntArray(IntArray& arr);
+
+#endif
diff --git a/Week02/Exercise03/main.cpp b/Week02/Exercise03/main.cpp
new file mode 100644
index 0000000..07f59fb
--- /dev/null
+++ b/Week02/Exercise03/main.cpp
@@ -0,0 +1,17 @@
+#include <iostream>
+#include "intarray.h"
+
+int main() {
+ unsigned N;
+ std::cin >> N;
+
+ IntArray ia = newIntArray(N);
+ for (unsigned i = 0; i < ia.size; ++i)
+ std::cin >> ia.elems[i];
+
+ std::cout << ((double)ia.sum() / ia.size) << std::endl;
+
+ deleteIntArray(ia);
+
+ return 0;
+}
diff --git a/Week02/Exercise04/main.cpp b/Week02/Exercise04/main.cpp
new file mode 100644
index 0000000..c02fd21
--- /dev/null
+++ b/Week02/Exercise04/main.cpp
@@ -0,0 +1,35 @@
+#include <iostream>
+#include "product.h"
+
+int main() {
+ unsigned productsCount;
+ std::cin >> productsCount;
+
+ Product* products = new Product[productsCount];
+
+ for (unsigned i = 0; i < productsCount; ++i) {
+ std::cin >> products[i].ID;
+ std::cin.ignore();
+ std::cin.getline(products[i].name, MAX_NAME_LENGTH);
+ }
+
+ for (unsigned i = 0; i < productsCount; ++i) {
+ unsigned ID, stockSize;
+ std::cin >> ID >> stockSize;
+
+ for (unsigned i = 0; i < productsCount; ++i) {
+ if (products[i].ID == ID) {
+ products[i].stockSize = stockSize;
+ break;
+ }
+ }
+ }
+
+ for (unsigned i = 0; i < productsCount; ++i) {
+ std::cout << products[i].name << ": " << products[i].stockSize << std::endl;
+ }
+
+ delete[] products;
+
+ return 0;
+}
diff --git a/Week02/Exercise04/product.h b/Week02/Exercise04/product.h
new file mode 100644
index 0000000..e83e5ee
--- /dev/null
+++ b/Week02/Exercise04/product.h
@@ -0,0 +1,12 @@
+#ifndef _PRODUCT
+#define _PRODUCT
+
+static const unsigned MAX_NAME_LENGTH = 256;
+
+struct Product {
+ unsigned ID;
+ char name[MAX_NAME_LENGTH];
+ unsigned stockSize;
+};
+
+#endif
diff --git a/Week02/Exercise05/main.cpp b/Week02/Exercise05/main.cpp
new file mode 100644
index 0000000..a79d628
--- /dev/null
+++ b/Week02/Exercise05/main.cpp
@@ -0,0 +1,49 @@
+#include <iostream>
+#include "package.h"
+#include "warehouse.h"
+
+int main() {
+ unsigned warehouseCount;
+ std::cin >> warehouseCount;
+
+ Warehouse* warehouses = new Warehouse[warehouseCount];
+
+ for (unsigned i = 0; i < warehouseCount; ++i) {
+ std::cin >> warehouses[i].ID >> warehouses[i].packageCapacity;
+ warehouses[i].packages = new Package[warehouses[i].packageCapacity];
+ }
+
+ unsigned packagesCount;
+ std::cin >> packagesCount;
+ std::cin.ignore();
+
+ for (unsigned i = 0; i < packagesCount; ++i) {
+ Package p = readPackage();
+
+
+ char warehouse;
+ std::cin >> warehouse;
+ std::cin.ignore();
+
+ for (unsigned i = 0; i < warehouseCount; ++i) {
+ if (warehouses[i].ID == warehouse) {
+ warehouses[i].storePackage(p);
+ break;
+ }
+ }
+ }
+
+ for (unsigned i = 0; i < warehouseCount; ++i) {
+ std::cout << warehouses[i].ID << ' ' << warehouses[i].storedPackages << '/' << warehouses[i].packageCapacity << std::endl;
+ for (unsigned j = 0; j < warehouses[i].storedPackages; ++j) {
+ std::cout << warehouses[i].packages[j].ID << std::endl;
+ }
+ }
+
+ for (unsigned i = 0; i < warehouseCount; ++i) {
+ delete[] warehouses[i].packages;
+ }
+ delete[] warehouses;
+
+ return 0;
+}
diff --git a/Week02/Exercise05/package.cpp b/Week02/Exercise05/package.cpp
new file mode 100644
index 0000000..b055fa7
--- /dev/null
+++ b/Week02/Exercise05/package.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include "package.h"
+
+Package readPackage() {
+ Package p;
+ std::cin.getline(p.ID, 11);
+ // Когато getline не стигне искания знак ('\n'),
+ // една специална "fail" променлива става true и
+ // това задължава всеки следващ вход да се провали.
+ // С clear правим "fail" променливата на false
+ std::cin.clear();
+ std::cin >> p.weight;
+ return p;
+}
diff --git a/Week02/Exercise05/package.h b/Week02/Exercise05/package.h
new file mode 100644
index 0000000..d076189
--- /dev/null
+++ b/Week02/Exercise05/package.h
@@ -0,0 +1,11 @@
+#ifndef _PACKAGE
+#define _PACKAGE
+
+struct Package {
+ char ID[11]; // 10 букви за кода и още една за терминираща нула
+ double weight;
+};
+
+Package readPackage();
+
+#endif
diff --git a/Week02/Exercise05/warehouse.cpp b/Week02/Exercise05/warehouse.cpp
new file mode 100644
index 0000000..ea968ab
--- /dev/null
+++ b/Week02/Exercise05/warehouse.cpp
@@ -0,0 +1,9 @@
+#include "warehouse.h"
+
+bool Warehouse::storePackage(const Package& p) {
+ if (storedPackages >= packageCapacity)
+ return false;
+
+ packages[storedPackages++] = p;
+ return true;
+}
diff --git a/Week02/Exercise05/warehouse.h b/Week02/Exercise05/warehouse.h
new file mode 100644
index 0000000..34f19b4
--- /dev/null
+++ b/Week02/Exercise05/warehouse.h
@@ -0,0 +1,15 @@
+#ifndef _WAREHOUSE
+#define _WAREHOUSE
+
+#include "package.h"
+
+struct Warehouse {
+ char ID;
+ Package* packages;
+ unsigned storedPackages;
+ unsigned packageCapacity;
+
+ bool storePackage(const Package& p);
+};
+
+#endif
diff --git a/Week02/Exercise06/main.cpp b/Week02/Exercise06/main.cpp
new file mode 100644
index 0000000..e1e6d58
--- /dev/null
+++ b/Week02/Exercise06/main.cpp
@@ -0,0 +1,13 @@
+#include <iostream>
+#include "time.h"
+
+// По условие не ни е поискана логика за главната функция,
+// но все пак е добра идея да се провери имплементацията
+int main() {
+ Time first = { 13, 4, 15 }, second = { 1, 59, 45 };
+ first.Advance(second);
+
+ std::cout << first.hour << ' ' << first.minutes << ' ' << first.seconds << std::endl;
+
+ return 0;
+}
diff --git a/Week02/Exercise06/time.cpp b/Week02/Exercise06/time.cpp
new file mode 100644
index 0000000..74b1447
--- /dev/null
+++ b/Week02/Exercise06/time.cpp
@@ -0,0 +1,19 @@
+#include "time.h"
+
+unsigned timeToMilliseconds(const Time& t) {
+ return (t.hour * 60 * 60 + t.minutes * 60 + t.seconds) * 1000;
+}
+
+void Time::Advance(const Time& other) {
+ // По-простия вариант е с експлицитни сметки, но помислете защо това работи
+ *this = millisecondsToTime(timeToMilliseconds(*this) + timeToMilliseconds(other));
+}
+
+Time millisecondsToTime(unsigned int milliseconds) {
+ Time t;
+ t.hour = (milliseconds / 1000 / 60 / 60) % 24;
+ t.minutes = (milliseconds / 1000 / 60) % 60;
+ t.seconds = (milliseconds / 1000) % 60;
+
+ return t;
+}
diff --git a/Week02/Exercise06/time.h b/Week02/Exercise06/time.h
new file mode 100644
index 0000000..a3a5921
--- /dev/null
+++ b/Week02/Exercise06/time.h
@@ -0,0 +1,14 @@
+#ifndef _TIME
+#define _TIME
+
+struct Time {
+ unsigned hour;
+ unsigned minutes;
+ unsigned seconds;
+
+ void Advance(const Time& other);
+};
+
+Time millisecondsToTime(unsigned milliseconds);
+
+#endif
diff --git a/Week02/README.md b/Week02/README.md
new file mode 100644
index 0000000..01c0271
--- /dev/null
+++ b/Week02/README.md
@@ -0,0 +1,3 @@
+# Задачи
+
+<https://syndamia.com/teaching/oop-2025/week02/>