Website Summary Files Commits Branches @commit @diff @stats
path: root / Week02 / Exercise05
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/Exercise05
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.tar
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.tar.gz
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.zip
Parentfcf38e62e8160f2c9901bdfd33760afed28beb8d (diff)
feat(Week02): Add solutions
5 files changed, 98 insertions, 0 deletions
-rw-r--r--Week02/Exercise05/main.cpp49
-rw-r--r--Week02/Exercise05/package.cpp14
-rw-r--r--Week02/Exercise05/package.h11
-rw-r--r--Week02/Exercise05/warehouse.cpp9
-rw-r--r--Week02/Exercise05/warehouse.h15
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