Advertisement
35657

Untitled

Mar 9th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool check_year(int year) {
  6. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  7. return true;
  8. }
  9. return false;
  10. }
  11.  
  12. int calc_days_in_year(int arr[], int d1, int m1, int d2, int m2, int year) {
  13. if (m1 == m2) { // если обе даты относятся к одному месяцу
  14. return d2 - d1;
  15. }
  16. int count = 0;
  17. count += arr[m1 - 1] - d1; // считаем количество дней до конца первого месяца
  18. for (int i = m1; i < m2 - 1; i++) { // количество дней в полных месяцах
  19. count += arr[i];
  20. }
  21. count += d2; // количество дней с начала второго месяца
  22.  
  23. if (check_year(year) && m1 <= 2) {
  24. count += 1;
  25. }
  26. return count;
  27.  
  28. }
  29.  
  30. int calc_days(int d1, int m1, int y1, int d2, int m2, int y2) {
  31. int arr[12]{ 31,28,31,30,31,30,31,31,30,31,30,31 };
  32.  
  33. if (y1 == y2) {
  34. return calc_days_in_year(arr, d1, m1, d2, m2, y1);
  35. }
  36. else {
  37. int count = 0;
  38. count += calc_days_in_year(arr, d1, m1, 31, 12, y1);
  39. for (int i = y1 + 1; i < y2; i++) {
  40. check_year(i) ? count += 366 : count += 365;
  41. }
  42. count += calc_days_in_year(arr, 1, 1, d2, m2, y2);
  43. count += 1; // теряется переход с 31.12 на 01.01
  44. return count;
  45. }
  46.  
  47. }
  48.  
  49. int main() {
  50.  
  51. setlocale(LC_ALL, "ru");
  52.  
  53. cout << calc_days(1, 3, 1999, 14, 7, 2023) << endl; // 8901
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement