c语言 用switch语句解决; 输入一个日期(ymd),求该日期至当年末有多少天?
#include <stdio.h>
void main()
{
int y,m,d,day=0;
printf("请输入年月日:");
scanf("%d%d%d",&y,&m,&d);
switch(m)
{
case 1:day+=31-d;break;
case 2:day+=28+((y%4==0&&y%100!=0)||(y%400==0))-d;break;
case 3:day+=31-d;break;
case 4:day+=30-d;break;
case 5:day+=31-d;break;
case 6:day+=30-d;break;
case 7:day+=31-d;break;
case 8:day+=31-d;break;
case 9:day+=30-d;break;
case 10:day+=31-d;break;
case 11:day+=30-d;break;
case 12:day+=31-d;break;
default:day=0;
}
switch(m+1)
{
case 2:day+=28+((y%4==0&&y%100!=0)||(y%400==0));
case 3:day+=31;
case 4:day+=30;
case 5:day+=31;
case 6:day+=30;
case 7:day+=31;
case 8:day+=31;
case 9:day+=30;
case 10:day+=31;
case 11:day+=30;
case 12:day+=31;break;
default:day+=0;
}
printf("该日至当年末有%d天\n",day);
}
输入一个日期(ymd)后,使用FOR循环,并用用switch语句解决循环体中的计算,就可求出该日期至当年末有多少天。
#include<stdio.h>
int main()
{ int y,m,d;
scanf("%d%d%d",&y,&m,&d);
d=-d;
for(; m<13; m++)
switch(m)
{ case 4:
case 6:
case 9:
case 11:
d+=30;
break;
case 2:
d+=28+(y%4==0&&y%100||y%400==0);
break;
default:
d+=31;
}
printf("%d\n",d);
return 0;
}