计算绝对天数
答题时间: 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;
}
描述:
编写一个函数,输入某年某月某日,判断这一天是这一年的第几天。
详细描述:
能够实现如下的计算
支持任何一年任何一天的输入。
要考虑闰年的计算方式
例如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;
}