本文主要是介绍编程,输入日期年、月、日,求出该日期是这一年的第几天。要用switch编程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include<stdio.h>
int main(){int day,month,year,sum,leap;printf("请输入年、月、日\n");scanf("%d %d %d",&year,&month,&day);switch(month){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("月份错误\n");break;}sum=sum+day;switch(month){case 1:if(day<=31){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 2:if(day<=29){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 3:if(day<=31){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 4:if(day<=30){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 5:if(day<=31){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 6:if(day<=30){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 7:if(day<=31){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 8:if(day<=31){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 9:if(day<=30){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 10:if(day<=31){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 11:if(day<=30){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}case 12:if(day<=31){if(year%400==0||(year%4==0&&year%100!=0)){leap=1;}else{leap=0;}if(leap==1&&month>2){sum++;}printf("这是这一年的第%d天\n",sum);}else{printf("输入错误\n");}}return 0;
}
这篇关于编程,输入日期年、月、日,求出该日期是这一年的第几天。要用switch编程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!