知道写的很烂,发上来时希望各位兄台指点不足:)
先谢啦,呵呵.
#include<iostream>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
struct date
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
unsigned int year,month,day;
};
![](/Images/OutliningIndicators/None.gif)
unsigned int sum(unsigned int,unsigned int,unsigned int);
bool is_eyear(unsigned int);
![](/Images/OutliningIndicators/None.gif)
void main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
date temp;
unsigned int total;
cout<<"please input date,formation:2005 12 05"<<endl;
cin>>temp.year>>temp.month>>temp.day;
total=sum(temp.year,temp.month,temp.day);
cout<<"total day="<<total<<endl;
}
![](/Images/OutliningIndicators/None.gif)
unsigned int sum(unsigned int y,unsigned int m,unsigned int d)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
unsigned int t=0;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(m<1)
{
t=0;
return t;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(m==1)
{
t=d;
return t;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch(m)
{
case 3:
if(is_eyear(y))
t=d+sum(y,m-1,29);
else t=d+sum(y,m-1,28);
break;
case 2:case 4:case 6:case 8:case 9:case 11:
t=d+sum(y,m-1,31);
break;
default :
t=d+sum(y,m-1,30);
break;
}
}
return t;
}//计算日期总数函数
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//***************
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
bool is_eyear(unsigned int y)
{
if(y%400==0) return 1;
else if(y%4==0) return 1;
else return 0;
}//判断闰年函数
**************/
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
bool is_eyear(unsigned int y)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
if(y%100==0&&y%400==0) return 1;
if(y%100!=0&&y%4==0) return 1;
return 0;
}//判断闰年函数
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
posted on 2005-12-28 23:12
豪 阅读(917)
评论(6) 编辑 收藏 引用 所属分类:
C++之梦
![](http://www.cppblog.com/qywyh/aggbug/2222.html?webview=1)
计算日期总数"
trackback:ping="http://www.cppblog.com/qywyh/services/trackbacks/2222.aspx" />
-->
FeedBack:
# re: 递归--->计算日期总数 2005-12-29 09:57
两个错误
1.拼写错误
cout<<"please input date,formation:2005 12 05"<<endl;
改为
cout<<"please input date,format:2005 12 05"<<endl;
2.闰年计算方法错误
请问1900是闰年么?
回复 更多评论
# re: 递归--->计算日期总数 2005-12-29 10:00
有两个问题:
1. 什么叫日期总数?是计算总天数吗?从什么时候到什么时候的总天数?
从程序上看,似乎是计算给定日期当年元旦的总天数,对吗?
2. 是为了试验递归函数的用法,还是为了试验公历历法的算法?
如果是试验公历历法的算法,is_eyear函数算法不对。
公历的闰年判断条件为(可根据地球公转周期为365天5小时48分46秒来算出来):
a. 能被4整除
b. 不能被100整除
c. 能被400整除
d. 不能被3200整除
e. 能被86400整除
NOTE:
如果仅仅是为了得到结果,那完整可以用c语言库来得到。
#include <time.h>
int sum(int y, int m, int d)
{
struct tm t;
memset(&t, 0, sizeof(t));
t.tm_year = y-1900;
t.tm_mon = y-1;
t.tm_mday = day;
time_t _t = mktime(&t);
int days = gmtime(&_t)->tm_yday;
return days;
}
缺点是,只能计算1900年以后的日期。
回复 更多评论
# re: 递归--->计算日期总数 2005-12-29 10:47
# re: 递归--->计算日期总数 2005-12-29 10:58
bool isleapyear(int y)
{
if(y%400==0) return true;
if(y%100==0) return false;
if(y%4==0) return true;
return false;
}
回复 更多评论
# re: 递归--->计算日期总数 2005-12-30 14:27
# re: 递归--->计算日期总数
2006-08-30 14:18