微软面试题目(一) 计算两个日期之间的天数

微软的面试,没能参加啊,只好打听了下题目,其中一题是在20分钟内写出:计算两个日期之间天数的方法。昨天晚上想了一下,今天实现了一下,经过调试,差不多一个小时才弄好,惭愧啊

代码如下:

/********************************************************************
	file name:	CountDays
	file ext:	cpp
	author:		zoudh
	created:	2012/04/18	
	
	purpose:	计算两个日期之间的天数
*********************************************************************/

#include <stdlib.h>
#include <string.h>

typedef struct _Date{
	int year;
	int month;
	int day;
	_Date(char* strDate);
}Date;

int CountDays(Date date1,Date date2);
int GetDaysFromThisYear(Date date);
int GetDaysToNextYear(Date date);
int GetDaysInYear(int year);
int GetDaysInMonth(int month,bool LeapYear);
bool IsLeapYear(int year);

_Date::_Date( char* strDate )
{
	//初始化
	year  = 1;
	month = 1;
	day   = 1;

	const char* startpos=strDate;
	const char* pos = startpos;
	char buf[64];	
	//获取年份
	memset(buf,0,64);
	while( '-' != *pos && '\0' != *pos ) pos++;
	memcpy_s(buf,64,startpos,pos-startpos);
	year = atoi(buf);
	startpos = ++pos;
	//获取月份
	memset(buf,0,64);
	while( '-' != *pos && '\0' != *pos ) pos++;
	memcpy_s(buf,64,startpos,pos-startpos);
	month = atoi(buf);
	startpos = ++pos;
	//获取日期
	memset(buf,0,64);
	memcpy_s(buf,64,pos,strlen(pos));
	day = atoi(buf);
}
int CountDays(Date date1,Date date2)
{
	int iTotalDays = 0;
	if( date1.year == date2.year)\
	{
		return GetDaysFromThisYear(date2) - GetDaysFromThisYear(date1) + 1;
	}
	iTotalDays = GetDaysToNextYear(date1); 
	iTotalDays += GetDaysFromThisYear(date2);
	int year = date1.year + 1;
	while(year < date2.year )
	{
		iTotalDays += GetDaysInYear(year); 
	}
	return iTotalDays;
}

//计算从当年年初到当前日期的天数
int GetDaysFromThisYear( Date date )
{
	bool bLeapYear = IsLeapYear(date.year);
	int iTotalDays = date.day;
	for( int month = 1; month < date.month ; month++)
	{
		iTotalDays += GetDaysInMonth(month,bLeapYear);
	}
	return iTotalDays;
}
//计算从当前日期到年底的天数
int GetDaysToNextYear( Date date )
{
	bool bLeapYear = IsLeapYear(date.year);
	//当月有多少天
	int iDaysInMonth = GetDaysInMonth(date.month,bLeapYear);
	//统计当月的天数
	int iTotalDays = iDaysInMonth - date.day + 1;
	for( int month = date.month + 1; month <= 12 ; month++)
	{
		iTotalDays += GetDaysInMonth(month,bLeapYear);
	}
	return iTotalDays;
}
//计算本年内的天数
int GetDaysInYear( int year )
{
	bool bLeapYear = IsLeapYear(year);
	if(bLeapYear)
		return 366;
	else
		return 365;
}
//判断是否是闰年
bool IsLeapYear( int year )
{
	if( year%100 == 0)
	{
		if( year%4 == 0)
			return true;
		else
			return false;
	}
	else
	{
		if( year%4 == 0)
			return true;
		else
			return false;
	}
}

//获取一个月份中的天数
int GetDaysInMonth( int month,bool LeapYear )
{
	switch (month)
	{
	case 1:
		return 31;
	case 2:
		if(LeapYear)
			return 29;
		else
			return 28;
	case 3:
		return 31;
	case 4:
		return 30;
	case 5:
		return 31;
	case 6:
		return 30;
	case 7:
		return 31;
	case 8:
		return 31;
	case 9:
		return 30;
	case 10:
		return 31;
	case 11:
		return 30;
	case 12:
		return 31;
	default:
		return 0;		
	}
}

int main()
{
	char* szdate1 = "2011-3-1";
	char* szdate2 = "2012-3-1";
	Date date1(szdate1);
	Date date2(szdate2);
	int days = CountDays(date1,date2);
}
posted @ 2012-05-13 18:12  DylanTsou  阅读(590)  评论(0编辑  收藏  举报