SHARE
TWEET

Untitled

a guest Feb 24th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <stdexcept>
  5.  
  6. using namespace std;
  7.  
  8. bool leapYear(int year) {
  9.     return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
  10. }
  11.  
  12. class Date {
  13. private:
  14.     static constexpr int dayMonths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  15.     int year, month, day;
  16. public:
  17.     Date() :year(-1), month(-1), day(-1) {}
  18.     Date(int year, int month, int day) :year(year), month(month), day(day) {}
  19.  
  20.     friend Date& operator ++(Date& d);
  21.     friend Date operator ++(Date& d, int);
  22.  
  23.     friend ostream& operator <<(ostream& out, Date& d);
  24. };
  25.  
  26. Date& operator ++(Date& d) {
  27.     d.day++;
  28.     if (d.day == d.dayMonths[d.month - 1] + 1 + (d.month == 2 && leapYear(d.year))) {
  29.         d.day = 1;
  30.         d.month++;
  31.         if (d.month == 13) {
  32.             d.month = 1;
  33.             d.year++;
  34.         }
  35.     }
  36.     return d;
  37. }
  38.  
  39. Date operator ++(Date& d, int) {
  40.     Date old = d;
  41.     ++d;
  42.     return old;
  43. }
  44.  
  45. ostream& operator <<(ostream& out, Date& d) {
  46.     out << d.day << "." << d.month << "." << d.year << endl;
  47.     return out;
  48. }
  49.  
  50. int main() {
  51.     Date d(2019, 12, 30);
  52.     cout << d;
  53.     Date d1 = d++;
  54.     cout << d1 << d;
  55.     ++d;
  56.     cout << d;
  57. }
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