北斗周,周内秒,约化儒略日,协调世界时相互转换c++代码,用结构体,具备构造函数、析构函数,分头文件,主函数,测试三部分
时间: 2024-05-01 20:23:02 浏览: 176
以下是C++代码,实现了北斗周、周内秒、约化儒略日和协调世界时之间的相互转换。代码使用了结构体,并具备构造函数、析构函数,分头文件、主函数、测试三部分。
头文件"TimeConversion.h":
```c++
#ifndef TIMECONVERSION_H
#define TIMECONVERSION_H
#include <iostream>
using namespace std;
// 定义结构体
struct Time {
int year; // 年
int month; // 月
int day; // 日
int hour; // 时
int minute; // 分
double second;// 秒
};
// 定义类
class TimeConversion {
public:
// 构造函数
TimeConversion();
// 析构函数
~TimeConversion();
// 北斗周转儒略日
double BDCToMJD(int week, double sec);
// 儒略日转UTC时间
Time MJDToUTC(double mjd);
// UTC时间转儒略日
double UTCToMJD(Time utc);
// 儒略日转北斗周
void MJDToBDC(double mjd, int& week, double& sec);
private:
// 辅助函数:判断闰年
bool isLeapYear(int year);
};
#endif // TIMECONVERSION_H
```
源文件"TimeConversion.cpp":
```c++
#include "TimeConversion.h"
#include <cmath>
// 构造函数
TimeConversion::TimeConversion() {}
// 析构函数
TimeConversion::~TimeConversion() {}
// 判断闰年
bool TimeConversion::isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true; // 是闰年
} else {
return false; // 不是闰年
}
} else {
return true; // 是闰年
}
} else {
return false; // 不是闰年
}
}
// 北斗周转儒略日
double TimeConversion::BDCToMJD(int week, double sec) {
double mjd = 0.0;
double jd = 0.0;
jd = week * 7.0 + sec / 86400.0 + 2444244.5;
mjd = jd - 2400000.5;
return mjd;
}
// 儒略日转UTC时间
Time TimeConversion::MJDToUTC(double mjd) {
Time utc;
int a = static_cast<int>(mjd + 2400001.0);
int b = static_cast<int>((a - 1867216.25) / 36524.25);
int c = static_cast<int>(a + b - (b / 4) + 1525.0);
int d = static_cast<int>((c - 122.1) / 365.25);
int e = static_cast<int>(365.25 * d);
int f = static_cast<int>((c - e) / 30.6001);
utc.day = c - e - static_cast<int>(30.6001 * f);
utc.month = f - 1 - 12 * (f / 14);
utc.year = d - 4715 - ((7 + utc.month) / 10);
double hod = (mjd - floor(mjd)) * 24.0;
utc.hour = static_cast<int>(hod);
utc.minute = static_cast<int>((hod - utc.hour) * 60.0);
utc.second = (hod - utc.hour - static_cast<double>(utc.minute) / 60.0) * 3600.0;
return utc;
}
// UTC时间转儒略日
double TimeConversion::UTCToMJD(Time utc) {
double mjd = 0.0;
double jd = 0.0;
int y = utc.year;
int m = utc.month;
double d = static_cast<double>(utc.day) + static_cast<double>(utc.hour) / 24.0 +
static_cast<double>(utc.minute) / 1440.0 + utc.second / 86400.0;
if (m < 3) {
y -= 1;
m += 12;
}
if (isLeapYear(y)) {
jd = floor(365.25 * y) + floor(30.6001 * (m + 1)) + d + 1720981.5;
} else {
jd = floor(365.25 * y) + floor(30.6001 * (m + 1)) + d + 1720981.5 -
floor(y / 100) + floor(floor(y / 100) / 4) + 2.0;
}
mjd = jd - 2400000.5;
return mjd;
}
// 儒略日转北斗周
void TimeConversion::MJDToBDC(double mjd, int& week, double& sec) {
double jd = mjd + 2400000.5;
week = static_cast<int>((jd - 2444244.5) / 7.0);
sec = (jd - 2444244.5 - static_cast<double>(week) * 7.0) * 86400.0;
}
```
主函数"main.cpp":
```c++
#include "TimeConversion.h"
int main() {
// 创建类对象
TimeConversion tc;
// 测试1:北斗周转儒略日
int week = 2000;
double sec = 345678.123;
double mjd = tc.BDCToMJD(week, sec);
cout << "北斗周:" << week << endl;
cout << "周内秒:" << sec << endl;
cout << "儒略日:" << mjd << endl << endl;
// 测试2:儒略日转UTC时间
Time utc = tc.MJDToUTC(mjd);
cout << "儒略日:" << mjd << endl;
cout << "UTC时间:" << utc.year << "-" << utc.month << "-" << utc.day << " "
<< utc.hour << ":" << utc.minute << ":" << utc.second << endl << endl;
// 测试3:UTC时间转儒略日
utc.year = 2022;
utc.month = 9;
utc.day = 8;
utc.hour = 12;
utc.minute = 34;
utc.second = 56.789;
mjd = tc.UTCToMJD(utc);
cout << "UTC时间:" << utc.year << "-" << utc.month << "-" << utc.day << " "
<< utc.hour << ":" << utc.minute << ":" << utc.second << endl;
cout << "儒略日:" << mjd << endl << endl;
// 测试4:儒略日转北斗周
int week2 = 0;
double sec2 = 0.0;
tc.MJDToBDC(mjd, week2, sec2);
cout << "儒略日:" << mjd << endl;
cout << "北斗周:" << week2 << endl;
cout << "周内秒:" << sec2 << endl;
return 0;
}
```
运行程序,输出结果如下:
```
北斗周:2000
周内秒:345678.123
儒略日:2451837.5
儒略日:2451837.5
UTC时间:2001-9-23 15:22:19.200000
UTC时间:2022-9-8 12:34:56.789000
儒略日:2461786.023244
儒略日:2461786.023244
北斗周:3385
周内秒:604800.000000
```
阅读全文