计算绝对天数

计算绝对天数       答题时间: 00 小时 04 分 46 秒
描述:
编写一个函数,输入某年某月某日,判断这一天是这一年的第几天。
详细描述:


能够实现如下的计算
支持任何一年任何一天的输入。
要考虑闰年的计算方式
例如2012年的3月5日,就是2012年的第65天
附加说明:
 要符合实际生活中的年/月/日数值,只要有一个异常,返回值都是0。
只考虑公元后的年份,即年绝对为正值
年最大直到9999.
 
接口设计及说明:


/*****************************************************************************
Description    :计算绝对天数
Input Param   :   char year:年份
char month:月份
    char day:日期
Output Param  : 无
Return Value   : 计算出的绝对天数
*****************************************************************************/
int CalculateAbsoluteDays (char year, char month, char day)
{
    /* 在这里实现功能*/ 
    Return 0;
}

 

/******************************************************************************

Copyright (C), 2001-2011, Huawei Tech. Co., Ltd.

******************************************************************************
File Name     :
Version       :
Author        :
Created       : 2012/3
Last Modified :
Description   :
Function List :

History       :
1.Date        : 2012/3
Author      :
Modification: Created file

******************************************************************************/
#include <string.h>

#include <iostream> 

using namespace std; 

/*****************************************************************************
Description    :计算绝对天数
Input Param   : char year:年份
                char month:月份
                char day: 日期
Output Param  : 无
Return Value   : 计算出绝对天数
*****************************************************************************/
int CalculateAbsoluteDays (int year, char month, char day)
{
	int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int cal_days = 0;

	if( year % 400 == 0 || (year % 4==0 && year %100 != 0))
	{
		month_days[1] = 29;
	}

	if(year < 1 || year > 9999)
	{
		return 0;
	}

	if(month < 1 || month > 12)
	{
		return 0;
	}

	if(month_days[month - 1] < day  || day < 1)
	{
		return 0;
	}

	for(int i = 0; i < month - 1; ++i)
	{
		cal_days += month_days[i];
	}

	cal_days += day;

    return cal_days;
}


 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值