Leap year Test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.