距离某年某月某日计时器的设计

上午刚刚写了一篇srand()在随机数发生器中的使用,当时用当了头文件库<ctime>中的time(0)。所以下午一时手痒,何不做一个距离某年某月某的计时器。

其实有这个想法,也是因为昨天刚看了一篇《程序员能为爱情做的,就是用他的技术告诉世界:我爱你》的博客,语言很朴实,但是感觉很温馨。

谁说程序员,除了木讷的脑袋外,就不懂浪漫?

大家先看一下他的界面


这是那位程序员哥的网址  http://www.cnblogs.com/ider/archive/2011/11/02/i_love_you.html,大家有空可以看一下的哈,到时如果给自己女友也弄个的话,估计效果不错!

先说下我的思路,上篇文章已经提到过,time(0),会返回从格林尼治统一时间(GMT)1970年1月1日午夜开始到现在的秒数。

我们所做的就是利用这一点,实现代码很简单,剩下的就是数学计算了,好了不多说了,贴代码:

 

  1. <span style="font-size:16px;">  
  2. </span>  
  1. <span style="font-size:16px;">/******************************************* 
  2.     距离某年某月某日某时某分   计时器 
  3.     finished  by   huangshizeng 
  4.     2011年11月03日,于 CUIT 
  5. *******************************************/  
  6. #include <iostream>  
  7. #include <ctime>  
  8. #include <iomanip>  
  9.   
  10. using namespace std;  
  11.   
  12. int main()  
  13. {  
  14.     long long time_huang;  
  15.     int time_second;//   秒  
  16.     int time_min; //   分钟  
  17.     int time_hour; //   小时  
  18.     int time_day; //    天  
  19.   
  20.     /******************************************************* 
  21.     在2011年11月03日14时34分  time(0) 的值为 1320302060 
  22.     *******************************************************/  
  23.   
  24.     time_huang=time(0)-1320302060;  
  25.     time_day=time_huang/(24*60*60);  
  26.     time_hour=(time_huang%(24*60*60))/(60*60);  
  27.     time_min=((time_huang%(24*60*60))%(60*60))/60;  
  28.     time_second=((time_huang%(24*60*60))%(60*60))%60;  
  29.   
  30.     /****************************************************** 
  31.              取当前时间 
  32.     ******************************************************/  
  33.     time_t timep;  
  34.     time(&timep);  
  35.     cout<<"当前时间是 "<<ctime(&timep)<<endl;  
  36.   
  37.     /******************************************************* 
  38.              计算距离所定日期 时间 
  39.     *******************************************************/  
  40.     cout<<"距离2011年11月03日14时34分 "<<time(0)-1320302060<<" 秒"<<endl<<endl;;  
  41.     cout<<setw(25)<<"距离2011年11月03日14时34分 "<<endl<<endl<<setw(25)<<"已经有"<<endl<<endl;  
  42.     cout<<setw(25)<<time_day<<" 天"<<"  ";  
  43.     cout<<time_hour<<" 时"<<"  ";  
  44.     cout<<time_min<<" 分"<<"  ";  
  45.     cout<<time_second<<" 秒"<<"  ";  
  46.     return 0;  
  47. }  
  48. </span>  


下面看一下执行效果:

 


    好了就这多了,其他的以后再聊~~

posted @ 2012-07-02 20:14  黄世增  阅读(267)  评论(0编辑  收藏  举报