G2A Many GEOs
SHARE
TWEET

Untitled

a guest Apr 10th, 2020 144 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <sstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. unsigned int daysOfMonth1[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  8. unsigned int daysOfMonth2[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  9.  
  10. struct Date {
  11.     unsigned int year;
  12.     unsigned int month;
  13.     unsigned int day;
  14. };
  15.  
  16. void display(Date d) {
  17.     cout << "year: " << d.year << "; ";
  18.     cout << "month: " << d.month << "; ";
  19.     cout << "day: " << d.day << endl;
  20. }
  21.  
  22. void read(Date& d) {
  23.     cout << "Enter year" << endl;
  24.     cin >> d.year;
  25.  
  26.     cout << "Enter month" << endl;
  27.     cin >> d.month;
  28.  
  29.     cout << "Enter day" << endl;
  30.     cin >> d.day;
  31. }
  32.  
  33. void init(Date& d, unsigned int year, unsigned int month, unsigned int day) {
  34.     d.year = year;
  35.     d.month = month;
  36.     d.day = day;
  37. }
  38.  
  39. void init(Date& d, string date) { // 2020.03.31
  40.     stringstream str(date);
  41.     char symbol;
  42.     str >> d.year >> symbol >> d.month >> symbol >> d.day;
  43. }
  44.  
  45. string toString(Date d) {
  46.     stringstream str;
  47.     str << d.year << "." << setprecision(2) << d.month << "." << setprecision(2) << d.day;
  48.     return str.str();
  49. }
  50.  
  51. bool isLeapYear(Date& date) {
  52.     return (date.year % 4 == 0);
  53. }
  54.  
  55. void setYear(Date& date, unsigned int year) {
  56.     date.year = year;
  57. }
  58.  
  59. unsigned int getYear(Date& date) {
  60.     return date.year;
  61. }
  62.  
  63. void setMonth(Date& date, unsigned int month) {
  64.     date.month = month;
  65. }
  66.  
  67. unsigned int getMonth(Date& date) {
  68.     return date.month;
  69. }
  70.  
  71. void setDay(Date& date, unsigned int day) {
  72.     date.day = day;
  73. }
  74.  
  75. unsigned int getDay(Date& date) {
  76.     return date.day;
  77. }
  78.  
  79. bool isDateEquals(Date& date1, Date& date2) {
  80.     if (date1.year != date2.year) {
  81.         return false;
  82.     }
  83.  
  84.     if (date1.month != date2.month) {
  85.         return false;
  86.     }
  87.  
  88.     if (date1.day != date2.day) {
  89.         return false;
  90.     }
  91.  
  92.     return true;
  93. }
  94.  
  95. bool isDateGreate(Date& date1, Date& date2) {
  96.     if (date1.year < date2.year) {
  97.         return false;
  98.     }
  99.  
  100.     if (date1.year == date2.year) {
  101.         if (date1.month < date2.month) {
  102.             return false;
  103.         }
  104.  
  105.         if (date1.month == date2.month) {
  106.             if (date1.day <= date2.day) {
  107.                 return false;
  108.             }
  109.         }
  110.     }
  111.  
  112.     return true;
  113. }
  114.  
  115. bool isDateLess(Date& date1, Date& date2) {
  116.     if (date1.year > date2.year) {
  117.         return false;
  118.     }
  119.  
  120.     if (date1.year == date2.year) {
  121.         if (date1.month > date2.month) {
  122.             return false;
  123.         }
  124.  
  125.         if (date1.month == date2.month) {
  126.             if (date1.day >= date2.day) {
  127.                 return false;
  128.             }
  129.         }
  130.     }
  131.  
  132.     return true;
  133. }
  134.  
  135. unsigned int convertDateToDays(Date& date) {
  136.     unsigned int days = 0;
  137.  
  138.     for (int year = 0; year < date.year; year++) {
  139.         if (year % 4 == 0) days += 366;
  140.         else days += 365;
  141.     }
  142.  
  143.     for (int month = 1; month < date.month; month++) {
  144.         if (date.year % 4 == 0) days += daysOfMonth1[month];
  145.         else days += daysOfMonth2[month];
  146.     }
  147.  
  148.     days += date.day;
  149.  
  150.     return days;
  151. }
  152.  
  153. unsigned int getDaysBetweenDates(Date& date1, Date& date2) {
  154.     if (isDateEquals(date1, date2)) return 0;
  155.     int days = convertDateToDays(date1) - convertDateToDays(date2);
  156.  
  157.     return (days > 0) ? (days) : (-days);
  158. }
  159.  
  160. void addDay(Date& date, unsigned int days) {
  161.     if (days == 0) return;
  162.  
  163.     for (int i = 1; i <= days; i++) {
  164.         if (isLeapYear(date)) {
  165.             if (date.day > daysOfMonth1[date.month]) {
  166.                 date.day %= daysOfMonth1[date.month];
  167.  
  168.                 if (date.month == 12) {
  169.                     date.month = 1;
  170.                     date.year++;
  171.                 }
  172.                 else {
  173.                     date.month++;
  174.                 }
  175.             }
  176.         }
  177.         else if (date.day > daysOfMonth2[date.month]) {
  178.             date.day %= daysOfMonth2[date.month];
  179.  
  180.             if (date.month == 12) {
  181.                 date.month = 1;
  182.                 date.year++;
  183.             }
  184.             else {
  185.                 date.month++;
  186.             }
  187.         }
  188.  
  189.         date.day++;
  190.     }
  191. }
  192.  
  193. string isCompareDates(Date& date1, Date& date2) {
  194.     if (isDateEquals(date1, date2)) return "равны";
  195.     if (isDateGreate(date1, date2)) return "больше";
  196.     if (isDateGreate(date1, date2)) return "меньше";
  197.  
  198.     return "неизвестно";
  199. }
  200.  
  201. void subDay(Date& date, unsigned int days) {
  202.     if (days == 0) return;
  203.  
  204.     for (int i = 1; i <= days; i++) {
  205.         if (date.day == 1) {
  206.             if (date.month == 1) date.month = 12;
  207.             else date.month--;
  208.  
  209.             if (isLeapYear(date)) date.day = daysOfMonth1[date.month];
  210.             else date.day = daysOfMonth2[date.month];
  211.         }
  212.         else date.day--;
  213.     }
  214. }
  215.  
  216. int main() {
  217.     setlocale(LC_ALL, "rus");
  218.     Date a;
  219.     read(a);
  220.     display(a);
  221.  
  222.     Date b;
  223.     init(b, 2020, 31, 03);
  224.     display(b);
  225.  
  226.     Date c;
  227.     init(c, "2020.03.31");
  228.     display(c);
  229.  
  230.     addDay(a, 2);
  231.     display(a);
  232.  
  233.     subDay(b, 2);
  234.     display(b);
  235. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top