随笔 - 87  文章 - 279  trackbacks - 0
<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

潜心看书研究!

常用链接

留言簿(19)

随笔分类(81)

文章分类(89)

相册

ACM OJ

My friends

搜索

  •  

积分与排名

  • 积分 - 211788
  • 排名 - 116

最新评论

阅读排行榜

评论排行榜

知道写的很烂,发上来时希望各位兄台指点不足:)

先谢啦,呵呵.



#include<iostream>
using namespace std;

struct date
{
     unsigned 
int year,month,day;
}
;

unsigned 
int sum(unsigned int,unsigned int,unsigned int);
bool is_eyear(unsigned int);

void main()
{
     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;
}


unsigned 
int sum(unsigned int y,unsigned int m,unsigned int d)
{
     unsigned 
int t=0;
     
if(m<1){
          t
=0;
          
return t;
     }
else if(m==1){
          t
=d;
          
return t;
     }
else{
          
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;
}
//计算日期总数函数

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


bool is_eyear(unsigned int y)
{
     if(y%400==0) return 1;
     else if(y%4==0) return 1;
     else return 0;
}//判断闰年函数
*************
*/





bool is_eyear(unsigned int y)
{
     
if(y%100==0&&y%400==0return 1;
     
if(y%100!=0&&y%4==0return 1;
     
return 0;
}
//判断闰年函数

posted on 2005-12-28 23:12 阅读(911) 评论(6)  编辑 收藏 引用 所属分类: C++之梦
计算日期总数" 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 x
ni   回复  更多评论
  
# re: 递归--->计算日期总数 2006-08-30 14:18 子弹
碰巧我写了个类似的程序, http://www.cppblog.com/ztwaker/archive/2006/08/25/11688.html
欢迎交流,呵呵  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理