实现计算一年中的某一天属于这一年的第几天

测试代码1

#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,...)  
#endif  
 
int main(int argc, const char* 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 0

	if (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;		
		}
	}
//#else
	if (year < 1 || day < 1 ) {
		puts("invalid date !");
		return -1;
	}
	if (!(year % 4) && year % 100 || !(year % 400))
		flag = 1;
	switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (day > 31) {
				puts("invalid date !");
				return -1;
			}
		case 4:
		case 6:
		case 9:
		case 11:
			if (day > 30) {
				puts("invalid date !");
				return -1;
			}
			break;
		case 2:
			if (day - flag > 28) {
				puts("invalid date !");
				return -1;		
			}
			break;
		default:
			puts("invalid date !");
			return -1;		
	}

#endif

	if (year < 1 || day < 1 || day > 31 || month < 1 || month > 12) 
		goto Error;
	if (!(year % 4) && year % 100 || !(year % 400))
		flag = 1;
	switch (month) {
	case 4:
	case 6:
	case 9:
	case 11:
		if(day > 30)
			goto Error;
		break;
	case 2:
		if(day - flag > 28)
			goto Error;
		break;
	default:
		break;
	}
/************ kernel code  ************/

	switch (month) {
	case 12:
		sum += MONTH11;
		DEBUG("check me");
	case 11:
		sum += MONTH10;
		DEBUG("check me");
	case 10:
		DEBUG("check me");
		sum += MONTH9;
	case 9:
		sum += MONTH8;
		DEBUG("check me");
	case 8:
		sum += MONTH7;
		DEBUG("check me");
	case 7:
		DEBUG("check me");
		sum += MONTH6;
	case 6:
		DEBUG("check me");
		sum += MONTH5;
	case 5:
		sum += MONTH4;
		DEBUG("check me");
	case 4:
		sum += MONTH3;
		DEBUG("check me");
	case 3:
		sum += MONTH2 + flag;
		DEBUG("check me");
	case 2:
		sum += MONTH1;
		DEBUG("check me");
	case 1:
			sum += day;
	}
	DEBUG("check me");
	printf("the date \"%d-%d-%d\" is the %dth day\n",year,month,day,sum);
	return 0;

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	0

int main(int argc,const char * argv[])
{
	int num  	 = 0;
	int day  	 = 0;
	int mon  	 = 0;
	int year 	 = 0;
	int f_isleap = 0;
#if USE_ARRAY
	int 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_ARRAY
	for (i = 0, num = 0; i < mon - 1; i++)
		num += data[i];
	num += day + (mon > 2 ? f_isleap : 0);
#else
	
	switch (mon) {
	case 12:
		num += DAYS_MON_11;
	case 11:
		num += DAYS_MON_10;
	case 10:
		num += DAYS_MON_9;
	case 9:
		num += DAYS_MON_8;
	case 8:
		num += DAYS_MON_7; 
	case 7:
		num += DAYS_MON_6;
	case 6:
		num += DAYS_MON_5;
	case 5:
		num += DAYS_MON_4;
	case 4:
		num += DAYS_MON_3;
	case 3:
		num += DAYS_MON_2 + f_isleap;
	case 2:
		num += DAYS_MON_1;
	case 1:
		num += day;
	}

#endif

	/* output */
	printf("THE DAY OF YEAR: %d\n", num);

	return 0;

ErrorHandler:
	puts("invalid date");
	return -1;
}

测试结果2

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值