Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. //
  2. // Created by Vlad on 18.01.2017.
  3. //
  4.  
  5. #ifndef PREPAREFROMEXAMS_TEST_H
  6. #define PREPAREFROMEXAMS_TEST_H
  7.  
  8. #include <iostream>
  9.  
  10. class Date {
  11. private:
  12.     const int dayInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  13.     const int dayInMonthV[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  14.     long date;
  15.  
  16.     Date(long date) {
  17.         this->date = date;
  18.     }
  19.  
  20.     long threeToOne(int day, int month, int year) {
  21.         long date = 0;
  22.         date = date + year * 365 + year / 4 - year / 100 + year / 400;
  23.         bool isV = yearIsV(year);
  24.         for (int i = 0; i < month - 1; i++) {
  25.             date += isV ? dayInMonthV[i] : dayInMonth[i];
  26.         }
  27.         date += day;
  28.         return date;
  29.     }
  30.  
  31.     void oneToThree(long date, int &day, int &month, int &year) {
  32.         while (date >= 365) {
  33.             date -= yearIsV(year) ? 366 : 365;
  34.             year++;
  35.         }
  36.         bool isV = yearIsV(year);
  37.         std::cout << date << std::endl;
  38.         while (date >= (isV ? dayInMonthV[month] : dayInMonth[month])) {
  39.             date -= isV ? dayInMonthV[month] : dayInMonth[month];
  40.             month++;
  41.         }
  42.         month++;
  43.         day = date + 1;
  44.     }
  45.  
  46.     bool yearIsV(int year) {
  47.         return ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0));
  48.     }
  49.  
  50. public:
  51.     Date(int day, int month, int year) {
  52.         this->date = threeToOne(day, month, year);
  53. //        long test = threeToOne(day, month, year);
  54. //        int d = 0, m = 0, y = 0;
  55. //        std::cout << test << std::endl;
  56. //        oneToThree(test, d, m, y);
  57. //        std::cout << d << ":" << m << ":" << y;
  58.     }
  59.  
  60.     Date operator+(Date &d) {
  61.         return Date(this->date + d.date -366);
  62.     }
  63.  
  64.     friend std::ostream &operator<<(std::ostream &os, Date &date) {
  65.         int day = 0;
  66.         int month = 0;
  67.         int year = 0;
  68.         date.oneToThree(date.date, day, month, year);
  69.         os << day << ":" << month << ":" << year;
  70.         return os;
  71.     }
  72.  
  73. };
  74.  
  75.  
  76. #endif //PREPAREFROMEXAMS_TEST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement