计算该日在本年中是第几天?注意闰年
struct date {
int year;
int month;
int day;
} adate;
int flag = 0;
while (1) {
printf("请输入年月日:");
scanf("%d%d%d", &adate.year, &adate.month, &adate.day);
flag = adate.year % 400 == 0 || (adate.year % 4 == 0 && adate.year % 100 != 0);
if (adate.year <= 0 || adate.month <= 0 || adate.month > 12) {
printf("输入日期有误,请重新输入!\n");
continue;
}
if (adate.month == 1 || adate.month == 3 || adate.month == 5 || adate.month == 7 || adate.month == 8 || adate.month == 10 || adate.month == 12) {
if (adate.day <= 0 || adate.day > 31) {
printf("输入日期有误,请重新输入!\n");
continue;
}
}
if (adate.month == 4 || adate.month == 6 || adate.month == 9 || adate.month == 11) {
if (adate.day <= 0 || adate.day > 30) {
printf("输入日期有误,请重新输入!\n");
continue;
}
}
if (flag == 1 && adate.month == 2 && adate.day > 29) {
printf("输入日期有误,请重新输入!\n");
continue;
}
if (flag == 0 && adate.month == 2 && adate.day > 28) {
printf("输入日期有误,请重新输入!\n");
continue;
}
break;
}
int sum = 0;
int number[12] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30};
for (int i = 0; i < adate.month; i++) {
sum += number[i];
}
sum += adate.day;
if (flag == 0 && adate.month > 2) {
sum -= 1;
}
printf("%d年%d月%d日是%d年的第%d天", adate.year, adate.month, adate.day, adate.year, sum);