请帮忙。我是编程新手。我现在专注于功能。基本上这个功能“有点”有效。然而,从摄氏转换为华氏温度的温度并不准确。任何关于为什么答案开始正常但越来越错误的解释都会非常有帮助。谢谢。下面是代码:
#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;
}
最佳答案
将转换放入循环中并传入转换为 float 的 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 函数,而不是针对每个摄氏值调用一次。
关于c - 如何使用float、double和integer进行精确的温度转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29786770/