C语言:判断输入的一个日期的正确性

要求:接收键盘输入的一个日期,若日期正确则输出“right date”,否则输出“wrong date”。年份大于0,2月份的天数应考虑闰年/非闰年,其他月份的天数应区分30天还是31天。

用SWITH对月份进行选择 然后每个月份下都用IF做个判断 然后输出 最后再对2单独进行判断 判断闰年的算法是year%400==0||(year%4==0&&year%100!=0)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-15
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int iY,iM,iD;
int flag=0;
char sY[5];
char sM[3];
char sD[3];
char s[9];
int a[2][12]={31,28,31,30,31,30,31,31,30,31,30,31,
31,29,31,30,31,30,31,31,30,31,30,31};
memset(sY,0,5);
memset(sM,0,3);
memset(sD,0,3);
memset(s,0,9);
clrscr();
scanf("%s",s);
strncpy(sY,s,4);
iY=atoi(sY);
strncpy(sM,&s[4],2);
iM=atoi(sM);
strncpy(sD,&s[6],2);
iD=atoi(sD);
if(iY<0||iY>9999)
{
printf("wrong date !\n");
return 1;
}
if((iY%4==0&&iY%100!=0)||(iY%100==0&&iY%400==0))
flag=1;
if(iM<0||iM>12||iD>a[flag][iM-1]||iD<0)
{
printf("wrong date !\n");
return 1;
}
else
printf("right date!\n");
return 0;
}本回答被网友采纳
相似回答