Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class date
  4. {
  5. int day;
  6. int month;
  7. int year;
  8. public:
  9. date()
  10. {
  11. cout << "enter the day" << endl;
  12. cin >> day;
  13. cout << "enter the month" << endl;
  14. cin >> month;
  15. cout << "enter the year" << endl;
  16. cin >> year;
  17. }
  18. date(int a, int b, int c) : day(a), month(b), year(c)
  19. {
  20.  
  21. }
  22. date(const date &prod)
  23. {
  24. this->day = prod.day, this->month = prod.month, this->year = prod.year;
  25. }
  26.  
  27. date & operator+=( int d);
  28. date & operator-=( int d);
  29. date & operator =(const date&);
  30. int countdays();
  31. date & operator+=( date&);
  32. date & operator -=(date&);
  33. friend ostream& operator<<(ostream& os, date&);
  34. friend istream& operator >> (istream& is, date& prod);
  35. ~date() { }
  36.  
  37. };
  38. date & date :: operator = (const date &prod1)
  39. {
  40. this->day = prod1.day, this->month =prod1.month, this->year = prod1.year;
  41. return *this;
  42. }
  43.  
  44. int main()
  45. {
  46. date prod, prod1;
  47. prod = prod1;
  48. cout <<"prod = "<< prod;
  49. cout << endl;
  50. cout << "prod1 = "<<prod1;
  51. cout << "enter the amount of days you want to add" << endl;
  52. int amount; cin >> amount;
  53. prod += amount;
  54. cout <<"prod +"<<amount<<" days =" << prod;
  55. prod -= amount;
  56. cout << "prod -" << amount << " days =" << prod;
  57. prod += prod1;
  58. cout << "prod += prod1=" <<prod;
  59. prod -= prod1;
  60. cout <<"prod -= prod1 = "<< prod;
  61. system("pause");
  62. return 0;
  63. }
  64. date & date :: operator+=( date& prod1)
  65. {
  66. int k = prod1.countdays();
  67. *this += k;
  68. return *this;
  69. }
  70. ostream& operator<<(ostream& os, date& prod)
  71. {
  72. os << prod.day << "." << prod.month << "." << prod.year << endl;
  73. return os;
  74. }
  75. istream & operator >> (istream & is, date&prod)
  76. {
  77. cout << "Ente day,month,year " << endl;
  78. is >> prod.day;
  79. is >> prod.month;
  80. is >> prod.year;
  81. return is ;
  82. }
  83. date & date :: operator-=(int d)
  84. {
  85. int c = day, count = 0;
  86. day -= d;
  87. int i = 0;
  88. if (year % 4 == 0 && (((year % 100 != 0 || year % 400 == 0)))&& month>2)i++;
  89. while (d > 0)
  90. {
  91.  
  92. if (count != 0)
  93. {
  94. c = day; day -= d;
  95. }
  96. count++;
  97. switch (month-1)
  98. {
  99. case 0:
  100. case 1:
  101. case 3:
  102. case 5:
  103. case 7:
  104. case 8:
  105. case 10:
  106. case 12:
  107. {
  108. if (day < 1)
  109. {
  110. d -= c;
  111. day = 31;
  112. month -= 1;
  113. if (month < 1)
  114. {
  115. month = 12;
  116. year -= 1;
  117. if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))i++;
  118. if (year == 0) { cout << "Even Jesus Christ was born later..." << endl; d = 0; break; }
  119. }
  120. }
  121. else d = 0;
  122. break;
  123. }
  124. case 4:
  125. case 6:
  126. case 9:
  127. case 11:
  128. {
  129. if (day < 1)
  130. {
  131. d -= c;
  132. day = 30;
  133. month -= 1;
  134. }
  135. else d = 0;
  136. break;
  137. }
  138. case 2:
  139. {
  140. if (day < 1)
  141. {
  142. d -= c;
  143. day = 28+i;
  144. month -= 1;
  145. if(i==1) i = 0;
  146. }
  147. else d = 0;
  148. break;
  149. }
  150.  
  151. }
  152. }
  153. return *this;
  154. }
  155. date& date :: operator+=(int d)
  156. {
  157. int c = day, count = 0;
  158. day += d;
  159. int i = 0;
  160. if (((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) && month <= 2)i++;
  161. while (d > 0)
  162. {
  163. if (count != 0) {
  164. c = day; day += d;
  165. }
  166. count++;
  167. switch (month)
  168. {
  169. case 1:
  170. case 3:
  171. case 5:
  172. case 7:
  173. case 8:
  174. case 10:
  175. case 12:
  176. {
  177. if (day > 31)
  178. {
  179. d -= 32 - c;
  180. day = 1;
  181. month += 1;
  182. if (month > 12)
  183. {
  184. month = 1;
  185. year += 1;
  186. if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))i++;
  187. }
  188. }
  189. else d = 0;
  190. break;
  191. }
  192. case 4:
  193. case 6:
  194. case 9:
  195. case 11:
  196. {
  197. if (day > 30)
  198. {
  199. d -= 31 - c;
  200. day = 1;
  201. month += 1;
  202. }
  203. else d = 0;
  204. break;
  205. }
  206. case 2:
  207. {
  208. if (day > 28 + i)
  209. {
  210. d -= 29 + i - c;
  211. day = 1;
  212. month += 1;
  213. if(i==1) i = 0;
  214. }
  215. else d = 0;
  216. break;
  217. }
  218.  
  219. }
  220. }
  221. return *this;
  222. }
  223. int date :: countdays()
  224. {
  225. int s = 0; int i = 0; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)i++;
  226. switch (month)
  227. {
  228. case 1: s = 0; break;
  229. case 2: s = 31; break;
  230. case 3: s = 59 + i; break;
  231. case 4: s = 90; break;
  232. case 5: s = 120; break;
  233. case 6: s = 151; break;
  234. case 7: s = 181; break;
  235. case 8: s = 212; break;
  236. case 9: s = 243; break;
  237. case 10: s = 273; break;
  238. case 11: s = 304; break;
  239. case 12: s = 334; break;
  240. default: return 1;
  241. }
  242. s = s + day + (365.25 *year) - i;
  243. return s;
  244. }
  245. date & date::operator -=(date&prod)
  246. {
  247. int p = prod.countdays();
  248. int t = this->countdays();
  249. if (t < p)
  250. cout << "Even Jesus Christ was born later..." << endl;
  251. else *this -= p;
  252. return *this;
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement