我正在尝试将纬度和经度从度,分,秒转换为十进制度数。我已经正确地转换了数字但是我在转换符号时遇到了麻烦。当我为纬度输入数字39 57 21和经度输入-75 11 14时,它会正确转换为(39.9558,75.1872),但我的符号计算公式无法正常工作。我用细齿梳检查了代码,但似乎在配方中找不到错误。我给出的公式是:sign =(2x)/(abs(x)+1)。我在下面提供了我的代码。任何帮助将不胜感激。
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int user_degrees_latitude, user_minutes_latitude, user_seconds_latitude,
user_degrees_longitude, user_minutes_longitude, user_seconds_longitude,
sign_latitude, sign_longitude;
double total_minutes_latitude, total_degrees_latitude,
total_minutes_longitude, total_degrees_longitude, final_latitude,
final_longitude;
const double sixtieth = (1. / 60);
cout << "Input latitude in degrees, minutes, seconds:";
cin >> user_degrees_latitude >> user_minutes_latitude >>
user_seconds_latitude;
cout << "Input longitude in degrees, minutes, seconds:";
cin >> user_degrees_longitude >> user_minutes_longitude >>
user_seconds_longitude;
total_minutes_latitude =
(user_minutes_latitude + ((sixtieth) * user_seconds_latitude));
total_degrees_latitude =
(abs(user_degrees_latitude) + ((sixtieth) * total_minutes_latitude));
total_minutes_longitude =
(user_minutes_longitude + ((sixtieth) * user_seconds_longitude));
total_degrees_longitude =
(abs(user_degrees_longitude) + ((sixtieth) * total_minutes_longitude));
sign_latitude =
(2 * (total_degrees_latitude)) / ((abs(total_degrees_latitude)) + 1);
sign_longitude =
(2 * (total_degrees_longitude)) / ((abs(total_degrees_longitude)) + 1);
final_latitude = (sign_latitude * total_degrees_latitude);
final_longitude = (sign_longitude * total_degrees_longitude);
cout << user_degrees_latitude << " deg " << user_minutes_latitude << "\' "
<< user_seconds_latitude << "\" latitude, " << user_degrees_longitude
<< " deg " << user_minutes_longitude << "\' " << user_seconds_longitude
<< "\"";
cout << " is (" << total_degrees_latitude << "," << total_degrees_longitude
<< ")" << endl;
return 0;
}
答案 0 :(得分:0)
首先,尝试更好的计算符号的功能,例如:
double sign(double x) {return (x < 0) ? -1 : 1;}
其次,因为只有变量user_degrees_latitude
和user_degrees_longitude
才会变为负数,所以只要有大小,就应该只使用那些来计算最后的符号。
sign_latitude = sign(user_degrees_latitude);
sign_longitude = sign(user_degrees_longitude);