这是我大学c班的一个练习,我似乎解决不了,有人帮我解决吗
编写一个程序,对于从SI读取的日期(格式为DD MM YYYY),如果日期正确且可能,则在标准输出上打印一条消息YES;如果日期不正确,则打印NO 当决定日期是否正确时,你必须考虑以下因素:这是我大学c班的一个练习,我似乎解决不了,有人帮我解决吗,c,C,编写一个程序,对于从SI读取的日期(格式为DD MM YYYY),如果日期正确且可能,则在标准输出上打印一条消息YES;如果日期不正确,则打印NO 当决定日期是否正确时,你必须考虑以下因素: 是一月和十二月之间的月份(1-12) 天数是否与指定月份的天数一致 如果月份是二月,那是闰年吗? 闰年是可以用400整除的年份,或者可以用4整除,但不能用100整除 #包括 int main() { 整数日、月、年; scanf(“%d%d%d”、&day、&month、&year); 对于(month
- 是一月和十二月之间的月份(1-12)
- 天数是否与指定月份的天数一致 如果月份是二月,那是闰年吗? 闰年是可以用400整除的年份,或者可以用4整除,但不能用100整除
#包括
int main()
{
整数日、月、年;
scanf(“%d%d%d”、&day、&month、&year);
对于(month=1;month您需要一个月内的天数数组,然后检查该月是否介于1和12之间。如果该月正好是2,则应首先检查闰年
大概是这样的:
int daysInMonth = {31, 28, ...}; //replace ... with actual information
if(month < 1 || month > 13) {
//month isn't valid
printf("NO");
return 0;
}
month -= 1; //to convert to 0-indexed
if(month == 1 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
//month is february and is leap year
daysInMonth[1] += 1; //february now has 29
}
if(day < 1 || day > daysInMonth[month]) {
//day isn't valid
printf("NO");
return 0;
}
//When getting to this point it means we have a valid date
printf("YES");
return 0;
int daysInMonth={31,28,…};//将…替换为实际信息
如果(月<1 | |月>13){
//月份无效
printf(“否”);
返回0;
}
月份-=1;//转换为0索引
如果(月份==1&&((年份%4==0&&year%100!=0)| |年份%400==0)){
//月份是二月,是闰年
daysInMonth[1]+=1;//2月份现在有29个
}
如果(日<1 | |日>日/月[月]){
//日期无效
printf(“否”);
返回0;
}
//当到达这一点,这意味着我们有一个有效的日期
printf(“是”);
返回0;
请注意,这不会处理一些非常特殊的边缘情况,解释道。这些情况包括由于从朱利安日历改为公历而不存在的日期。但我几乎可以肯定,这不是练习要求的。你可能还需要检查负年份,具体取决于有效年份的准确定义。正如其他人所提到的,您不需要循环[月份]
您应该通过索引到数组中来获得一个月的天数(vs.开关
)
添加一些额外的布尔标志可以帮助简化工作
无论如何,这里有一个重构版本,带有一些描述性注释:
// is the month between January and December (1-12)
// does the number for days correspond with the number of days in the specified
// month
// if the month is February, is the year leap?
// Leap years are those years who are divisible with 400, or they are
// divisible with 4, but not with 100
#include <stdio.h>
#include <stdlib.h>
int days_in_month[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 31, 31, 30, 31
};
int
main(int argc,char **argv)
{
int day, month, year;
int month_valid;
int leap_year;
int max_days;
int day_valid;
int date_valid;
--argc;
++argv;
if (argc == 3) {
month = atoi(argv[0]);
day = atoi(argv[1]);
year = atoi(argv[2]);
}
else {
if (0)
scanf("%d %d %d", &day, &month, &year);
else
scanf("%d %d %d", &month, &day, &year);
}
// is month in range?
month_valid = (month >= 1) && (month <= 12);
// is it a leap year?
leap_year = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
// get number of days in month [if month is valid]
if (month_valid)
max_days = days_in_month[month - 1];
else
max_days = -1;
// february in a leap year has an extra day
if (month == 2)
max_days += leap_year;
// is day of the month valid?
day_valid = (day >= 1) && (day <= max_days);
// is entire date valid?
date_valid = month_valid && day_valid;
if (date_valid)
printf("YES\n");
else
printf("NO\n");
return 0;
}
//是一月和十二月之间的月份(1-12)
//天数是否与指定时间内的天数一致
//月
//如果月份是二月,那是闰年吗?
//闰年是那些可以被400整除的年份,或者说是
//可被4整除,但不能被100整除
#包括
#包括
月内整数天[12]={
31, 28, 31, 30, 31, 30, 31, 31, 31, 31, 30, 31
};
int
主(内部argc,字符**argv)
{
整数日、月、年;
有效期为整月;
闰年;
国际最长日;
int day_有效;
int date_有效;
--argc;
++argv;
如果(argc==3){
月=atoi(argv[0]);
日=atoi(argv[1]);
年份=atoi(argv[2]);
}
否则{
如果(0)
scanf(“%d%d%d”、&day、&month、&year);
其他的
scanf(“%d%d%d”、&月、&日、&年);
}
//月在范围内吗?
month_valid=(month>=1)和&(month=1)和&(day)您正在检查年份是否为闰年,但您没有检查月份或日期是否有效。您还将覆盖month
的值。您不需要月循环,而是需要每月天数的数组(或使用开关语句)。检查月份是否在范围内,年份是否“合理”,确定是否是闰年,然后检查日期。完成。如果月份介于(1-12)之间,如何在开关中“检查”?我的输出必须是“是”或“否”,我不能在里面打印任何内容switch@conce1ted不客气。我知道你是stackoverflow新手,所以你可能想阅读。基本上,你不想用评论来表明哪个答案对你有帮助,而是要向上投票或接受答案。因为你是新手,所以你还不能向上投票,但欢迎你加入一个团队接受任何能帮助你解决问题的答案。
// is the month between January and December (1-12)
// does the number for days correspond with the number of days in the specified
// month
// if the month is February, is the year leap?
// Leap years are those years who are divisible with 400, or they are
// divisible with 4, but not with 100
#include <stdio.h>
#include <stdlib.h>
int days_in_month[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 31, 31, 30, 31
};
int
main(int argc,char **argv)
{
int day, month, year;
int month_valid;
int leap_year;
int max_days;
int day_valid;
int date_valid;
--argc;
++argv;
if (argc == 3) {
month = atoi(argv[0]);
day = atoi(argv[1]);
year = atoi(argv[2]);
}
else {
if (0)
scanf("%d %d %d", &day, &month, &year);
else
scanf("%d %d %d", &month, &day, &year);
}
// is month in range?
month_valid = (month >= 1) && (month <= 12);
// is it a leap year?
leap_year = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
// get number of days in month [if month is valid]
if (month_valid)
max_days = days_in_month[month - 1];
else
max_days = -1;
// february in a leap year has an extra day
if (month == 2)
max_days += leap_year;
// is day of the month valid?
day_valid = (day >= 1) && (day <= max_days);
// is entire date valid?
date_valid = month_valid && day_valid;
if (date_valid)
printf("YES\n");
else
printf("NO\n");
return 0;
}