C++ 经纬度转化为度分秒格式的函数
时间: 2023-11-05 19:16:10 浏览: 180
经纬度转换(度《---》度分秒)
以下是将经度和纬度分别转换为度分秒格式的函数:
```
// 经度转换为度分秒格式
void convert_longitude(double longitude, int &Degrees, int &Minutes, int &Seconds) {
Degrees = int(longitude);
double fractional_part = longitude - Degrees;
Minutes = int(fractional_part * 60);
Seconds = int((fractional_part * 60 - Minutes) * 60);
}
// 维度转换为度分秒格式
void convert_latitude(double latitude, int &Degrees, int &Minutes, int &Seconds) {
Degrees = int(latitude);
double fractional_part = latitude - Degrees;
Minutes = int(fractional_part * 60);
Seconds = int((fractional_part * 60 - Minutes) * 60);
}
```
使用方法如下:
```
int main() {
double longitude = 116.405285; // 116.405285°E
double latitude = 39.904989; // 39.904989°N
int longitude_degrees, longitude_minutes, longitude_seconds;
convert_longitude(longitude, longitude_degrees, longitude_minutes, longitude_seconds);
int latitude_degrees, latitude_minutes, latitude_seconds;
convert_latitude(latitude, latitude_degrees, latitude_minutes, latitude_seconds);
cout << "Longitude: " << longitude_degrees << "°" << longitude_minutes << "'" << longitude_seconds << "\"" << endl;
cout << "Latitude: " << latitude_degrees << "°" << latitude_minutes << "'" << latitude_seconds << "\"" << endl;
return 0;
}
```
输出结果为:
```
Longitude: 116°24'19"
Latitude: 39°54'18"
```
阅读全文