一道C語言題
20
題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
最佳回答:
這樣的程序,我真的不願寫,即沒有技術含量變量常量還多,可是我就是愛回答0回答的問題。switch語句就搞定了:
#include <stdio.h>
#define MOD ((year%400==0)||(year%100!=0)&&(year%4==0)) //這裡是文本替換,判斷是否為閏年
int main(void)
{
const int jan=31,feb=28,feb_leep=29,mar=31,apr=30,may=31,jun=30,jul=31,aug=31,sep=30,oct=31,nov=30,dec=12;
int year,month,day,day_s;
printf("輸入日期(年月日之間空格隔開):")
scanf("%d%d%d",year,month,day);
switch(month)
{
case 1:day_s=day;
case 2:MOD?day_s=jan+feb_leep:day_s=jan+feb;
case 3:MOD?day_s=jan+feb_leep+mar:day_s=jan+feb+mar;
case 4:MOD?day_s=jan+feb_leep+mar+apr:day_s=jan+feb+mar+apr;
case 5:MOD?day_s=jan+feb_leep+mar+apr+may:day_s=jan+feb+mar+apr+may;
case 6:MOD?day_s=jan+feb_leep+mar+apr+may+jun:day_s=jan+feb+mar+apr+may+jun;
case 7:MOD?day_s=jan+feb_leep+mar+apr+may+jun+jul:day_s=jan+feb+mar+apr+may+jun+jul;
case 8:MOD?day_s=jan+feb_leep+mar+apr+may+jun+jul+aug:day_s=jan+feb+mar+apr+may+jun+jul+aug;
case 9:MOD?day_s=jan+feb_leep+mar+apr+may+jun+jul+aug+sep:day_s=jan+feb+mar+apr+may+jun+jul+aug+sep;
case 10:MOD?day_s=jan+feb_leep+mar+apr+may+jun+jul+aug+sep+oct:day_s=jan+feb+mar+apr+may+jun+jul+aug+sep+oct;
case 11:MOD?day_s=jan+feb_leep+mar+apr+may+jun+jul+aug+sep+nov:day_s=jan+feb+mar+apr+may+jun+jul+aug+sep+nov;
case 12:MOD?day_s=jan+feb_leep+mar+apr+may+jun+jul+aug+sep+dec:day_s=jan+feb+mar+apr+may+jun+jul+aug+sep+dec;
}
printf("%d年%d月%d日是這一年的%d天\n",year,month,day,day_s);
return 0;
}