/***********************************************************
* 功能: 此程序只用于检查用户输入8位日期合法性 *
* 函数: int DateVerify() *
* 入口: char *date *
* 出口: Succees: return 0 *
* Faild : return -1 *
* 作者: LinXi *
* 日期: 2002-09-25 *
***********************************************************/
#include
#include
int DateVerify(char *date);
int main()
{
char inputDate[20];
int ret=0;
printf("\nPlease input the date:");
gets(inputDate);
ret=DateVerify(inputDate);
switch (ret)
{
case -1:
printf("[E]: date is faild\n");
return -1;
break;
case 0:
printf("Success\n");
break;
}
return 0;
}
int DateVerify(char *date)
{
int i=0;
int year=0;
int month=0;
int day=0;
/*检查字符长度*/
if (strlen(date)!=8)
return -1;
/*检查字符为数字型*/
for (i=0;i<8;i++)
{
if (date[i]<'0' || date[i]>'9')
return -1;
}
/*按长度取值:YYYYMMDD*/
sscanf(date,"%4d%2d%2d",&year,&month,&day);
/*检查年份*/
if (year<1000 || year>=3000)
return -1;
/*检查月份*/
if (month<1 || month>12)
return -1;
/*检查日期*/
if (day<1 || day>31)
return -1;
if (month==4 || month==6 || month==9 || month==11)
{
if (day>30)
return -1;
}
if (month==2)
{
if ((year%4==0 && year%100!=0) || (year%400==0))
{
if (day>29)
return -1;
}
else
{
if (day>28)
return -1;
}
}
printf("\ndate is: %s",date);
return 0;
}
阅读(874) | 评论(0) | 转发(0) |