C语言学习-菜鸟教程C经典100例-练习4
·
C语言学习-菜鸟教程C经典100例-练习4
问题
输入某年某月某日,判断这一天是这一年的第几天?
思路
对于输入的日期要设置为year, month, day.
-
首先判断是否为闰年:(两个条件满足一个)
- (1) 能被4整除,但不能被100整除;
- (2) 能被400整除
-
设置一个元素来判断是否为闰年;
-
用一个数组来存储每个月的天数(非闰年的);
-
注意对于闰年的情况要修改2月份的天数;
-
计算这一年的第几天:
- 把前面几个月的日期加起来然后加上这个月的;
- 可以用一个for循环来加前几个月的日期;
代码
#include <stdio.h>
int main(){
int year, month, day;
int is_leap_year = 0; // 0表示不是闰年,1表示是闰年
printf("请输入年份、月份和日期(格式:年 月 日):");
scanf("%d %d %d", &year, &month, &day);
// 使用数组来存储每个月的天数
int days_of_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 判断是否为闰年
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 ==0)){
is_leap_year = 1;
days_of_month[1] = 29; // 2月份的天数为29
}
// 计算是这一年的第几天
int total_days = 0;
// 先加前几个月的天数
for(int i=0; i<month-1; i++){
total_days = total_days + days_of_month[i];
}
// 加上这个月的天数
total_days = total_days + day;
printf("%d年%d月%d日是该年的第%d天\n", year, month, day, total_days);
return 0;
}
输出结果
请输入年份、月份和日期(格式:年 月 日):2024 3 5
2024年3月5日是该年的第65天
#加一个判断日期是否有效
思路
在读取到年月日后加一个判断,判断日期是否有效;
- 年份不能为负;
- 月份是否在1-12之间;
- 日期是否在该月范围内;
代码
#include <stdio.h>
int main(){
int year, month, day;
int is_leap_year = 0; // 0表示不是闰年,1表示是闰年
printf("请输入年份、月份和日期(格式:年 月 日):");
scanf("%d %d %d", &year, &month, &day);
// 判断年份是否有效
if(year < 0){
printf("输入无效,年份 %d 无效!年份必须在1-12之间,请输入正确的年份、月份和日期。\n", year);
return 1; // 返回非0值表示程序异常结束
}
// 判断月份是否有效
if(month < 1 || month > 12){
printf("输入无效,月份 %d 无效!月份必须在1-12之间,请输入正确的年份、月份和日期。\n", month);
return 1;
}
// 使用数组来存储每个月的天数
int days_of_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 判断是否为闰年
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 ==0)){
is_leap_year = 1;
days_of_month[1] = 29; // 2月份的天数为29
}
// 判断日期是否有效
if(day < 1 || day > days_of_month[month-1]){
printf("输入无效,日期 %d 无效!日期必须在1-%d之间,请输入正确的年份、月份和日期。\n", day, days_of_month[month-1]);
return 1;
}
// 计算是这一年的第几天
int total_days = 0;
// 先加前几个月的天数
for(int i=0; i<month-1; i++){
total_days = total_days + days_of_month[i];
}
// 加上这个月的天数
total_days = total_days + day;
printf("%d年%d月%d日是该年的第%d天\n", year, month, day, total_days);
return 0;
}
输出结果
请输入年份、月份和日期(格式:年 月 日):-1023 2 21
输入无效,年份 -1023 无效!年份必须在1-12之间,请输入正确的年份、月份和日期。
请输入年份、月份和日期(格式:年 月 日):2024 18 2
输入无效,月份 18 无效!月份必须在1-12之间,请输入正确的年份、月份和日期。
请输入年份、月份和日期(格式:年 月 日):2024 2 32
输入无效,日期 32 无效!日期必须在1-31之间,请输入正确的年份、月份和日期。
请输入年份、月份和日期(格式:年 月 日):2025 2 32
输入无效,日期 32 无效!日期必须在1-28之间,请输入正确的年份、月份和日期。
请输入年份、月份和日期(格式:年 月 日):2024 3 5
2024年3月5日是该年的第65天
#将闰年判断逻辑封装成函数
代码
#include <stdio.h>
// 判断是否为闰年
int isLeapYear(int year){
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 ==0)){
return 1; // 是闰年
}
return 0; // 不是闰年
}
int main(){
int year, month, day;
int is_leap_year = 0;
printf("请输入年份、月份和日期(格式:年 月 日):");
scanf("%d %d %d", &year, &month, &day);
// 使用数组来存储每个月的天数
int days_of_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 调用函数判断是否为闰年
is_leap_year = isLeapYear(year);
// 处理闰年2月的天数
if(is_leap_year){
days_of_month[1] = 29;
}
// 计算是这一年的第几天
int total_days = 0;
// 先加前几个月的天数
for(int i=0; i<month-1; i++){
total_days = total_days + days_of_month[i];
}
// 加上这个月的天数
total_days = total_days + day;
printf("%d年%d月%d日是该年的第%d天\n", year, month, day, total_days);
return 0;
}
更多推荐



所有评论(0)