2012-10-11 51 views
-1

可能重複:
Why can’t decimal numbers be represented exactly in binary?在C語言中使用mod輸出分鐘需要幫助?

#include <stdio.h> 

int main(void) 
{ 
    int temperature, time; 
    float minutes; 

    printf("What is the Fahrenheit temperature?\n"); 
    scanf("%d",&temperature); 

    time = (5 - (temperature - 90)/5); 
    minutes = ((5 % time) * 60); 


    if (temperature >= 90) 
     printf("It will rain at approximately %d hours and %f minutes after noon.\n", time, minutes); 

    else 
     printf("It will not rain today.\n"); 


    system("PAUSE"); 
    return 0; 

} 

我現在用的是C語言編程。這是家庭作業,但只要它不是整個代碼就可以使用網絡上的一些位。不管怎麼說,如果當前的溫度是93F time = 4.4,所以我想輸出:

It will rain approximately 4 hours and 24 minutes after noon但產量反而是:

It will rain at approximately 5 hours and 0.000000 minutes after noon

編輯: 我得到一個錯誤,現在規定的無效操作數爲二進制%(有int和float)請耐心等待我LOL我正在努力學習。

#include <stdio.h> 

int main(void) 
    { 
    int current_temp; 
    float remaining_minutes, time; 

    printf("What is the temperature in Fahrenheit?\n"); 
    scanf("%d",&current_temp); 

    time = (5 - (current_temp - 90)/5); 
    ERROR**** remaining_minutes = ((5 % time) * 60); 


    if (current_temp >= 90) 
     printf("It will rain at approximately %d hours and %f minutes after noon.\n", time); 

    else 
     printf("It will not rain today.\n"); 


    system("PAUSE"); 
    return 0; 



} 
+0

** ** INT溫度,時間。 – m0skit0

回答

0

您還需要將您的時間定義爲float。現在你用整數分開整數,所以你只能得到整數。

+0

我試過。我添加了新的編輯過的代碼,但它不起作用。你能解釋更多嗎?我真的很感激。 –

0

mins =(時間%1)* 60?

編輯:或分鐘=(時間* 10%10)* 6.在我的手機上,所以不能真正測試。

+0

它沒有工作。我添加了編輯的代碼。 –

0

timeint。你不能指望它保存一個浮點數。

+0

頂部我把它設置爲int,你能告訴我更多,我是一個noob。 –

0

時間=(5-(溫度-90)/ 5);

時間,5,90是整數所以時間= 5 - 0 ...至極爲5

特里結構使用浮動時間和

time = (5.0 - (float)(temperature - 90)/5);