#include <stdio.h> #include <stdlib.h> /* 输入年月日,输出该日期是当年的第几天。 year大于1900 */ int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int isGoodYear(int year); int isGoodMonth(int month); int isGoodDay(int year,int month,int day); int isLeapYear(int year); int dayOfYear(int year, int month, int day); int main(){ int year, month, day; while (fflush(stdin), scanf("%d%d%d", &year, &month, &day) != EOF){ if (isGoodYear(year) && isGoodMonth(month) && isGoodDay(year, month, day)) //判断输入是否合法 printf("%d\n", dayOfYear(year, month, day)); else printf("Please input a right date.\n"); } system("pause"); return 0; } int dayOfYear(int year, int month, int day){//当年第几天 int total = 0; for (int i = 0; i < month - 1; ++i) total += monthDays[i]; total += day; if (isLeapYear(year) && (month > 2)) total++; return total; } int isLeapYear(int year){ if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) return 1; else return 0; } int isGoodYear(int year){ if (year >= 1900) return 1; else return 0; } int isGoodMonth(int month){ if ((month >= 1) && (month <= 12)) return 1; else return 0; } int isGoodDay(int year, int month, int day){ switch (month) { case 2: if (isLeapYear(year)) if ((day >= 1) && (day <= 29)) return 1; else if ((day >= 1) && (day <= 28)) return 1; else return 0; case 4: case 6: case 9: case 11: if ((day >= 1) && (day <= 30)) return 1; else return 0; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if ((day >= 1) && (day <= 31)) return 1; else return 0; default: printf("month error in day judge.\n"); return 0; } }
时间: 2024-11-05 21:56:30