C温度转换程序保持输出0华氏温度至摄氏温度

时间:2014-03-04 04:30:21

标签: c output stdio temperature

当我尝试将华氏温度转换为摄氏温度时,C中的温度转换程序会保持输出0。从摄氏温度到华氏温度的转换似乎工作正常。对于函数和部分我都做了完全相同的事情,但是第二次转换我一直得到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;
}

2 个答案:

答案 0 :(得分:5)

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

此处5/9为整数除法,结果为0,将其更改为5.0/9


在几行中,您正在使用

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

&*不是必需的,只需scanf("%f", Celsius);即可。

答案 1 :(得分: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);