C程序不打印值

时间:2014-09-22 00:28:09

标签: c double degrees

我正在编写一个将十进制度转换为度,分,秒的程序。 这就是它应该做的:

-118.1406209154 = 118 degrees 8 minutes 26.2342 seconds West.
W     If it is negative then it is either west longitude or south latitude
118     The degrees are left of the decimal place
8     Multiply the decimal portion (0.140620915) by 60 to get decimal minutes (8.4372549). The minutes are left of the decimal place.
26.2353     Multiply the decimal portion (0.4372549) by 60 to get decimal seconds.

这是我到目前为止的代码,然而,当我运行它时的问题是它只打印decimalDegrees值而没有别的。我无法弄清楚为什么。非常感谢任何提示/帮助。

double decimalDegrees = -118.1406209154;

double degrees;
double minutes;
double seconds;
char longitude;
char latitude;
double temp;

int main(int argc, char **argv)
{
    minutes = decimalDegrees - (int)decimalDegrees; //get the decimal portion of decimalDegrees
    degrees = decimalDegrees - minutes; //get degrees by removing the decimal portion
    minutes = minutes*60.0;
    temp = minutes - (int)minutes; //get decimal portion
    minutes = minutes - temp; //remove decimal portion from minutes
    seconds = temp * 60.0;

    printf("%0.10lf ", decimalDegrees);
    printf("%lf degrees ", degrees);
    printf("%lf minutes ", minutes);
    printf("%0.4lf seconds ", seconds);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

到目前为止,这对我很有用。也许您应该尝试使用不同的编译器,如果您使用的是基于unix的系统,如果您还没有使用GCC,可以尝试使用GCC,如果您使用的是Mac,则可以尝试使用Xcode,如果运行Windows MinGW,则可以尝试使用GCC。流行的选择(Windows的GCC解决方案)。

考虑到其他人已经说过的话,你可以在你做的时候稍微改善你的代码。正如Paul建议的那样,在这里使用%lf是不必要的,但是如果你打算让用户在以后输入自己的值,你将使用%lf来读取双打{ {1}}。