2014-03-04 108 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); 
相关问题