题目要求:Program 2
Write the program date_check which should check if a date is valid.
Requirements:
The program should get a date from the user in the format day/month/year. Tip: the function scanf can be used to input a formatted date string using a format like "%d/%d/%d".
The program should check the three parts of the date (day, month, year), and print a warning message for any part of the date that is wrong. If the whole date is correct, it should print a message saying that the date is valid.
The normal rules for dates apply: valid days are from 1 to 28, 29, 30, or 31 (depending on the month and on whether the year is a leap year); valid months are from 1 to 12; valid years start from 1582 (the year in which our current calendar system started).
The rule for deciding if a year is a leap year is as follows: a year evenly divisble by 4 is a leap year; except if the year is evenly divisible by 100, in which case it is not a leap year; except if the year is evenly divisible by 400, in which case it is a leap year.
Examples: (user input is shown in bold)
One invalid part of the date:
Enter a date (d/m/y): 29/2/2002
The day 29 is not valid!
Several invalid parts of the date:
Enter a date (d/m/y): 32/15/1002
The year 1002 is not valid!
The month 15 is not valid!
The day 32 is not valid!
Valid date:
Enter a date (d/m/y): 20/9/2002
The given date is valid.
程序:
/* Judge whether a date is valid */
#include <stdio.h>
int main()
{
/* Define the data type for each variable */
int DAY;
int MONTH;
int YEAR;
/* Display the requirement and get the data from the user */
printf(" Enter a date(d/m/y):");
scanf("%d/%d/%d", &DAY, &MONTH, &YEAR);
/* Begin the selection */
/* First Judge wheter it's a leap year Then beging the first selection*/
if(((YEAR>=1582)&&(YEAR % 400!=0)||((YEAR%100 !=0)&&(YEAR % 4 !=0)))&&
(((MONTH==1)&&(DAY>=1&&DAY<=31))||((MONTH==2)&&(DAY>=1&&DAY<=28))||
((MONTH==3)&&(DAY>=1&&DAY<=31))||((MONTH==4)&&(DAY>=1&&DAY<=30))||
((MONTH==5)&&(DAY>=1&&DAY<=31))||((MONTH==6)&&(DAY>=1&&DAY<=30))||
((MONTH==7)&&(DAY>=1&&DAY<=31))||((MONTH==8)&&(DAY>=1&&DAY<=31))||
((MONTH==9)&&(DAY>=1&&DAY<=30))||((MONTH==10)&&(DAY>=1&&DAY<=31))||
((MONTH==11)&&(DAY>=1&&DAY<=30))||((MONTH==12)&&(DAY>=1&&DAY<=31))))
{
printf(" The given date is valid \n");
}
else if(YEAR<1582)
{
printf(" The year %d is not valid.\n" ,YEAR);
}
if(MONTH<1||MONTH>12)
{
printf(" The month %d is not valid.\n", MONTH);
}
if(((YEAR>=1582)&&(YEAR % 400!=0)||((YEAR%100 !=0)&&(YEAR % 4 !=0))||(YEAR<1582))&&
(((MONTH==1)&&(DAY<1&&DAY>31))||((MONTH==2)&&(DAY<1&&DAY>28))||
((MONTH==3)&&(DAY<1&&DAY>31))||((MONTH==4)&&(DAY<1&&DAY>30))||
((MONTH==5)&&(DAY<1&&DAY>31))||((MONTH==6)&&(DAY<1&&DAY>30))||
((MONTH==7)&&(DAY<1&&DAY>31))||((MONTH==8)&&(DAY<1&&DAY>31))||
((MONTH==9)&&(DAY<1&&DAY>30))||((MONTH==10)&&(DAY<1&&DAY>31))||
((MONTH==11)&&(DAY<1&&DAY>30))||((MONTH==12)&&(DAY<1&&DAY>31))))
{
printf(" The day %d is not valid.\n" , DAY);
}
/* Then if it is not a leap year Then beging the second selection*/
else if
(((YEAR>=1582)&&(YEAR % 400==0)||((YEAR%100 !=0)&&(YEAR % 4 ==0)))&&
(((MONTH==1)&&(DAY>=1&&DAY<=31))||((MONTH==2)&&(DAY>=1&&DAY<=28))||
((MONTH==3)&&(DAY>=1&&DAY<=31))||((MONTH==4)&&(DAY>=1&&DAY<=30))||
((MONTH==5)&&(DAY>=1&&DAY<=31))||((MONTH==6)&&(DAY>=1&&DAY<=30))||
((MONTH==7)&&(DAY>=1&&DAY<=31))||((MONTH==8)&&(DAY>=1&&DAY<=31))||
((MONTH==9)&&(DAY>=1&&DAY<=30))||((MONTH==10)&&(DAY>=1&&DAY<=31))||
((MONTH==11)&&(DAY>=1&&DAY<=30))||((MONTH==12)&&(DAY>=1&&DAY<=31))))
{
printf(" The given date is valid\n");
}
else if(YEAR<1582)
{
printf(" The year %d is not valid.\n" ,YEAR);
}
if(MONTH<1||MONTH>12)
{
printf(" The month %d is not valid.\n", MONTH);
}
if(((YEAR>=1582)&&(YEAR % 400==0)||((YEAR%100 !=0)&&(YEAR % 4 ==0))||(YEAR<1582))&&
(((MONTH==1)&&(DAY<1&&DAY>31))||((MONTH==2)&&(DAY<1&&DAY>29))||
((MONTH==3)&&(DAY<1&&DAY>31))||((MONTH==4)&&(DAY<1&&DAY>30))||
((MONTH==5)&&(DAY<1&&DAY>31))||((MONTH==6)&&(DAY<1&&DAY>30))||
((MONTH==7)&&(DAY<1&&DAY>31))||((MONTH==8)&&(DAY<1&&DAY>31))||
((MONTH==9)&&(DAY<1&&DAY>30))||((MONTH==10)&&(DAY<1&&DAY>31))||
((MONTH==11)&&(DAY<1&&DAY>30))||((MONTH==12)&&(DAY<1&&DAY>31))))
{
printf(" The day %d is not valid.\n", DAY);
}
return 0;
}