Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Vlad on 18.01.2017.
- //
- #ifndef PREPAREFROMEXAMS_TEST_H
- #define PREPAREFROMEXAMS_TEST_H
- #include <iostream>
- class Date {
- private:
- const int dayInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int dayInMonthV[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- long date;
- Date(long date) {
- this->date = date;
- }
- long threeToOne(int day, int month, int year) {
- long date = 0;
- date = date + year * 365 + year / 4 - year / 100 + year / 400;
- bool isV = yearIsV(year);
- for (int i = 0; i < month - 1; i++) {
- date += isV ? dayInMonthV[i] : dayInMonth[i];
- }
- date += day;
- return date;
- }
- void oneToThree(long date, int &day, int &month, int &year) {
- while (date >= 365) {
- date -= yearIsV(year) ? 366 : 365;
- year++;
- }
- bool isV = yearIsV(year);
- std::cout << date << std::endl;
- while (date >= (isV ? dayInMonthV[month] : dayInMonth[month])) {
- date -= isV ? dayInMonthV[month] : dayInMonth[month];
- month++;
- }
- month++;
- day = date + 1;
- }
- bool yearIsV(int year) {
- return ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0));
- }
- public:
- Date(int day, int month, int year) {
- this->date = threeToOne(day, month, year);
- // long test = threeToOne(day, month, year);
- // int d = 0, m = 0, y = 0;
- // std::cout << test << std::endl;
- // oneToThree(test, d, m, y);
- // std::cout << d << ":" << m << ":" << y;
- }
- Date operator+(Date &d) {
- return Date(this->date + d.date -366);
- }
- friend std::ostream &operator<<(std::ostream &os, Date &date) {
- int day = 0;
- int month = 0;
- int year = 0;
- date.oneToThree(date.date, day, month, year);
- os << day << ":" << month << ":" << year;
- return os;
- }
- };
- #endif //PREPAREFROMEXAMS_TEST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement