#include<stdio.h> #if 0 输入某年某月某日,判断这一天是这一年的第几天? 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。 #endif int main(){ int day,month,year,flag,sum,leap; printf("请输入年份:\n");//获取日期 scanf("%d",&year); printf("请输入月份:\n"); scanf("%d",&month); printf("请输入天数:\n"); scanf("%d",&day); if( year%400==0 || (year%4==0 && year%100==0) ){//判断是否是润年,是则重置flag为1,不是则为0 flag = 1;} else{ flag = 0;} do{ leap = 0; switch(month){//这个月之前过的总天数,并对日期进行判断,限制范围 case 1: sum = 0; if(day<0 || day>31){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 2: sum = 31; if(flag==0&&(day<0 || day>28)){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} if(flag==1&&(day<0 || day>29)){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 3: sum = 59; if(day<0 || day>31){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 4: sum = 90; if(day<0 || day>30){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 5: sum = 120; if(day<0 || day>31){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 6: sum = 151; if(day<0 || day>30){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 7: sum = 181; if(day<0 || day>31){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 8: sum = 212; if(day<0 || day>31){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 9: sum = 243; if(day<0 || day>30){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 10: sum = 273; if(day<0 || day>31){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 11: sum = 304; if(day<0 || day>30){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; case 12: sum = 334; if(day<0 || day>31){ printf("您的天数输入有误,请重新输入!\n"); printf("请输入天数:\n"); scanf("%d",&day);} break; default:{printf("您的输入有误,请重新输入!\n"); leap = 1; printf("请输入年份:\n"); scanf("%d",&year); printf("请输入月份:\n"); scanf("%d",&month); printf("请输入天数:\n"); scanf("%d",&day); } } }while(leap); sum = sum+day; if( flag=1 && month>2 ){ sum++;} printf("%d %d %d为本年的第%d天",year,month,day,sum); return 0; }