2014-03-04 107 views
0

當我試圖將華氏溫度轉換爲攝氏溫度時,我的C溫度轉換程序保持輸出0。從攝氏到華氏的轉換似乎很好。對於函數和部分我都做了完全相同的事情,但第二次轉換時我一直得到0。有人可以幫我或告訴我我做錯了什麼嗎?C溫度轉換程序保持輸出0華氏溫度到攝氏溫度

#include <stdio.h> 

//Function Declarations 

float get_Celsius (float* Celsius);  //Gets the Celsius value to be converted. 
void to_Fahrenheit (float cel);   //Converts the Celsius value to Fahrenheit and prints the new value. 
float get_Fahrenheit (float* Fahrenheit); //Gets the Fahrenheit value to be converted. 
void to_Celsius (float fah);    //Converts the Fahrenheit value to Celsius and prints the new value. 

int main (void) 
{ 
    //Local Declarations 
    float Fahrenheit; 
    float Celsius; 
    float a; 
    float b; 

    //Statements 
    printf("Please enter a temperature value in Celsius to be converted to Fahrenheit:\n"); 
    a = get_Celsius(&Celsius); 
    to_Fahrenheit(a); 
    printf("Please enter a temperature value in Fahrenheit to be converted to Celsius:\n"); 
    b = get_Fahrenheit(&Fahrenheit); 
    to_Celsius(b); 

    return 0; 
} //main 

float get_Celsius (float* Celsius) 
{ 
    //Statements 
    scanf("%f", &*Celsius); 
    return *Celsius; 
} 

void to_Fahrenheit (float cel) 
{ 
    //Local Declarations 
    float fah; 

    //Statements 
    fah = ((cel*9)/5) + 32; 
    printf("The temperature in Fahrenheit is: %f\n", fah); 
    return; 
} 

float get_Fahrenheit (float* Fahrenheit) 
{ 
    //Statements 
    scanf("%f", &*Fahrenheit); 
    return *Fahrenheit; 
} 

void to_Celsius (float fah) 
{ 
    //Local Declarations 
    float cel; 

    //Statements 
    cel = (fah-32) * (5/9); 
    printf("The temperature in Celsius is: %f\n", cel); 
    return; 
} 
+0

哇我甚至沒有看到這個問題,這是幾乎和我一樣。我很抱歉我是這個網站的新手。 – user3377510

+0

至少你知道下次。一般來說,像這樣的大多數初學者類型的問題至少被問過一次(在這個例子中是多次),在你發佈之前,你應該已經展示了很多可能相關的問題。 –

回答

5
cel = (fah-32) * (5/9); 

這裏,5/9是整數除法,其結果是0,將其更改爲5.0/9


而且在幾行,您使用的

scanf("%f", &*Celsius); 

&*是沒有必要,只需要scanf("%f", Celsius);就可以了O操作。

+0

_scanf(「%f」,Celsius); _我想這不會工作。至少你需要& –

+0

非常感謝你,我知道這是愚蠢的,但我無法弄清楚它是什麼。也許我應該乘以由5.0/9創建的小數。儘管如此,謝謝你的幫助。 – user3377510

+0

@Abhay'Celsius'在該函數中有'float *'類型。雖然不是很好的變量命名,因爲'main'中的同名'Celsius'具有'float'類型。 –

1
cel = (fah-32) * (5/9); 

5/9int/int,並給你在int所以這是0結果。

將其更改爲

cel = (fah-32) * (5.0/9.0); 

cel = (fah-32) * ((float)5/(float)9); 
相關問題