#include<stdio.h>#include<stdlib.h>#define MONTH1 31#define MONTH2 28#define MONTH3 31#define MONTH4 30#define MONTH5 31#define MONTH6 30#define MONTH7 31#define MONTH8 31#define MONTH9 30#define MONTH10 31#define MONTH11 30#define MONTH12 31//#define __DEBUG__ #ifdef __DEBUG__ #define DEBUG(format,...) printf("File: "__FILE__", Line: %05d, Func: %s ++++ "format"\n", __LINE__,__func__, ##__VA_ARGS__) #else#define DEBUG(format,...) #endifintmain(int argc,constchar* argv[]){int year =0;int month =0;int day =0;int sum =0;int flag =0;if(4!= argc ){printf("Usage:year month day\n");printf("2020 08 26\n");return-1;}
year =atoi(argv[1]);
month =atoi(argv[2]);
day =atoi(argv[3]);#if 0if(year <1|| month <1|| month >12|| day <1|| day >31){puts("invalid date !");return-1;}if(day >30){if(4== month ||6== month ||9== month ||11== month){puts("invalid date !");return-1;}}if(!(year %4)&& year %100||!(year %400))
flag =1;if(2== month){if(day - flag >28){puts("invalid date !");return-1;}}//#elseif(year <1|| day <1){puts("invalid date !");return-1;}if(!(year %4)&& year %100||!(year %400))
flag =1;switch(month){case1:case3:case5:case7:case8:case10:case12:if(day >31){puts("invalid date !");return-1;}case4:case6:case9:case11:if(day >30){puts("invalid date !");return-1;}break;case2:if(day - flag >28){puts("invalid date !");return-1;}break;default:puts("invalid date !");return-1;}#endifif(year <1|| day <1|| day >31|| month <1|| month >12)goto Error;if(!(year %4)&& year %100||!(year %400))
flag =1;switch(month){case4:case6:case9:case11:if(day >30)goto Error;break;case2:if(day - flag >28)goto Error;break;default:break;}/************ kernel code ************/switch(month){case12:
sum += MONTH11;DEBUG("check me");case11:
sum += MONTH10;DEBUG("check me");case10:DEBUG("check me");
sum += MONTH9;case9:
sum += MONTH8;DEBUG("check me");case8:
sum += MONTH7;DEBUG("check me");case7:DEBUG("check me");
sum += MONTH6;case6:DEBUG("check me");
sum += MONTH5;case5:
sum += MONTH4;DEBUG("check me");case4:
sum += MONTH3;DEBUG("check me");case3:
sum += MONTH2 + flag;DEBUG("check me");case2:
sum += MONTH1;DEBUG("check me");case1:
sum += day;}DEBUG("check me");printf("the date \"%d-%d-%d\" is the %dth day\n",year,month,day,sum);return0;
Error:puts("invalid date !");return-1;}
测试结果1
测试代码2
#include<stdio.h>#define DAYS_MON_1 31#define DAYS_MON_2 28#define DAYS_MON_3 31#define DAYS_MON_4 30#define DAYS_MON_5 31#define DAYS_MON_6 30#define DAYS_MON_7 31#define DAYS_MON_8 31#define DAYS_MON_9 30#define DAYS_MON_10 31#define DAYS_MON_11 30#define DAYS_MON_12 31#define USE_ARRAY 0intmain(int argc,constchar* argv[]){int num =0;int day =0;int mon =0;int year =0;int f_isleap =0;#if USE_ARRAYint data[12]={DAYS_MON_1, DAYS_MON_2, DAYS_MON_3, DAYS_MON_4, DAYS_MON_5, DAYS_MON_6,
DAYS_MON_7, DAYS_MON_8, DAYS_MON_9, DAYS_MON_10, DAYS_MON_11, DAYS_MON_12};int i;#endif/* input */puts("enter your date:");if(3!=scanf("%d.%d.%d",&year,&mon,&day))goto ErrorHandler;/* check */if(year <0|| mon <1|| mon >12|| day <1|| day >31)goto ErrorHandler;if(mon ==4|| mon ==6|| mon ==9|| mon ==11){if(day >30)goto ErrorHandler;}if(!(year %4)&& year %100||!(year %400))
f_isleap =1;if(mon ==2){if(day - f_isleap >28)goto ErrorHandler;}/* process */#if USE_ARRAYfor(i =0, num =0; i < mon -1; i++)
num += data[i];
num += day +(mon >2? f_isleap :0);#elseswitch(mon){case12:
num += DAYS_MON_11;case11:
num += DAYS_MON_10;case10:
num += DAYS_MON_9;case9:
num += DAYS_MON_8;case8:
num += DAYS_MON_7;case7:
num += DAYS_MON_6;case6:
num += DAYS_MON_5;case5:
num += DAYS_MON_4;case4:
num += DAYS_MON_3;case3:
num += DAYS_MON_2 + f_isleap;case2:
num += DAYS_MON_1;case1:
num += day;}#endif/* output */printf("THE DAY OF YEAR: %d\n", num);return0;
ErrorHandler:puts("invalid date");return-1;}