Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Solution {
- public:
- string dayOfTheWeek(int day, int month, int year) {
- string names[7] { "Niedziela", "Poniedziałek", "Wtorek", "Środa", "Czwartek", "Piątek", "Sobota"};
- int total = 0;
- if (isLeap(year) == false && month == 2 && day == 29) {
- return "Błędna data. Podany rok nie był rokiem przestępnym.";
- }
- for (int i = 1970; i < year; ++ i) {
- if (isLeap(i)) {
- total += 366;
- } else {
- total += 365;
- }
- }
- for (int i = 1; i < month; ++ i) {
- switch (i) {
- case 1: total += 31; break;
- case 2: total += (isLeap(year) ? 29 : 28); break;
- case 3: total += 31; break;
- case 4: total += 30; break;
- case 5: total += 31; break;
- case 6: total += 30; break;
- case 7: total += 31; break;
- case 8: total += 31; break;
- case 9: total += 30; break;
- case 10: total += 31; break;
- case 11: total += 30; break;
- }
- }
- return names[(4 + total + day - 1) % 7];
- }
- private:
- bool isLeap(int year) {
- if (year % 400 == 0) return true;
- if (year % 100 == 0) return false;
- if (year % 4 == 0) return true;
- return false;
- }
- };
- int main()
- {
- Solution test;
- string day;
- int d;
- int m;
- int y;
- cout<<"Podaj dzień:";
- cin>>d;
- cout<<"Podaj miesiąc:";
- cin>>m;
- cout<<"Podaj rok:";
- cin>>y;
- day = test.dayOfTheWeek(d, m, y);
- cout<<day;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement