OJ click to judge whether the date (year, month, and day) is valid
Description
Compile the isValid_date function. The function declaration is as follows:
Int isValid_date (int year, int month, int day); // function declaration that determines whether the date (year, month, and day) is valid. if the date is valid, 1 is returned. if the date is invalid, 0 is returned.
Add the isValid_date function definition on the basis of the following programs so that the program can be correctly executed.
When submitting, you only need to submit the definition code of the isValid_date function.
# Include <iostream>
Using namespace std;
Int isValid_date (int year, int month, int day); // function declaration that determines whether the date (year, month, and day) is valid
Int main ()
{
Int y, m, d; // defines three variables to store the input year, month, and day.
Cin> y> m> d;
Cout <y <"-" <m <"-" <d;
If (isValid_date (y, m, d) = 1)
Cout <"valid" <endl;
Else
Cout <"not valid" <endl;
Return 0;
}
Input
Enter three positive integers, which represent the year, month, and day of a date.
Output
Whether the date is correct, for example, whether the month is correct, and whether the month date is correct
Sample Input
2002 13 40
Sample output
2002-13-40 not valid
The Code is as follows:
# Include <iostream> using namespace std; int isValid_date (int year, int month, int day); // function declaration that determines whether the date (year, month, and day) is valid int main () {int y, m, d; // defines three variables to store the input year, month, and day cin> y> m> d; cout <y <"-" <m <"-" <d; if (isValid_date (y, m, d) = 1) cout <"valid" <endl; else cout <"not valid" <endl; return 0;} int isValid_date (int year, int month, int day) {int days, I; for (I = 1; I <= month; I ++) {if (I = 1 | I = 3 | I = 5 | I = 7 | I = 8 | I = 1 0 | I = 12) days = 31; else if (I = 4 | I = 6 | I = 9 | I = 11) days = 30; else if (year % 4 = 0 & year % 100! = 0) | year % 400 = 0) days = 29; else days = 28 ;} if (year >=0 & month >=1 & month <= 12 & day >=1 & day <= days) return 1; else return 0 ;}
Running result: