Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int g, m, a; // declare 3 ints for day, month, year
- cout << "Immetti la data: ";
- cin >> g >> m >> a; // take g, m, a from stdin
- // must be divisible by 4 and, if divisible by 100, it must also be divisible by 400
- bool isLeapYr = a % 4 == 0 && (a % 100 != 0 || (a % 100 == 0 && a % 400 == 0));
- bool isValidYear = a > 0; // leap year algorithm does not work for years BC
- bool isValidMonth = m > 0 && m < 13;
- /*
- Thirty days hath September
- April, June and November;
- February has twenty eight alone
- All the rest have thirty-one
- Except in Leap Year, that's the time
- When February's Days are twenty-nine
- */
- bool isValidDay = (g > 0 && (g < 29 || (g < 32 && m != 2 && m != 11 && m != 4 && m != 6 && m != 9) || (g < 31 && m != 2) || (g < 30 && isLeapYr)));
- if (isValidYear && isValidMonth && isValidDay) cout << "Data corretta" << endl;
- else cout << "Data non valida" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement