Guest User

Untitled

a guest
Dec 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #include "date_class.h"
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. Date::Date() : m_year(1901), m_month(1), m_day(1) {}
  6.  
  7. Date::Date(const short int &year, const short int &month, const short int &day) : m_year(year), m_month(month),
  8.                                                                                   m_day(day) {}
  9.  
  10. Date::Date(const Date &copy) : m_year(copy.m_year), m_month(copy.m_month), m_day(copy.m_day) {}
  11.  
  12. //long int Date::operator-(const Date & d2){
  13. //    return this->count_days() - d2.count_days();
  14. //}
  15. //
  16. //long int Date::operator+(const Date & d2){
  17. //    return this->count_days() + d2.count_days();
  18. //}
  19.  
  20. const bool Date::operator<(const Date &d2) const {
  21.     return this->m_year <= d2.m_year && this->m_month <= d2.m_month && this->m_day < d2.m_day;
  22. }
  23.  
  24. const bool Date::operator>(const Date &d2) const {
  25.     return this->m_year >= d2.m_year && this->m_month >= d2.m_month && this->m_day > d2.m_day;
  26. }
  27.  
  28. const bool Date::operator<=(const Date &d2) const {
  29.     return !(*this > d2);
  30. }
  31.  
  32. const bool Date::operator>=(const Date &d2) const {
  33.     return !(*this < d2);
  34. }
  35.  
  36. const bool Date::operator==(const Date &d2) const {
  37.     return this->m_year == d2.m_year && this->m_month == d2.m_month && this->m_day == d2.m_day;
  38. }
  39.  
  40. const bool Date::operator!=(const Date &d2) const {
  41.     return !(*this == d2);
  42. }
  43.  
  44. void Date::print_date() const {
  45.     std::cout << "\t " << m_year << '/' << m_month << '/' << m_day << std::endl;
  46. }
  47.  
  48. void Date::set_date(const short int &year, const short int &month, const short int &day) {
  49.     m_year = year;
  50.     m_month = month;
  51.     m_day = day;
  52. }
  53.  
  54. const bool Date::is_leap_year() const {
  55.     return m_year % 400 == 0
  56.            || (m_year % 100 != 0
  57.                && m_year % 4 == 0);
  58. }
  59.  
  60. const bool Date::is_leap_year(const short int &year) const {
  61.     return year % 400 == 0
  62.            || (year % 100 != 0
  63.                && year % 4 == 0);
  64. }
  65.  
  66. const short int Date::days_in_year() const {
  67.     if (is_leap_year())
  68.         return 366;
  69.     return 365;
  70. }
  71.  
  72. const short int Date::days_in_year(const short int &year) const {
  73.     if (is_leap_year(year))
  74.         return 366;
  75.     return 365;
  76. }
  77.  
  78. const short int Date::days_in_month() const {
  79.     if (m_month == 4 || m_month == 6 || m_month == 9 || m_month == 11)
  80.         return 30;
  81.     if (m_month == 2) {
  82.         if (is_leap_year())
  83.             return 29;
  84.         else
  85.             return 28;
  86.     }
  87.     return 31;
  88. }
  89.  
  90. const short int Date::days_in_month(const short int &year, const short int &month) const {
  91.     if (month == 4 || month == 6 || month == 9 || month == 11)
  92.         return 30;
  93.     if (month == 2) {
  94.         if (is_leap_year(year))
  95.             return 29;
  96.         else
  97.             return 28;
  98.     }
  99.     return 31;
  100. }
  101.  
  102. const bool Date::correct_date() const {
  103.     return m_year >= 1901
  104.            && m_month >= 1 && m_month <= 12
  105.            && m_day >= 1 && m_day <= days_in_month();
  106. }
  107.  
  108. void Date::next_day() const {
  109.     if (!correct_date()) {
  110.         std::cout << "wrong data!";
  111.         return;
  112.     }
  113.  
  114.     Date date(*this);
  115.     if (m_day < days_in_month())
  116.         ++date.m_day;
  117.     else {
  118.         date.m_day = 1;
  119.         ++date.m_month;
  120.     }
  121.  
  122.     if (date.m_month == 13) {
  123.         date.m_month = 1;
  124.         ++date.m_year;
  125.     }
  126.  
  127.     date.print_date();
  128. }
  129.  
  130. void Date::previous_day() const {
  131.     if (!correct_date()) {
  132.         std::cout << "wrong data!";
  133.         return;
  134.     }
  135.  
  136.     Date date(*this);
  137.     if (date.m_day > 1)
  138.         --date.m_day;
  139.     else {
  140.         --date.m_month;
  141.  
  142.         if (date.m_month == 0) {
  143.             date.m_month = 12;
  144.             --date.m_year;
  145.         }
  146.  
  147.         date.m_day = days_in_month(date.m_year, date.m_month);
  148.     }
  149.     date.print_date();
  150. }
  151.  
  152. const long int Date::count_days() const {
  153.     long int s(0);
  154.     short int y;
  155.     for (y = _firstYear; y < m_year; ++y)
  156.         s += days_in_year(y);
  157.  
  158.     for (short int m = 1; m < m_month; ++m)
  159.         s += days_in_month(y, m);
  160.  
  161.     s += m_day;
  162.  
  163.     return s;
  164. }
  165.  
  166. void Date::define_date(long int days) const {
  167.     Date date(0, 1, 0);
  168.  
  169.     while (days > days_in_year(_firstYear + date.m_year))
  170.         days -= days_in_year(_firstYear + date.m_year++);
  171.     date.m_year += _firstYear;
  172.  
  173.     while (days > days_in_month(date.m_year, date.m_month))
  174.         days -= days_in_month(date.m_year, date.m_month++);
  175.  
  176.     date.m_day = (short int) days;
  177.  
  178.     date.print_date();
  179. }
  180.  
  181. void Date::future_date(long int days) const {
  182.     days += count_days();
  183.     define_date(days);
  184. }
  185.  
  186. void Date::past_date(long int days) const {
  187.     days = count_days() - days;
  188.     define_date(days);
  189. }
  190.  
  191. const long int count_days_between_dates(const Date &d1, const Date &d2) {
  192.     return std::abs(d1.count_days() - d2.count_days());
  193. }
  194.  
  195. const bool compare_dates(const Date &d1, const Date & d2) {
  196.     return d1 < d2;
  197. }
  198.  
  199. void sort_dates(Date *dates, const int &num) {
  200.     std::sort(dates, dates + num, compare_dates);
  201. }
Add Comment
Please, Sign In to add comment