Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commite3b77ecd975e6f75adc50c2e0fed9e78f5163e07 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-04-02 20:42:16 +0300
Committer
 GitHub <noreply@github.com>
2022-04-02 20:42:16 +0300
Filesee7875627027ab613ed4be355a0ea24f5b9057fb
Object-oriented_programming_FMI-e3b77ecd975e6f75adc50c2e0fed9e78f5163e07.tar
Object-oriented_programming_FMI-e3b77ecd975e6f75adc50c2e0fed9e78f5163e07.tar.gz
Object-oriented_programming_FMI-e3b77ecd975e6f75adc50c2e0fed9e78f5163e07.zip
Parent8c11fe93cd21fe2a7586959f85d79dcf9812968c (diff)
Add files via upload
diff --git a/допълнително упражнение/Task1/LogFile.cpp b/допълнително упражнение/Task1/LogFile.cpp
new file mode 100644
index 0000000..01fa25e
--- /dev/null
+++ b/допълнително упражнение/Task1/LogFile.cpp
@@ -0,0 +1,92 @@
+#include "LogFile.h"
+
+void LogFile::readTime(std::ifstream& inFile, Timestamp& obj) const {
+ char isLogin;
+
+ inFile >> isLogin;
+
+ obj.isLogin = (isLogin == '+');
+
+ inFile >> obj.hour >> obj.minutes >> obj.seconds;
+}
+
+
+LogFile::LogFile() {
+ size = 0;
+}
+
+LogFile::LogFile(const char* filePath) {
+ std::ifstream inFile(filePath);
+
+ if (!inFile.is_open()) {
+ size = 0;
+ return;
+ }
+
+ inFile >> size;
+
+ for (int i = 0; i < size; i++)
+ readTime(inFile, data[i]);
+}
+
+
+int LogFile::getLoginsCountAfterHour(short hours, short minutes, short seconds) const {
+ int ctr = 0;
+ int totalSeconds = hours * 3600 + minutes * 60 + seconds;
+ for (int i = 0; i < size; i++) {
+ if (data[i].toSeconds() > totalSeconds) {
+ ctr = i + 1;
+ break;
+ }
+ }
+
+ return size - ctr;
+}
+
+int LogFile::getLoginsCountBetweenHours(short h1, short m1, short s1, short h2, short m2, short s2) const {
+ return getLoginsCountAfterHour(h1, m1, s1) - getLoginsCountAfterHour(h2, m2, s2);
+}
+
+int LogFile::getLongestActivePeriod() const {
+ int max = 0;
+ bool isFirstLogOut = !data[0].isLogin;
+ bool isLastLogIn = data[size - 1].isLogin;
+
+ if (isFirstLogOut)
+ max = data[0].toSeconds();
+
+ if (isLastLogIn) {
+ int lastTimeInterval = 23 * 3600 + 59 * 60 + 59 - data[size - 1].toSeconds();
+
+ if (max < lastTimeInterval)
+ max = lastTimeInterval;
+ }
+
+ for (int i = isFirstLogOut; i < size - isLastLogIn; i += 2) {
+ int diff = data[i + 1].toSeconds() - data[i].toSeconds();
+ if (diff > max)
+ max = diff;
+ }
+
+ return max;
+}
+
+bool LogFile::loggedInHour(int hour) const {
+ int left = 0;
+ int right = size-1;
+
+ while (left <= right) {
+ int mid = left + (right - left) / 2;
+ if (hour == data[mid].hour) {
+ return true;
+ }
+
+ if (hour < data[mid].hour) {
+ right = mid - 1;
+ }
+ else {
+ left = mid + 1;
+ }
+ }
+ return false;
+} \ No newline at end of file
diff --git a/допълнително упражнение/Task1/LogFile.h b/допълнително упражнение/Task1/LogFile.h
new file mode 100644
index 0000000..8c021f8
--- /dev/null
+++ b/допълнително упражнение/Task1/LogFile.h
@@ -0,0 +1,24 @@
+#pragma once
+#include<fstream>
+#include "Timestamp.h"
+const int MAX_FILE_SIZE = 100;
+class LogFile {
+private:
+ size_t size;
+ Timestamp data[MAX_FILE_SIZE];
+
+ void readTime(std::ifstream& inFile, Timestamp& obj) const;
+
+public:
+ LogFile();
+
+ LogFile(const char* filePath);
+
+ int getLoginsCountAfterHour(short hours, short minutes, short seconds) const;
+
+ int getLoginsCountBetweenHours(short h1, short m1, short s1, short h2, short m2, short s2) const;
+
+ int getLongestActivePeriod() const;
+
+ bool loggedInHour(int hour) const;
+}; \ No newline at end of file
diff --git a/допълнително упражнение/Task1/Timestamp.cpp b/допълнително упражнение/Task1/Timestamp.cpp
new file mode 100644
index 0000000..a874eb8
--- /dev/null
+++ b/допълнително упражнение/Task1/Timestamp.cpp
@@ -0,0 +1,5 @@
+#include "Timestamp.h"
+
+int Timestamp::toSeconds() const{
+ return hour * 3600 + minutes * 60 + seconds;
+} \ No newline at end of file
diff --git a/допълнително упражнение/Task1/Timestamp.h b/допълнително упражнение/Task1/Timestamp.h
new file mode 100644
index 0000000..912d69a
--- /dev/null
+++ b/допълнително упражнение/Task1/Timestamp.h
@@ -0,0 +1,9 @@
+#pragma once
+struct Timestamp {
+ bool isLogin;
+ unsigned short hour;
+ unsigned short minutes;
+ unsigned short seconds;
+
+ int toSeconds() const;
+}; \ No newline at end of file