当前位置:网站首页>C language brush question | temperature conversion (11)

C language brush question | temperature conversion (11)

2022-06-22 09:14:00 C language train

example 11: Someone used a thermometer to measure the temperature in Fahrenheit 98°F, Now ask for C The language implementation converts it to the temperature expressed in Celsius .

Their thinking : The algorithm for this problem is very simple , The key is to find the conversion formula between the two , Celsius equals five ninths times Fahrenheit minus 32 Product of , That is to say :

centigrade=(5.0/9)*(f_Degree-32);

Source code demo :

#include<stdio.h>// The header file 
 int main() // The main function 
 {
   float f_Degree,centigrade; // Define floating point variables 
   f_Degree=98.0;// Initialize the Fahrenheit variable 
   centigrade=(5.0/9)*(f_Degree-32);// Note that this should be 5.0
   printf(" Fahrenheit 98 The degree Celsius of is :%f\n",centigrade);// Output results 
   return 0;// The return value of the function is 0
 }

Compilation result :

 Fahrenheit 98 The degree Celsius of is :36.666668

--------------------------------
Process exited after 1.796 seconds with return value 0
 Please press any key to continue . . .

The reader should be able to easily understand this example , But there is one thing that requires the reader's special attention , This is the line of code

centigrade=(5.0/9)*(f_Degree-32);

If written

centigrade=(5/9)*(f_Degree-32);

Then there will be logical errors , The following output :

 Fahrenheit 98 The degree Celsius of is :0.000000

--------------------------------
Process exited after 1.967 seconds with return value 0
 Please press any key to continue . . .

Why ?

stay C No format is declared in the language , The divisor of this integer is larger than the divisor , Will be directly equal to 0, There is no number after the decimal point .

原网站

版权声明
本文为[C language train]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220909473342.html