Website Summary Files Commits Branches @commit @diff @stats
path: root / Week02 / Exercise06
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/Exercise06
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.tar
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.tar.gz
oop-2025-solutions-f18c44909c604a01bb0265d006a84223ee88d603.zip
Parentfcf38e62e8160f2c9901bdfd33760afed28beb8d (diff)
feat(Week02): Add solutions
3 files changed, 46 insertions, 0 deletions
-rw-r--r--Week02/Exercise06/main.cpp13
-rw-r--r--Week02/Exercise06/time.cpp19
-rw-r--r--Week02/Exercise06/time.h14
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