c语言练习题:输入年月份输出天数是这一年中的第几天
#include <stdio.h>//输入年月十分判断这是这一年的第几天
int main(int argc, char const *argv[])
{
int sum=0;
int year=0,mon=0,day=0;
int mon2=0;
printf("欢迎来到本系统\n");
printf("请输入年月份(格式为空格)");
scanf("%d %d %d",&year,&mon,&day);
if(year<0){
printf("输入错误请重新输入");
}
if((year%100!=0&&year%4==0)||year%400==0){
mon2=29;
}else{
mon2=28;
}
switch (mon)
{
case 1:sum+=day;break;
case 2:sum+=(31+day);break;
case 3:sum+=(31+mon2+day);break;
case 4:sum+=(31+mon2+31+day);break;
case 5:sum+=(31+mon2+31+30+day);break;
case 6:sum+=(31+mon2+31+30+31+day);break;
case 7:sum+=(31+mon2+31+30+31+30+day);break;
case 8:sum+=(31+mon2+31+30+31+30+31+day);break;
case 9:sum+=(31+mon2+31+30+31+30+31+31+day);break;
case 10:sum+=(31+mon2+31+30+31+30+31+31+30+day);break;
case 11:sum+=(31+mon2+31+30+31+30+31+31+30+31+day);break;
case 12:sum+=(31+mon2+31+30+31+30+31+31+30+31+30+day);break;
default:
printf("你输入错误请重新输入\n");
break;
}
if(mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12){
day<=31&&day>0;
if(day>31||day<0){
printf("输入错请重新输入\n");
}else{
printf("%d %d %d是这一年中的第%d天\n",year,mon,day,sum);
}
}else if(mon==4||mon==6||mon==9||mon==11){
day<=30&&day<0;
if(day>30||day<0){
printf("输入错误请重新输入\n");
}else{
printf("%d %d %d是这一年中的第%d天\n",year,mon,day,sum);
}
}
if(mon==2){
if((year%100!=0&&year%4==0)||year%400==0){
day<=29&&day!=0;
if(day<=29&&day>0){
printf("%d %d %d是这一年中的第%d天\n",year,mon,day,sum);
}else{
printf("%d是平年,请重新输入\n",year);
}
}else{
day<=28&&day!=0;
if( day<=28&&day>0){
printf("%d %d %d是这一年中的第%d天\n",year,mon,day,sum);
}else{
printf("%d是平年请重新输入\n",year);
}
}
}
return 0;
}
|