Advertisement
chevengur

Chapter 9: Task 1

Sep 25th, 2023 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #chrono.h
  2. =======================================================================================================================================
  3. #pragma once
  4. #include <iostream>
  5.  
  6.  
  7. namespace chrono {
  8.     enum class Month {
  9.         jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
  10.     };
  11.  
  12.     class Date {
  13.     public:
  14.         class Invalid {
  15.             Invalid() = default;
  16.         };
  17.         Date(int y, Month m, int d);
  18.  
  19.         Date();
  20.  
  21.         int day() const { return d; }
  22.         Month month() const { return m; }
  23.         int year() const { return y; }
  24.         void add_day(int n);
  25.         void add_month(int n);
  26.         void add_year(int n);
  27.  
  28.     private:
  29.         int y;
  30.         Month m;
  31.         int d;
  32.  
  33.     };
  34.  
  35.     bool is_date(int y, Month m, int d);
  36.     bool leapyear(int y);
  37.     bool operator==(const Date& a, const Date& b);
  38.     bool operator!=(const Date& a, const Date& b);
  39.     std::ostream& operator<<(std::ostream& os, const Date& d);
  40.     std::istream& operator>>(std::istream& is, Date& dd);
  41.  
  42. }
  43. =======================================================================================================================================
  44. #chrono.cpp
  45. =======================================================================================================================================
  46. #include "chrono.h"
  47.  
  48. namespace chrono {
  49.     Date::Date(int yy, Month mm, int dd) : y(yy), m(mm), d(dd) {
  50.         if (!is_date(yy, mm, dd)) throw Invalid{};
  51.     }
  52.  
  53.     const Date& default_date() {
  54.         static Date dd{ 2001, Month::jan, 1 };
  55.         return dd;
  56.     }
  57.  
  58.     Date::Date() : y{ default_date().year() }, m(default_date().month()), d{ default_date().day() } {
  59.  
  60.     }
  61.  
  62.     void Date::add_day(int n) {
  63.         d+=n;
  64.     }
  65.  
  66.     void Date::add_month(int n) {
  67.  
  68.     }
  69.  
  70.     void Date::add_year(int n) {
  71.         if (m == Month::feb && d == 29 && !leapyear(y + n)) {
  72.             m = Month::mar;
  73.             d = 1;
  74.         }
  75.         y += n;
  76.     }
  77.  
  78.     bool is_date(int y, Month m, int d) {
  79.         if (d <= 0)
  80.             return false;
  81.         if (m < Month::jan || Month::dec < m) return false;
  82.  
  83.         int days_in_month = 31;
  84.         switch (m) {
  85.         case Month::feb:
  86.             days_in_month = (leapyear(y)) ? 29 : 28;
  87.             break;
  88.  
  89.         case Month::apr: case Month::jun: case Month::sep: case Month::nov:
  90.             days_in_month = 30;
  91.             break;
  92.         }
  93.  
  94.         if (days_in_month < d) return false;
  95.         return true;
  96.     }
  97.  
  98.     bool leapyear(int y) {
  99.         if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
  100.             return true;
  101.         }
  102.         else {
  103.             return false;
  104.         }
  105.     }
  106.  
  107.     bool operator==(const Date& a, const Date& b) {
  108.         return a.year() == b.year()
  109.             && a.month() == b.month()
  110.             && a.day() == b.day();
  111.     }
  112.  
  113.     bool operator!=(const Date& a, const Date& b) {
  114.         return !(a == b);
  115.     }
  116.  
  117.     std::ostream& operator<<(std::ostream& os, const Date& d){
  118.         return os << '(' << d.year()
  119.             << ", " << static_cast<int>(d.month())
  120.             << ", " << d.day() << ')';
  121.     }
  122.  
  123.     std::istream& operator>>(std::istream& is, Date& dd){
  124.         int y, m, d;
  125.         char ch1, ch2, ch3, ch4;
  126.         is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
  127.         if (!is) return is;
  128.         if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')') {
  129.             is.clear(std::ios_base::failbit);
  130.             return is;
  131.         }
  132.         dd = Date(y, Month(m), d);
  133.         return is;
  134.     }
  135.  
  136.     enum class Day {
  137.         sunday, monday, tuesday, wednesday, thursday, friday, saturday
  138.     };
  139.    
  140.     /*void day_of_week(const Date& d) {
  141.  
  142.     }
  143.  
  144.     void next_Sunday(const Date& d) {
  145.  
  146.     }
  147.  
  148.     void next_weekday(const Date& d) {
  149.  
  150.     }*/
  151. } //chrono
  152. =======================================================================================================================================
  153. #main.cpp
  154. =======================================================================================================================================
  155. #include <iostream>
  156. #include "chrono.h"
  157.  
  158. int main() {
  159.     chrono::Date today{ 1963, chrono::Month::may, 32 };
  160.     chrono::Date tomorrow = today;
  161.     tomorrow.add_day(1);
  162.  
  163.     std::cout << today << " " << tomorrow;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement