First, the procedure requirements
For the year entered, determine whether it is leap years.
is a leap year, the output "year is leap years", not a leap period, the output "year is not a yearly".
For illegal input, an error message is returned.
Second, test cases
Serial number |
Test Cases |
Expected output |
Actual output |
1 |
0 |
The year is leap years |
The year is leap years |
2 |
1999 |
The year is not run year |
The year is not run year |
3 |
2000 |
The year is leap years |
The year is leap years |
4 |
2008 |
The year is leap years |
The year is leap years |
Three, the key code
#include <cstdio>
#include <cstdlib>
int main ()
{
int year;
while (~SCANF ("%d", &year))
{
printf (((year% 4 = = 0 && Year% 100! = 0) | | year%400==0)? "The year is leap years \ n": "The Year is not a leap years \ n");
System ("pause");
}
return 0;
}
Iv. test Results
As can be seen from the results, for illegal characters, the program did not do any processing, resulting in errors in the output. Therefore, improvements to the program are used below.
Five, the improvement of illegal input detection
For string conversions:
In the C standard library, you can use the Atoi function.
Inside the C + + standard library, use the StringStream function.
In Java or C #, you can use the parse function.
Here is an example of the parse function in Java:
public static Boolean tryParse (String str) {
try{
Integer.parseint (str);
return true;
}
catch (NumberFormatException e) {
E.printstacktrice ();
return false;
]
}
The user enters the correct number, returns True, enters an illegal character, returns false, and throws a NumberFormatException exception.
This will achieve the function of judging the illegal input.
Leap year Test