c语言中怎样判断输入的日期是否合法要求用结构体并且能判断例如2000-3-21中的每一部分是否合法(包括-)

其中要判断年份是否为闰年,再根据年份判断输入的天数是否合法

struct Date
{
int year;
int month;
int day;
char ch;
};

void main()
{
Date date;
printf("please input date\n");
scanf("%d%c%d%c%d",&date.year,&date.ch,&date.month, &date.ch,&date.day);
if(ch != '-' || month > 12 || month < 1 ||day < 0 || day > n) n的值根据年月判断
{
printf("error\n");
}

再给你一个判断闰年的程序,自己试着组合下
#include<stdio.h>

void main()
{
int year;
printf("please input year :\n");
scanf("%d",&year);
if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
{
printf("yes!\n");
}
else
printf("no!");
}追问

多谢了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-14
用if语句判断嘛。在输入的时候先判断在存入。
比如输入年,然后判断是否是闰年存入结构体,然后输入月,判断是否是2月true存入结构踢。然后输入天判断是否合法true存入结构体。
判断方式用if语句写一个判断天的吧
if(闰年==true&&月==2)

if(天<=28)
存入
相似回答