Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7.     string dayOfTheWeek(int day, int month, int year) {
  8.         string names[7] { "Niedziela", "Poniedziałek", "Wtorek", "Środa", "Czwartek", "Piątek", "Sobota"};
  9.         int total = 0;
  10.         if (isLeap(year) == false && month == 2 && day == 29) {
  11.             return "Błędna data. Podany rok nie był rokiem przestępnym.";
  12.         }
  13.         for (int i = 1970; i < year; ++ i) {
  14.             if (isLeap(i)) {
  15.                 total += 366;
  16.             } else {
  17.                 total += 365;
  18.             }
  19.         }
  20.         for (int i = 1; i < month; ++ i) {
  21.             switch (i) {
  22.                 case 1: total += 31; break;
  23.                 case 2: total += (isLeap(year) ? 29 : 28); break;
  24.                 case 3: total += 31; break;
  25.                 case 4: total += 30; break;
  26.                 case 5: total += 31; break;
  27.                 case 6: total += 30; break;
  28.                 case 7: total += 31; break;
  29.                 case 8: total += 31; break;
  30.                 case 9: total += 30; break;
  31.                 case 10: total += 31; break;
  32.                 case 11: total += 30; break;
  33.             }
  34.         }
  35.         return names[(4 + total + day - 1) % 7];
  36.     }
  37. private:
  38.     bool isLeap(int year) {
  39.         if (year % 400 == 0) return true;
  40.         if (year % 100 == 0) return false;
  41.         if (year % 4 == 0) return true;
  42.         return false;
  43.     }    
  44. };
  45.  
  46. int main()
  47. {
  48.     Solution test;
  49.     string day;
  50.     int d;
  51.     int m;
  52.     int y;
  53.    
  54.     cout<<"Podaj dzień:";
  55.     cin>>d;
  56.     cout<<"Podaj miesiąc:";
  57.     cin>>m;
  58.     cout<<"Podaj rok:";
  59.     cin>>y;
  60.    
  61.     day = test.dayOfTheWeek(d, m, y);
  62.    
  63.     cout<<day;
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement