From d3c133e918cb85933c961c31871d038cace6b638 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Tue, 29 Mar 2022 20:00:49 +0300 Subject: Add files via upload --- Sem. 06/Event/Date.cpp | 107 ++++++++++++++++++++++++++++++++++++++++ Sem. 06/Event/Date.h | 33 +++++++++++++ Sem. 06/Event/Event.cpp | 50 +++++++++++++++++++ Sem. 06/Event/Event.h | 24 +++++++++ Sem. 06/Event/Source.cpp | 9 +++- Sem. 06/Event/Time.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++ Sem. 06/Event/Time.h | 41 +++++++++++++++ 7 files changed, 389 insertions(+), 1 deletion(-) create mode 100644 Sem. 06/Event/Date.cpp create mode 100644 Sem. 06/Event/Date.h create mode 100644 Sem. 06/Event/Event.cpp create mode 100644 Sem. 06/Event/Event.h create mode 100644 Sem. 06/Event/Time.cpp create mode 100644 Sem. 06/Event/Time.h diff --git a/Sem. 06/Event/Date.cpp b/Sem. 06/Event/Date.cpp new file mode 100644 index 0000000..51543c3 --- /dev/null +++ b/Sem. 06/Event/Date.cpp @@ -0,0 +1,107 @@ + +#include "Date.h" +#include +#include +using namespace std; + +bool Date::isLeapYear() const +{ +if (year <= 1916) + return year % 4 == 0; +return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); +} + + + +Date::Date(size_t day, size_t month, size_t year) +{ + setYear(year); + setMonth(month); + setDay(day); +} +size_t Date::getDay() const +{ + return day; +} +size_t Date::getMonth() const +{ + return month; +} +size_t Date::getYear() const +{ + return year; +} +void Date::setYear(size_t year) +{ + this->year = year; + + if (isLeapYear()) + MAX_DAYS[1] = 29; + else + MAX_DAYS[1] = 28; + isModified = true; +} +void Date::setMonth(size_t month) +{ + if (month > 12) + { + month = 1; + } + this->month = month; + isModified = true; +} +void Date::setDay(size_t day) +{ + if (day > MAX_DAYS[month - 1]) + { + day = 1; + } + isModified = true; + this->day = day; +} +void Date::print() const +{ + std::cout << setw(2) << setfill('0') << day << "." << setw(2) << setfill('0') << month << "."; + std::cout << setw(4) << setfill('0') << year; +} + +void Date::goToNextDay() +{ + if (areEqual(Date(31, 03, 1916))) + { + setDay(14); + setMonth(04); + return; + } + setDay(day + 1); + if (day == 1) + { + setMonth(month + 1); + if (month == 1) + setYear(year + 1); + } +} +bool Date::areEqual(const Date& other) const +{ + return year == other.year && month == other.month && day == other.day; +} + +int Date::getDayOfWeek() +{ + + if (!isModified) + return dayOfWeek; + + Date d(1, 1, 1); + int day = 5; + + while (!areEqual(d)) + { + d.goToNextDay(); + (day += 1) %= 7; + + } + dayOfWeek = day + 1; + isModified = false; + return dayOfWeek; +} \ No newline at end of file diff --git a/Sem. 06/Event/Date.h b/Sem. 06/Event/Date.h new file mode 100644 index 0000000..8c2f085 --- /dev/null +++ b/Sem. 06/Event/Date.h @@ -0,0 +1,33 @@ +#pragma once + +class Date +{ + size_t day; + size_t month; + size_t year; + + size_t MAX_DAYS[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + mutable int dayOfWeek; //allowing to be changed by constant objects + mutable bool isModified = true; + + bool isLeapYear() const; +public: + Date(size_t day, size_t month, size_t year); + + size_t getDay() const; + size_t getMonth() const; + size_t getYear() const; + + void setDay(size_t); + void setMonth(size_t); + void setYear(size_t); + + void print() const; + void goToNextDay(); + + bool areEqual(const Date& other) const; + + int getDayOfWeek(); //cant be const, because of changings dayOfWeek and isModified + // can we get around this ?? +}; \ No newline at end of file diff --git a/Sem. 06/Event/Event.cpp b/Sem. 06/Event/Event.cpp new file mode 100644 index 0000000..0962843 --- /dev/null +++ b/Sem. 06/Event/Event.cpp @@ -0,0 +1,50 @@ +#pragma once +#include "Event.h" + +Event::Event(const char* name, const Time& begin, const Date& date) : begin(begin), date(date) +{ + setName(name); +} + +Event::Event(const char* name, size_t hours, size_t mins, size_t seconds, size_t day, size_t month, size_t year) : begin(hours, mins, seconds), date(day, month, year) +{ + setName(name); +} + + +void Event::setName(const char* name) +{ + if (strlen(name) > 19) + strcpy(this->name, "Unknown"); + else + strcpy(this->name, name); +} + + +const Time& Event::getTime() const +{ + return begin; +} +const Date& Event::getDate() const +{ + return date; +} + +const char* Event::getName() const +{ + return name; +} + +int Event::getEventsDayOfWeek() +{ + return date.getDayOfWeek(); +} + +void Event::print() const +{ + std::cout << "Event: " << name << ", date: "; + date.print(); + std::cout << ", time: "; + begin.print(true); + std::cout << std::endl; +} \ No newline at end of file diff --git a/Sem. 06/Event/Event.h b/Sem. 06/Event/Event.h new file mode 100644 index 0000000..d836a9f --- /dev/null +++ b/Sem. 06/Event/Event.h @@ -0,0 +1,24 @@ +#pragma once +#include "Date.h" +#include "Time.h" +class Event +{ + char name[20]; + Time begin; + Date date; + + void setName(const char* name); + +public: + Event(const char* name, const Time& begin, const Date& date); + Event(const char* name, size_t hours, size_t mins, size_t seconds, size_t day, size_t month, size_t year); + + const Time& getTime() const; + const Date& getDate() const; + const char* getName() const; + + void print() const; + + int getEventsDayOfWeek(); //can't be const (date.getDayOfWeek is not a constant function) + // how can we fix it ??? +}; \ No newline at end of file diff --git a/Sem. 06/Event/Source.cpp b/Sem. 06/Event/Source.cpp index b478595..8f2e013 100644 --- a/Sem. 06/Event/Source.cpp +++ b/Sem. 06/Event/Source.cpp @@ -1 +1,8 @@ -s +#include +#include +#include "Event.h" +int main() +{ + Event v("OOP seminar", 17, 0, 0, 5, 4, 2022); + v.print(); +} \ No newline at end of file diff --git a/Sem. 06/Event/Time.cpp b/Sem. 06/Event/Time.cpp new file mode 100644 index 0000000..5c8fe44 --- /dev/null +++ b/Sem. 06/Event/Time.cpp @@ -0,0 +1,126 @@ +#include "Time.h" + +Time::Time(size_t hours, size_t mins, size_t seconds) +{ + setHours(hours); + setMins(mins); + setSeconds(seconds); +} + +Time::Time() : Time(0, 0, 0) +{} + +Time::Time(size_t seconds) +{ + hours = seconds / 3600; + seconds %= 3600; + + mins = seconds / 60; + seconds %= 61; + + this->seconds = seconds; +} + + +size_t Time::getSeconds() const +{ + return seconds; +} +size_t Time::getMins() const +{ + return mins; +} +size_t Time::getHours() const +{ + return hours; +} + +void Time::setSeconds(size_t seconds) +{ + if (seconds > 59) + seconds = 59; + this->seconds = seconds; +} +void Time::setMins(size_t mins) +{ + if (mins > 59) + mins = 59; + this->mins = mins; +} +void Time::setHours(size_t hours) +{ + if (hours > 23) + hours = 23; + this->hours = hours; +} + +void Time::tick() +{ + size_t seconds = convertToSeconds(); + seconds++; + Time newObj(seconds); + + *this = newObj; //копие +} + +int Time::compare(const Time& other) const +{ + size_t mySeconds = convertToSeconds(); + size_t otherSeconds = other.convertToSeconds(); + + if (mySeconds > otherSeconds) + return 1; + else if (mySeconds < otherSeconds) + return -1; + else + return 0; +} +Time Time::getDiff(const Time& other) const +{ + size_t mySeconds = convertToSeconds(); + size_t otherSeconds = other.convertToSeconds(); + + size_t diff; + + if (mySeconds > otherSeconds) + diff = mySeconds - otherSeconds; + else + diff = otherSeconds - mySeconds; + + return Time(diff); +} + +Time Time::getToMidnight() const +{ + Time midnight(23, 59, 59); + Time diff = getDiff(midnight); + diff.tick(); + return diff; +} +bool Time::isDinnerTime() const +{ + Time lowerBound(20, 30, 0); + Time upperBound(22, 0, 0); + return compare(lowerBound) >= 0 && compare(upperBound) <= 0; +} +bool Time::isPartyTime() const +{ + Time lowerBound(23, 0, 0); + Time upperBound(6, 0, 0); + return compare(lowerBound) >= 0 || compare(upperBound) <= 0; +} + +void Time::print(bool is12hoursclock) const +{ + if (hours < 10) + std::cout << 0; + std::cout << ((is12hoursclock && hours > 12) ? hours - 12 : hours) << ":"; + if (mins < 10) + std::cout << 0; + std::cout << mins << ":"; + if (seconds < 10) + std::cout << 0; + std::cout << seconds; + if (is12hoursclock) + std::cout << ((hours > 12) ? "PM" : "AM"); +} \ No newline at end of file diff --git a/Sem. 06/Event/Time.h b/Sem. 06/Event/Time.h new file mode 100644 index 0000000..be90962 --- /dev/null +++ b/Sem. 06/Event/Time.h @@ -0,0 +1,41 @@ +#pragma once +#include + +class Time +{ + size_t seconds; //[0...59] + size_t mins; //0...59 + size_t hours; // 0...23 + + size_t convertToSeconds() const + { + return 3600 * hours + 60 * mins + seconds; + } + + +public: + Time(); // 00:00:00 + Time(size_t hours, size_t mins, size_t seconds); + Time(size_t seconds); + + + size_t getSeconds() const; + size_t getMins() const; + size_t getHours() const; + + void setSeconds(size_t seconds); + void setMins(size_t mins); + void setHours(size_t hours); + + void tick(); + + int compare(const Time& other) const; + Time getDiff(const Time& other) const; + + Time getToMidnight() const; + bool isPartyTime() const;//23:00 05;00 + bool isDinnerTime() const;//19:30 21:30 + + void print(bool is12hoursclock) const; + +}; \ No newline at end of file -- cgit v1.3.1-23-gd82d