Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <iostream>
  2. class Date{
  3. private:
  4. bool leapyear;
  5. int day;
  6. int month;
  7. int year;
  8. bool leap(int)const;
  9. int maxday()const;
  10. public:
  11. Date(int d,int m,int y):leapyear(leap(y)),day(d),month(m),year(y){};
  12. Date& operator+=(int);
  13. Date& operator-=(int);
  14. std::string operator-(Date&)const;
  15. bool operator==(Date&)const;
  16. friend std::ostream& operator<<(std::ostream&, const Date&);
  17. };
  18. bool Date::leap(int y)const{
  19. return ((y % 4 == 0 && y% 100 != 0) || y % 400);
  20. }
  21. int Date::maxday()const{
  22. int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  23. if(leapyear)days[1]=29;
  24. return days[month-1];
  25. }
  26. Date& Date::operator+=(int d){
  27. if(day+d>maxday()){
  28. d-=maxday()-day;
  29. if(++month>12){
  30. month=1;
  31. year++;
  32. }
  33. while(d/maxday()){
  34. if(++month>12){
  35. month=1;
  36. year++;
  37. }
  38. d-=maxday();
  39. }
  40. day=d;
  41. }
  42. else day+=d;
  43. return *this;
  44. }
  45. Date& Date::operator-=(int d){
  46. if(day-d<1){
  47. d-=day;
  48. if(--month==0){
  49. month=12;
  50. year--;
  51. }
  52. while(d/maxday()){
  53. d-=maxday();
  54. if(--month==0){
  55. month=12;
  56. year--;
  57. }
  58. }
  59. day=maxday()-d;
  60. }
  61. else day-=d;
  62. return *this;
  63. }
  64. #include <sstream> //stringstream
  65. std::string Date::operator-(Date &dt)const{
  66. int d,m,y;
  67. if(year>=dt.year){
  68. y=year-dt.year;
  69. if(month>dt.month){
  70. m=month-dt.month;
  71. d=day+dt.maxday()-dt.day;
  72. if(d>dt.maxday())d-=dt.maxday();
  73. else --m;
  74. --y;
  75. }
  76. else {
  77. m=12-month+dt.month;
  78. d=dt.day+maxday()-day;
  79. if(d>maxday())d-=maxday();
  80. else --m;
  81. }
  82. }
  83. else {
  84. y=dt.year-year;
  85. if(month>dt.month){
  86. m=12-month+dt.month;
  87. d=day+dt.maxday()-dt.day;
  88. if(d>dt.maxday())d-=dt.maxday();
  89. else --m;
  90. --y;
  91. }
  92. else {
  93. m=dt.month-month;
  94. d=maxday()-day+dt.day;
  95. if(d>maxday())d-=maxday();
  96. else --m;
  97. }
  98. }
  99. std::string s;
  100. std::stringstream ss;
  101. ss<<d<<" days "<<m<<" month "<<y<<" years";
  102. getline(ss,s);
  103. return s;
  104. }
  105. bool Date::operator==(Date& dt)const{
  106. return day==dt.day && month==dt.month && year==dt.year;
  107. }
  108. std::ostream& operator<<(std::ostream &os, const Date &dt){
  109. std::string m[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
  110. os<<dt.day<<" "<<m[dt.month-1]<<" "<<dt.year;
  111. return os;
  112. }
  113.  
  114. int main(){
  115. int d,m,y,reduce,increase;
  116. std::cout<<"Enter first date (day month year): ";
  117. std::cin>>d>>m>>y;
  118. Date date(d,m,y);
  119. std::cout<<"Date = "<<date<<std::endl;
  120. std::cout<<"reduce date by : ";//уменьшить дату на столько то дней
  121. std::cin>>reduce;
  122. date-=reduce;
  123. std::cout<<"Date = "<<date<<std::endl;
  124. std::cout<<"Increase date by : ";//увеличить дату
  125. std::cin>>increase;
  126. date+=increase;
  127. std::cout<<"Date = "<<date<<std::endl;
  128. std::cout<<"Enter second date (day month year): ";
  129. std::cin>>d>>m>>y;
  130. Date date2(d,m,y);
  131. std::cout<<"Date2 = "<<date2<<std::endl;
  132. std::cout<<"Dates are "<<(date==date2 ? "equal":"not equal")<<std::endl;
  133. std::cout<<"Date1 - Date2 = "<<date-date2<<std::endl;
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement