
给定的日期格式为日期、月份和年份(整数)。任务是确定该日期是否可行。
有效日期范围应为 1/1/1800 – 31/12/9999,超出这些日期的日期无效。
这些日期不仅包含年份范围,还包含与日历日期相关的所有约束。
约束是 –
日期不能是小于 1 且大于 31月份不能小于 1 且大于 12年份不能小于 1800 且大于 9999当月份为四月、六月、九月、十一月时,日期不能超过 30。当月份为二月时,我们必须检查是否,如果年份是闰年则天数不能超过29天否则天数不能超过28天。 li>
如果所有约束都为真,则它是有效日期,否则不是。
示例
Input: y = 2002 d = 29 m = 11Output: Date is validInput: y = 2001 d = 29 m = 2Output: Date is not valid
算法
STARTIn function int isleap(int y) Step 1-> If (y % 4 == 0) && (y % 100 != 0) && (y % 400 == 0) then, Return 1 Step 2-> Else Return 0In function int datevalid(int d, int m, int y) Step 1-> If y max_yr then, Return 0 Step 2-> If m 12 then, Return 0 Step 3-> If d 31 then, Return 0 Step 4-> If m == 2 then, If isleap(y) then, If d If m == 4 || m == 6 || m == 9 || m == 11 then, If(d Assign and initialize values as y = 2002, d = 29, m = 11 Step 2-> If datevalid(d, m, y) then, Print "Date is valid" Step 3-> Else Print "date is not valid” End mainSTOP
示例
实时演示
#include #define max_yr 9999#define min_yr 1800//to check the year is leap or not//if the year is a leap year return 1int isleap(int y) { if((y % 4 == 0) && (y % 100 != 0) && (y % 400 == 0)) return 1; else return 0;}//Function to check the date is valid or notint datevalid(int d, int m, int y) { if(y max_yr) return 0; if(m 12) return 0; if(d 31) return 0; //Now we will check date according to month if( m == 2 ) { if(isleap(y)) { if(d <= 29) return 1; else return 0; } } //April, June, September and November are with 30 days if ( m == 4 || m == 6 || m == 9 || m == 11 ) if(d <= 30) return 1; else return 0; return 1; }int main(int argc, char const *argv[]) { int y = 2002; int d = 29; int m = 11; if(datevalid(d, m, y)) printf("Date is valid"); else printf("date is not valid
"); return 0;}
输出
如果运行上面的代码,它将生成以下输出 –
Date is valid
以上就是C程序检查日期是否有效的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445516.html
微信扫一扫
支付宝扫一扫