簡體   English   中英

C程序未打印值

[英]C program not printing values

我正在編寫一個將十進制度轉換為度,分,秒的程序。 這是應該做的:

-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;
}

到目前為止,這對我來說還不錯。 也許您應該嘗試使用其他編譯器進行嘗試,如果您使用的是基於Unix的系統,則可以嘗試使用GCC(如果尚未使用的話),或者嘗試使用Xcode(如果使用的是Mac),並且如果運行Windows MinGW是一種流行的選擇(適用於Windows的GCC解決方案)。

考慮到其他人已經說過的話,您可以在編寫代碼時稍加改進。 正如Paul所建議的,此處不必在printf中使用%lf ,但是如果您打算以后供用戶輸入自己的值,則可以使用%lf通過scanf()讀取雙精度數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM