- #include <stdio.h>
- int main(void)
- {int y,m,n,d;
- n=0;
- printf("input your year\n");
- scanf("%d%d%d",&y,&m,&d);// 格式是2010 02 02这样默认是空格分开
- switch(m){
- case 12: n+=30;
- case 11: n+=31;
- case 10: n+=30;
- case 9: n+=31;
- case 8: n+=31;
- case 7: n+=30;
- case 6: n+=31;
- case 5: n+=30;
- case 4: n+=31;
- case 3: if((y%4==0&&y%100!=0)||y%400==0) n+=29;else n+=28;
- case 2:n+=31;
- case 1: n+=d;printf("today is %d of %d\n",n,y);break;
- default: printf("please input a valid year no.\n");}
- }
仔细体会下case 里不带break的用法,假如用break,那2月要d+31以此类推了。
最后说下这个2010 02 31也能算出结果,健壮性不够,要做判断,用数组可能更好点。
阅读(849) | 评论(0) | 转发(0) |