|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.求助各位大神,关于julianday的日期求法,本次程序是奠定一个起始日期epoch=2000年1月1日0点0分0秒;然后要求计算从任意日期—————>例如:2018年1月1日0时0分0秒到epoch之间的时间跨度。
2.完成对年,月,日之间的加减运算,在此期间需要注意的是要判断闰年(因为闰年的2月是29天,(如果输入的日期月份大于3月而且是闰年的话。。))
3.还要注意月份之间的相加和相减,因为2018年3月-2017年10月=0年5月, 2018年4月20日+11天=2018年5月1日。
4.接着需要从电脑自动获取现在的时间,然后依次打印
5.最后需要验证程序,老师让我们输出从2019年1月1日0时0分0秒连续加100天的数值,全部输出。
老师的要求如下:#include <time.h>
class JulianDate {
private:
static int EPOCH;
// int mon, day, year, hour, min, second;
// this representation would make difference VERY UGLY
// diff between Jan. 1 2001 12:03:04 and Feb 26 2028 11:19:02
double jday; // number of days since epoch
};
/*
how many days in a year? 365
leap year: if year MOD 4 == 0 LEAP EXCEPT
if year MOD 100 == 0 NOT LEAP YEAR Except
if year MOD 400 == 0 LEAP
Leap years:
NO 1900
YES 1904
YES 1908
YES 2000
NO 2100
YES 2400
days since EPOCH = 365 * (year - 2000) + years/4 - ....
2018 - 2000 = 18
seconds in a day = 24*60*60 = 86400
hh:mm:ss
00:00:00 0.0
12:00:00 0.5
*/
int JulianDate::EPOCH = 2000; // Jan.1 2000, 00:00:00 = 0
int main() {
JulianDate newyear(2018, 1, 1, 0,0,0);
JulianDate valentine(2018, 2, 14, 12,0,0); // 0.5
JulianDate today; // get it from the system time: time(nullptr)
// localtime
double days = valentine - newyear;
JulianDate due = today + 7;
cout << due << '\n';
cout << "\nyear: " << newyear.getYear()
<< "\nmonth: " << newyear.getMonth()
<< "\nday: " << newyear.getDay()
<< "\nhour: " << newyear.getHour()
<< "\nmin: " << newyear.getMin()
<< "\nsec: " << newyear.getSec() << '\n';
JulianDate d1(2019, 1, 1, 0,0,0);
for (int i = 0; i < 100; i++)
cout << d1 + i;
}
下面是我根据老师上课的要求写的代码,但是很多bug,也有很多不足,求大神帮我改一下和完善下,然后和我解释下谢谢呢!!#include <iostream>
#include <time.h>
using namespace std;
class JulianDate {
private:
static int EPOCH;
int mon, day, year, hour, min, second;
// int mon, day, year, hour, min, second;
// this representation would make difference VERY UGLY
// diff between Jan. 1 2001 12:03:04 and Feb 26 2028 11:19:02
double jday; // number of days since epoch
const static int daysPerMonth[12];
const static int cumulativeDaysUpToMonth[12];
const static int monthNames[12];
public:
JulianDate(int year, int mon, int day, int hour, int min, int second) {
double dy = year - 2000;
double days = 365 * dy + dy / 4 - dy / 100 + dy / 400; //这里的闰年加减感觉错了,2000年好像是闰年,但是dy/4感觉少了一年、、但是老师叫我们这样算的。。
days += cumulativeDaysUpToMonth[mon - 1]; //怎么判断2个年份之间的闰年然后把多的天数加上呢?
days += day; //还要注意如果输入的那年刚好是闰年。月份大于3和小于3的区别
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
if (mon >= 3) {
days++;
} else {
days = days;
}
}
}
friend JulianDate operator+(JulianDate d, int addday){
return JulianDate(d.year,d.mon,d.day+addday,d.hour,d.min,d.second);
}
friend double operator-(JulianDate n, JulianDate v){
return (v.year-n.year,v.mon-n.mon,v.day-n.day,v.hour,v.min,v.second);
}
JulianDate( ) {
time_t now=time(nullptr);
}
double getYear(){
return year;
}
double getMonth(){
return mon;
}
double getDay(){
return day;
}
double getHour(){
return hour;
}
double getMin(){
return min;
}
double getSec(){
return second;
}
friend ostream& operator <<(ostream& s, JulianDate f){
return s<<"("<<f.year<<","<<JulianDate::monthNames[f.mon]<<","<<f.day<<","<<f.hour<<","<<f.min<<","<<f.second<<")"<<endl;
}
};
const int JulianDate::daysPerMonth[12] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int JulianDate::cumulativeDaysUpToMonth[12] =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 //老师说要用数组做,他说这样做很丑陋但是必须的。。
};
const int JulianDate::monthNames[12] =
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int JulianDate::EPOCH = 2000; // Jan.1 2000, 00:00:00 = 0
/*
how many days in a year? 365
leap year: if year MOD 4 == 0 LEAP EXCEPT
if year MOD 100 == 0 NOT LEAP YEAR Except
if year MOD 400 == 0 LEAP
Leap years:
NO 1900
YES 1904
YES 1908
YES 2000
NO 2100
YES 2400
days since EPOCH = 365 * (year - 2000) + years/4 - ....
2018 - 2000 = 18
seconds in a day = 24*60*60 = 86400
hh:mm:ss
00:00:00 0.0
12:00:00 0.5
*/
int main() {
JulianDate newyear(2018, 1, 1, 0,0,0);
JulianDate valentine(2018, 2, 14, 12,0,0); // 0.5
JulianDate today; // get it from the system time: time(nullptr)
// localtime
double days = valentine - newyear;
JulianDate due = today + 7;
cout << due << '\n';
cout << "\nyear: " << newyear.getYear()
<< "\nmonth: " << newyear.getMonth()
<< "\nday: " << newyear.getDay()
<< "\nhour: " << newyear.getHour()
<< "\nmin: " << newyear.getMin()
<< "\nsec: " << newyear.getSec() << '\n';
JulianDate d1(2019, 1, 1, 0,0,0);
for (int i = 0; i < 100; i++)
cout << d1 + i;
}
本帖最后由 superbe 于 2019-10-3 12:18 编辑 #include <iostream>
#include <time.h>
#include <iomanip>
using namespace std;
class JulianDate {
private:
static int EPOCH;
int mon, day, year, hour, min, second;
double jday; //number of days since epoch
const static int daysPerMonth[12];
const static int cumulativeDaysUpToMonth[12];
const static int monthNames[12];
void calJday();
public:
JulianDate(){}
JulianDate(int year, int mon, int day, int hour, int min, int second);
int getYear(){ return year; }
int getMonth(){ return mon; }
int getDay(){ return day; }
int getHour(){ return hour; }
int getMin(){ return min; }
int getSec(){ return second; }
double getJday(){ return jday; }
static bool isLeapYear(int year);
friend JulianDate operator+(JulianDate &date, int days); //重载+
friend double operator-(JulianDate &d1, JulianDate &d2); //重载-
friend ostream& operator<<(ostream &out, const JulianDate &date); //重载<<
};
int JulianDate::EPOCH = 2000; //Jan.1 2000, 00:00:00 = 0
const int JulianDate::daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int JulianDate::cumulativeDaysUpToMonth[12] = {0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
const int JulianDate::monthNames[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
/* 构造函数 */
JulianDate::JulianDate(int year, int mon, int day, int hour, int min, int second) {
this->year = year;
this->mon = mon;
this->day = day;
this->hour = hour;
this->min = min;
this->second = second;
calJday();
}
/* 计算jday的函数 */
void JulianDate::calJday(){
int dy = year - EPOCH; //与EPOCH相差的整年数
if(dy>0) dy-=1;
double days = 365 * dy + dy / 4 - dy / 100 + dy / 400; //计算整年的总天数,dy/4-dy/100+dy/400是算闰年多出来的天数
days += cumulativeDaysUpToMonth[mon - 1]; //补加上从1月开始整月的天数
days += day - 1; //补加上不满整月的天数
if(isLeapYear(year) && mon>2) days ++; //如果year是闰年并且超过2月份,补加上1天
days += (hour*3600 + min*60 + second)*1.0/86400;
jday = days;
}
/* 判断是否闰年的函数 */
bool JulianDate::isLeapYear(int year){
bool isLeap = (!(year%4) && year%100) || (year%400==0);
return isLeap;
}
/* 重载运算符+ */
JulianDate operator+(JulianDate &date, int days){
JulianDate dt = date; //调用了默认复制构造函数
for(int i=1; i<= days; i++){
dt.day ++; //增加1天
int daysInMon = dt.daysPerMonth[dt.mon-1]; //当月天数
if(dt.isLeapYear(dt.year) && dt.mon==2) daysInMon += 1; //闰年2月份加1天
if(dt.day > daysInMon){ //如果大于当月天数
dt.day = 1; //日变成下月1号
dt.mon ++; //月份加1
if(dt.mon > 12){ //如果月份超过12
dt.mon = 1; //月份变成下年的1月份
dt.year ++; //年份加1
}
}
}
dt.jday += days;
return dt;
}
/* 重载运算符- */
double operator-(JulianDate &d1, JulianDate &d2){
return d1.jday-d2.jday;
}
/* 重载运算符<< */
ostream& operator<<(ostream &os,const JulianDate &date){
os << date.year << "-"<< date.mon << "-" <<date.day << " "
<< date.hour << ":" << date.min << ":" << date.second << " "
<< fixed << setprecision(1) << date.jday;
return os;
}
int main() {
JulianDate newyear(2018, 1, 1, 0, 0, 0);
JulianDate valentine(2018, 2, 14, 12, 0, 0); //0.5
double days = valentine - newyear;
time_t now = time(nullptr);
tm *ltm = localtime(&now);
JulianDate today(ltm->tm_year+1900, ltm->tm_mon+1, ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
JulianDate due = today + 7;
cout << "newyear: " << newyear;
cout << "\nyear: " << newyear.getYear()
<< "\nmonth: " << newyear.getMonth()
<< "\nday: " << newyear.getDay()
<< "\nhour: " << newyear.getHour()
<< "\nmin: " << newyear.getMin()
<< "\nsec: " << newyear.getSec() << '\n';
cout << "valentine: " << valentine << endl;
cout << "days between newyear and valentine: " << days << endl;
cout << "now date: " << today <<endl;
cout << "7 days later: " << due <<endl;
system("pause");
JulianDate d1(2019, 1, 1, 0, 0, 0);
cout << "1--100 days since " << d1 << ":" <<endl;
for (int i = 1; i <= 100; i++){
cout << d1 + i << endl;
}
return 0;
}
上面计算jday的函数,针对EPOCH是闰年是正确的(2000是闰年),如果不是闰年要修改下。
建议换成下面这个比较通用的。是老师要求必须用上面这种方法吗。
/* 计算jday的函数 2 */
void JulianDate::calJday(){
double days = 0;
for(int i=EPOCH; i < year; i ++){ //计算整年的总天数
days += 365;
if(isLeapYear(i)) days ++; //如果是闰年补加上1天
}
days += cumulativeDaysUpToMonth[mon - 1]; //补加上从1月开始整月的天数
days += day - 1; //补加上不满整月的天数
if(isLeapYear(year) && mon > 2) days ++; //如果year是闰年并且超过2月份,补加上1天
days += (hour*3600 + min*60 + second)*1.0/86400;
jday = days;
}
|
|