64,645
社区成员
发帖
与我相关
我的任务
分享
//看不懂你们老师的算法。。。汗。
//参照以前有个帖子写的。计算输入日期和当前日期之间的天数
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
struct tm *input_date=new struct tm;
struct tm *current_date,*now_date;
memset(now_date,0,sizeof(struct tm));
memset(input_date,0,sizeof(struct tm));
int year,mon,day;
time_t current_time;
current_time=time(NULL);
current_date=localtime(¤t_time);
now_date->tm_year=current_date->tm_year;
now_date->tm_mon=current_date->tm_mon;
now_date->tm_mday=current_date->tm_mday;
cout<<"Current date is: "<<now_date->tm_year+1900<<"-"<<now_date->tm_mon+1<<"-"<<now_date->tm_mday;
cout<<endl;
cout<<"Please input your date! "<<endl;
cin>>year>>mon>>day;
input_date->tm_year=year-1900;
input_date->tm_mon=mon-1;
input_date->tm_mday=day;
double sub=0.0;
sub=difftime(mktime(now_date),mktime(input_date));
sub/=60*60*24;
cout<<"The sub days between are: ";
cout<<sub<<endl;
return 0;
}
/*试验数据:
Current date is: 2008-10-2
Please input your date!
2007 9 20
The sub days between are: 378
Terminated with return code 0
Press any key to continue ...
*/
#include <iostream>
#include <cmath>
#include <time.h>
using namespace std;
long julian(int year, int month, int day)
{
long jy=year,jm=month,jd=day;
if (jy <0) jy=jy+1;
if (month>2)jm=jm+1;else jm=jm+13;jy=jy-1;
long jul=floor(365.25*jy)+floor(30.6001*jm)+jd+1720995.0;
if (jy <1582&&jm <10&&jd <15)
{
return jul;
}
else
{
int ja=0.01*jy;jul=jul+2-ja+0.25*ja;
return ja;
}
}
int main()
{
struct tm *tm_now;
time_t time_now;
time_now=time(NULL);
tm_now=localtime(&time_now);
int jy,jm,jd;
cout <<"This is a Julian Day calculator,it can calculator how many days between the date you enter and today's date." <<endl
<<"Please enter the date you want to calculate:" <<endl <<"year = ";
cin>>jy;
cout <<endl <<"month = ";
cin>>jm;
cout <<endl <<"day = ";
cin>>jd;
int tm_day=tm_now->tm_mday, tm_month=tm_now->tm_mon+1,tm_yearl=tm_now->tm_year+1900;
long t=julian(tm_yearl,tm_month,tm_day)- julian (jy,jm,jd);
cout <<"The julian Day between year:" <<jy <<" month:" <<jm <<" day:" <<jd <<"to year:" <<tm_yearl <<" month:" <<tm_month <<" day;" <<tm_day <<"is:" <<endl;
cout << t;
return 0;
}