卫星导航中的时间标示转换

此程序用C++代码实现卫星导航中常用的计时方法之间的相互转化,主要包括的计时方法有:格里高利日期,儒略日,年+年积日+周内秒,GPS周+周内秒,BD周+周内秒,Galileo周+周内秒,GLONASS计时。

具体实现算法如下:

 

 

 

C++代码实现如下:

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;

double fun1_1(float Y, float M, float D, float h, float m, float s)//此函数为格里高利日转儒略日
{
	if (M <= 2)
	{
		Y = Y - 1;
		M = M + 12;
	}
	double JD = floor(365.25 * Y) + floor(30.6001 * (M + 1)) + D + 1720981.5 + h / 24 + m / 1440 + s / 86400;
	return JD;
}

float* fun1_2(double JD) //此函数为儒略日转格里高利日
{
	static float arr[6];
	float L1, L2, L3, Y1, M1, T1, J, N, T;
	J = floor(JD + 0.5);
	N = floor(4 * (J + 68569) / 146097);
	L1 = J + 68569 - floor((N * 146097 + 3) / 4);
	Y1 = floor(4000 * (L1 + 1) / 1461001);
	L2 = L1 - floor(1461 * Y1 / 4) + 31;
	M1 = floor(80 * L2 / 2447.0);
	arr[2] = L2 - floor(2447 * M1 / 80);
	L3 = floor(M1 / 11);
	arr[1] = M1 + 2 - 12 * L3;
	arr[0] = floor(100 * (N - 49) + Y1 + L3);
	T = fmod((JD + 0.5 - floor(JD)) * 24, 24);
	arr[3] = floor(T);
	T1 = (T - arr[3]) * 60;
	arr[4] = floor(T1);
	arr[5] = (T1 - arr[4]) * 60;

	return arr;
}

double fun2_1(float year, float day, float sec) //此函数为年+年积日+天内秒转儒略日
{
	double JD1 = fun1_1(year, 1, 1, 0, 0, 0);
	double JD2 = day + JD1 - 1;
	double JD = JD2 + sec / 86400.0;
	return JD;
}

float* fun2_2(double JD)  //此函数为儒略日转年+年积日+天内秒
{
	static float arr1[3];
	float* arr=fun1_2(JD);
	arr1[0] = arr[0];
	double JD1 = fun1_1(arr1[0], 1, 1, 0, 0, 0);
	arr1[1] = JD - JD1 + 1;
	arr1[2] = arr[3] * 3600 + arr[4] * 60 + arr[5];
	
	return arr1;
}

double fun3_1(float GPSweek, float GPSweeksec)  //此函数为GPS周+周内秒转儒略日
{
	double JD = 2444244.5 + GPSweek * 7 + GPSweeksec / 86400;
	return JD;
}
 
float* fun3_2(double JD) //此函数为儒略日转GPS周+周内秒
{
	static float arr2[2];
	arr2[0] = floor((JD - 2444244.5) / 7);
	arr2[1] = ((JD - 2444244.5) / 7 - arr2[0]) * 604800;
	return arr2;
}

double fun4_1(float BDweek, float BDweeksec)  //此函数为BD周+周内秒转儒略日
{
	double JD = 2453736.5 + BDweek * 7 + BDweeksec / 86400;
	return JD;
}

float* fun4_2(double JD)  //此函数为儒略日转BD周+周内秒
{
	static float arr4[2];
	arr4[0] = floor((JD - 2453736.5) / 7);
	arr4[1] = ((JD - 2453736.5) / 7 - arr4[0]) * 604800;
	return arr4;
}

double fun5_1(float Galileoweek, float Galileoweeksec) // 此函数为Galileo周+周内秒转儒略日
{
	double JD = 2451412.5 + Galileoweek * 7 + Galileoweeksec / 86400;
	return JD;
}

float* fun5_2(double JD)  //此函数为儒略日转Galileo周+周内秒
{
	static float arr5[2];
	arr5[0] = floor((JD - 2451412.5) / 7);
	arr5[1] = ((JD - 2451412.5) / 7 - arr5[0]) * 604800;
	return arr5;
}

double fun6_1(float N4, float NT, float h, float m, float s)  //此函数为GLONASS计时转儒略日
{

	double JDN = fun1_1(1996 + 4 * (N4 - 1), 1, 1, 0, 0, 0);
	double JD = NT - 1 + JDN + h / 24 + m / 1440 + s / 86400;
	return JD;
}

float* fun6_2(double JD)  //此函数为儒略日转GLONASS计时
{
	static float arr6[5];
	float* arr = fun1_2(JD);
	arr6[0] = floor((arr[0] - 1996) / 4) + 1;
	double JDN = fun1_1(1996 + 4 * (arr6[0] - 1), 1, 1, 0, 0, 0);
	arr6[1] = JD - JDN + 1;
	for (int i = 2; i < 5; i++)
	{
		arr6[i] = arr[i + 1];
	}
	return arr6;
}

int main()
{
	FLAG_ :
	//判断用户想要从的转换方式
	cout << "格里高利历与儒略日之间转换请按:       1" << endl;
	cout << "年+年积日+天内秒与儒略日之间转换请按: 2" << endl;
	cout << "儒略日与GPS周+周内秒之间转换请按:     3" << endl;
	cout << "儒略日与BD周+周内秒之间转换请按:      4" << endl;
	cout << "儒略日与Galileo周+周内秒之间转换请按: 5" << endl;
	cout << "儒略日与GLONASST之间转换请按:         6" << endl;
	cout << "退出:                                 7" << endl;
	int sle = 0, sle1 = 0;
	double JD;
	float Y, M, D, h, m, s, year, day, sec, week, weeksec, BDweek, BDweeksec, Galileoweek, Galileoweeksec,NT,N4;

	FLAG:                                                                                      
	cin >> sle;
	switch (sle)
	{
	case 1:
		cout << "格里高利历日期转换为儒略日请按:0" << endl;
		cout << "儒略日转换成格里高利日期请按:1" << endl;
	FLAG1:
		cin >> sle1;
		if (sle1 != 0 && sle1 != 1)
		{
			cout << "输入有误,请重新输入" << endl;
			goto FLAG1;
		}
		switch (sle1)
		{
		case 0:																						
			cout << "请输入格里高利历日期的 年:  月:  日:  时:  分:  秒:" << endl;
			cin >> Y >> M >> D >> h >> m >> s;
			JD = fun1_1(Y, M, D, h, m, s);
			cout << "转换的儒略日是: " << endl << fixed << setprecision(6) << JD << endl;
			break;

		case 1:														
			cout << "请输入儒略日" << endl;
			cin >> JD;
			float* arr = fun1_2(JD);
			cout << "转换成的格里高利历是: " << endl << (int)arr[0] << "年" << (int)arr[1] << "月" << (int)arr[2]
				<< "日" << (int)arr[3] << "时" << (int)arr[4] << "分" << (int)arr[5] << "秒" << endl;
			break;
		}
		break;
	case 2:
		cout << "年+年积日+天内秒转换为儒略日请按:0" << endl;
		cout << "儒略日转换为年+年积日+天内秒请按:1" << endl;
	FLAG2:
		cin >> sle1;
		if (sle1 != 0 && sle1 != 1)
		{
			cout << "输入有误,请重新输入" << endl;
			goto FLAG2;
		}
		switch (sle1)
		{                                                        
		case 0:
			cout << "输入当年的年+年积日+天内秒" << endl;
			cin >> year >> day >> sec;
			JD = fun2_1(year, day, sec);
			cout << "转换的儒略日是: " << endl << fixed << setprecision(6) << JD << endl;
			break;


		case 1:
			cout << "输入儒略日" << endl;								
			cin >> JD;
			float* arr1 = fun2_2(JD);
			cout << "年+年积日+天内秒是:" << endl << (int)arr1[0] << "年" << (int)arr1[1] << "日" << (int)arr1[2] << "秒" << endl;
			break;
		}
		break;

	case 3:
		cout << "GPS周+周内秒转为儒略日请按:0" << endl;
		cout << "儒略日转换到GPS周+周内秒请按:1" << endl;
	FLAG3:
		cin >> sle1;
		if (sle1 != 0 && sle1 != 1)
		{
			cout << "输入有误,请重新输入" << endl;
			goto FLAG3;
		}
		switch (sle1)
		{
		case 0:						                            
			cout << "请输入周+周内秒" << endl;
			cin >> week >> weeksec;
			JD = fun3_1(week, weeksec);
			cout << "转换成的儒略日是:" << endl << fixed << setprecision(6) << JD << endl;
			break;
		case 1:													
			cout << "请输入儒略日" << endl;
			cin >> JD;
			float* arr2 = fun3_2(JD);
			cout << "转换的周+周内秒是:" << endl << (int)arr2[0] << "周" << (int)arr2[1] << "秒";
			break;
		}
		break;
	case 4:														
		cout << "BD周+周内秒转为儒略日请按:0" << endl;
		cout << "儒略日转换到BD周+周内秒请按:1" << endl;
	FLAG4:
		cin >> sle1;
		if (sle1 != 0 && sle1 != 1)
		{
			cout << "输入有误,请重新输入" << endl;
			goto FLAG4;
		}
		switch (sle1)
		{
		case 0:													
			cout << "请输入BD周+周内秒" << endl;
			cin >> BDweek >> BDweeksec;
			JD = fun4_1(BDweek, BDweeksec);
			cout << "转换成的儒略日是:" << endl << fixed << setprecision(6) << JD << endl;
			break;
		case 1:													
			cout << "请输入儒略日" << endl;
			cin >> JD;
			float* arr4 = fun4_2(JD);
			cout << "转换的BD周+周内秒是:" << endl << (int)arr4[0] << "个BD周" << (int)arr4[1] << "秒";
			break;
		}
		break;
	case 5:
		cout << "Galileo周+周内秒转为儒略日请按:0" << endl;
		cout << "儒略日转换到Galileo周+周内秒请按:1" << endl;
	FLAG5:
		cin >> sle1;
		if (sle1 != 0 && sle1 != 1)
		{
			cout << "输入有误,请重新输入" << endl;
			goto FLAG5;
		}
		switch (sle1)
		{
		case 0:													
			cout << "请输入Galileo周+周内秒" << endl;
			cin >> Galileoweek >> Galileoweeksec;
			JD = fun5_1(Galileoweek, Galileoweeksec);
			cout << "转换成的儒略日是:" << endl << fixed << setprecision(6) << JD << endl;
			break;

		case 1:													
			cout << "请输入儒略日" << endl;
			cin >> JD;
			float* arr5 = fun5_2(JD);
			cout << "转换的Galileo周+周内秒是:" << endl << (int)arr5[0] << "个Galileo周" << (int)arr5[1] << "秒";
			break;;
		}
		break;
	case 6:
		cout << "GLONASST计时转换到儒略日请按:0" << endl;
		cout << "儒略日转换到GLONASST计时请按:1" << endl;
	FLAG6:
		cin >> sle1;
		if (sle1 != 0 && sle1 != 1)
		{
			cout << "输入有误,请重新输入" << endl;
			goto FLAG6;
		}
		switch (sle1)
		{
		case 0:													
			cout << "请输入GLONASST的  实际间隔数:  日历序号:  时:  分:  秒:" << endl;
			cin >> N4 >> NT >> h >> m >> s;
			JD = fun6_1(N4, NT, h, m, s);
			cout << "转换的儒略日为:" << endl << fixed << setprecision(6) << JD << endl;
			break;
		case 1:													
			cout << "请输入儒略日" << endl;
			cin >> JD;
			float* arr6 = fun6_2(JD);
			cout << "转换成GLONASST计时为:\n" << (int)arr6[0] << "个实际间隔数" << "第" << (int)arr6[1] << "天" 
				<< (int)arr6[2] << "时" << (int)arr6[3] << "分" << (int)arr6[4] << "秒" << endl;
			break;

		}
		break;
	case 7:
		break;
	default:
		cout << "输入有误,请重新输入" << endl;
		goto FLAG;
	}
	cout << "是否继续转换\n1、是\n0、否" << endl;
	int last = 0;
	cin >> last;
	if (last == 1)
	{
		goto FLAG_;
	}
		
		system("pause");

		return 0;

}

 此代码中后面儒略日和GLONASS计时转换需要用到格里高利日期中的数据,所以通过使用指针数组分别获取格里高利日期中的年月日时分秒,到时候用到其中的数据时,只需调用函数,然后通过数组引用其中的指即可,另外,为了能够连续进行时间转化,引用了很多的goto语句使程序读起来比较麻烦,大家也可以将程序中的goto语句通过if语句实现一样的功能,这样不仅程序跟简洁,读起来不费力,最后,初次发博客,希望广大朋友批评指正。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值