Vladislav_Bezruk

Untitled

Oct 5th, 2021 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include<cmath>
  3. using namespace std;
  4. class Date {
  5. int year, month, day;
  6. public:
  7. Date() {}
  8. Date(int a, int b, int c) : year(a), month(b), day(c) {}
  9. Date operator + (const Date &a) {Date c(this -> year + a.year , this -> month + a.month ,this -> day + a.day ); c.norm(); return c; }
  10. Date operator + (const int &a) {Date c(this -> year, this -> month, this -> day + a); c.norm(); return c; }
  11. Date operator - (const Date &a) {Date c(this -> year - a.year , this -> month - a.month ,this -> day - a.day ); c.norm(); return c; }
  12. friend Date operator ++ (Date &a);
  13. friend Date operator ++ (Date &a, int);
  14. Date & operator = (const Date &a) {
  15. year = a.year;
  16. month = a.month;
  17. day = a.day;
  18. return *this;
  19. }
  20. Date & operator = (const int a) {
  21. year = month = day = a;
  22. return *this;
  23. }
  24. void set(char k) {
  25. cout << "Please, enter YYYY MM DD " << k << ": ";
  26. cin >> year >> month >> day;
  27. return;
  28. }
  29. void get(char k) {
  30. cout << " Date " << k << ": " << year << " " << month << " " << day << endl;
  31. return;
  32. }
  33. int getMaxDay(int month, int year) {
  34. if (month == 2) {
  35. if (isLeapYear(year)) {
  36. return 29;
  37. }
  38. return 28;
  39. }
  40. if (month % 2 == 0) {
  41. if (month < 7) return 30;
  42. return 31;
  43. }
  44. if (month < 8) return 31;
  45. return 30;
  46. }
  47. bool isLeapYear(int year) {
  48. return (!(year % 4) && year % 100 || !(year % 400));
  49. }
  50. void norm() {
  51. if (day < 1) {
  52. day += getMaxDay(month - 1, year);
  53. month--;
  54. }
  55. if (month < 1) {
  56. year--;
  57. month += 12;
  58. }
  59. if (day > getMaxDay(month, year)) {
  60. day -= getMaxDay(month, year);
  61. month++;
  62. }
  63. if (month > 12) {
  64. month -= 12;
  65. year++;
  66. }
  67. return;
  68. }
  69. };
  70.  
  71. int main() {
  72.  
  73. Date a, b, c;
  74.  
  75. a.set('a');
  76. b.set('b');
  77.  
  78. cout << endl << " operator + " << endl;
  79. c = a + b;
  80. cout << "c = a + b" << endl;
  81. c.get('c');
  82.  
  83. c = a + 30;
  84. cout << "c = a + 30" << endl;
  85. c.get('c');
  86.  
  87. cout << endl << " operator - " << endl;
  88. c = a - b;
  89. cout << "c = a - b" << endl;
  90. c.get('c');
  91.  
  92. cout << endl << " operator ++ " << endl;
  93. c = ++a;
  94. cout << "c = ++a " << endl;
  95. c.get('c');
  96. a.get('a');
  97.  
  98. c = a++;
  99. cout << "c = a++ " << endl;
  100. c.get('c');
  101. a.get('a');
  102.  
  103. return 0;
  104. }
  105.  
  106. Date operator ++ (Date &a) {
  107. a.day = a.day + 1;
  108. a.norm();
  109. return a;
  110. }
  111. Date operator ++ (Date &a, int){
  112. Date _a = a;
  113. a.day = a.day + 1;
  114. a.norm();
  115. return _a;
  116. }
  117.  
  118.  
Add Comment
Please, Sign In to add comment