From e3b77ecd975e6f75adc50c2e0fed9e78f5163e07 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Sat, 2 Apr 2022 20:42:16 +0300 Subject: Add files via upload --- .../Task1/LogFile.cpp" | 92 ++++++++++++++++++++++ .../Task1/LogFile.h" | 24 ++++++ .../Task1/Timestamp.cpp" | 5 ++ .../Task1/Timestamp.h" | 9 +++ 4 files changed, 130 insertions(+) create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" new file mode 100644 index 0000000..01fa25e --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/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/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" new file mode 100644 index 0000000..8c021f8 --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" @@ -0,0 +1,24 @@ +#pragma once +#include +#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/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" new file mode 100644 index 0000000..a874eb8 --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/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/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" new file mode 100644 index 0000000..912d69a --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/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 -- cgit v1.3.1-23-gd82d