C语言学习:switch穿透性
做了练习题,输入年月日,计算此日子为当年的第几天:
#include <stdio.h>
int main()
{
int year,mon,day,two,rizi;
printf("请输入年、月、日,格式为:年,月,日(2015,12,10):");
scanf("%d,%d,%d",&year,&mon,&day);
printf("%d\n",year);
printf("%d\n",mon);
printf("%d\n",day);
rizi = 0;
/*
two = 28;
if (year%400==0 || (year%4==0 && year%100!=0))
two++;
*/
(year%400==0 || (year%4==0 && year%100!=0))?two=29:two=28;//判断闰年
//printf("%d,%d,%d,%d",two,year%400,year%4,year%100);
switch(mon)//通过switch的穿透性(在前条件成立后,后续条件不判断,直接执行)
{
case 12:
rizi+=day;
day =30;
case 11:
rizi+=day;
day =31;
case 10:
rizi+=day;
day =30;
case 9:
rizi+=day;
day =31;
case 8:
rizi+=day;
day =31;
case 7:
rizi+=day;
day =30;
case 6:
rizi+=day;
day =31;
case 5:
rizi+=day;
day =30;
case 4:
rizi+=day;
day =31;
case 3:
rizi+=day;
day =two;
case 2:
rizi+=day;
day =31;
case 1:
rizi+=day;
}
printf("这是这一年的第 %d 天。",rizi);
return 0;
}