Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.     int g, m, a; // declare 3 ints for day, month, year
  9.     cout << "Immetti la data: ";
  10.     cin >> g >> m >> a; // take g, m, a from stdin
  11.    
  12.     // must be divisible by 4 and, if divisible by 100, it must also be divisible by 400
  13.     bool isLeapYr = a % 4 == 0 && (a % 100 != 0 || (a % 100 == 0 && a % 400 == 0));
  14.  
  15.     bool isValidYear = a > 0; // leap year algorithm does not work for years BC
  16.  
  17.     bool isValidMonth = m > 0 && m < 13;
  18.  
  19.     /*
  20.         Thirty days hath September
  21.         April, June and November;
  22.         February has twenty eight alone
  23.         All the rest have thirty-one
  24.         Except in Leap Year, that's the time
  25.         When February's Days are twenty-nine
  26.     */
  27.     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)));
  28.  
  29.     if (isValidYear && isValidMonth && isValidDay) cout << "Data corretta" << endl;
  30.     else cout << "Data non valida" << endl;
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement