#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//判断是否是闰年
bool Is_YearLeap(int year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}
//输入年月日,输出天数
int Get_YearMontDay_To_Sum(int year, int month, int day)
{
int sum = 0;
static const int days[] = { 0,0,31,59,90,120,151,181,212,243,273,304,334 };//静态常数数组 存在.data
// 0,1, 2, 3, 4, 5 ,6 , 7 ,8 ,9, 10, 11 12
if (month > 2 && Is_YearLeap(year))
{
sum = days[month] + day + 1;
//printf("\n是第 %d天\n", sum);
}
else {
sum = days[month] + day;
//printf("\n是第 %d天\n", sum);
}
return sum;
}
//输入年月 判断天数
int Get_YearMonth_To_Day(int year, int month) {
static const int days[] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };//静态常数数组 存在.data
// 0, 1, 2, 3, 4, 5 ,6 ,7 ,8, 9,10,11 12
if (month == 2 && Is_YearLeap(year))
{
month = 0;
}
return days[month];
}
int main() {
while (1) {
int year = 0; int month = 0; int day = 0, days = 0, Sum = 0;
printf("请输入年和月和日(中间用逗号隔开):");
scanf_s("%d,%d,%d", &year, &month, &day);
if (month >= 1 && month <= 12 && day >= 1 && day <=Get_YearMonth_To_Day(year, month)) {
Sum = Get_YearMontDay_To_Sum(year, month, day);
printf("\n%d年%d月%d日是%d年的第 %d天\n", year, month, day, year, Sum);
}
if (month < 1 || month >12) {
printf("月份输入有误!!!\n");
}
if (day < 1 || day > Get_YearMonth_To_Day(year, month)) {
printf("天数输入有误!!!\n");
}
}
return 0;
}
输入年月日,输出这一天是这一年的第几天
最新推荐文章于 2024-06-16 21:43:45 发布