如何使用float和double和integer进行精确的温度转换?
How to use float and double and integer for accurate temperature conversion?
请帮忙。我是编程新手。我现在专注于功能。基本上函数 'sort of' 有效。然而,从摄氏度转换为华氏度的温度并不是 accurate.Any 解释为什么答案一开始是正确的,但错误越来越多,这将非常有帮助。谢谢。这是下面的代码:
#include <stdio.h>
#include <conio.h>
//function prototypes
float convertTofahrenheit(float z);
int main (void){
float x,y;
int count_c;
x=convertTofahrenheit(y);
printf ("%s\t%s","Celcius","Fahrenheit\n");
for (count_c=0;count_c<=30;count_c++){
x++;
printf ("%d\t%f\n", count_c, x);
}
getch();
return 0;
}
//function for conversion from Celcius to Fahrenheit
float convertTofahrenheit(float z){
float fahrenheit;
fahrenheit= ((z*1.8)+32);
return fahrenheit;
}
将转换放在循环中并传入 count_c 转换为浮点数:
float x; // don't need y
int count_c;
printf ("%s\t%s","Celcius","Fahrenheit\n");
for (count_c=0;count_c<=30;count_c++){
x=convertTofahrenheit((float)count_c);
printf ("%d\t%f\n", count_c, x);
}
事实上,您只在程序开始时调用一次 convertTofahrenheit 函数,而不是针对每个摄氏度值调用一次。
请帮忙。我是编程新手。我现在专注于功能。基本上函数 'sort of' 有效。然而,从摄氏度转换为华氏度的温度并不是 accurate.Any 解释为什么答案一开始是正确的,但错误越来越多,这将非常有帮助。谢谢。这是下面的代码:
#include <stdio.h>
#include <conio.h>
//function prototypes
float convertTofahrenheit(float z);
int main (void){
float x,y;
int count_c;
x=convertTofahrenheit(y);
printf ("%s\t%s","Celcius","Fahrenheit\n");
for (count_c=0;count_c<=30;count_c++){
x++;
printf ("%d\t%f\n", count_c, x);
}
getch();
return 0;
}
//function for conversion from Celcius to Fahrenheit
float convertTofahrenheit(float z){
float fahrenheit;
fahrenheit= ((z*1.8)+32);
return fahrenheit;
}
将转换放在循环中并传入 count_c 转换为浮点数:
float x; // don't need y
int count_c;
printf ("%s\t%s","Celcius","Fahrenheit\n");
for (count_c=0;count_c<=30;count_c++){
x=convertTofahrenheit((float)count_c);
printf ("%d\t%f\n", count_c, x);
}
事实上,您只在程序开始时调用一次 convertTofahrenheit 函数,而不是针对每个摄氏度值调用一次。