c - 如何使用float、double和integer进行精确的温度转换?

标签 c floating-point integer double

请帮忙。我是编程新手。我现在专注于功能。基本上这个功能“有点”有效。然而,从摄氏转换为华氏温度的温度并不准确。任何关于为什么答案开始正常但越来越错误的解释都会非常有帮助。谢谢。下面是代码:

#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/

相关文章:

c - 二元炸弹第三阶段问题

c - 基于线程计算分配共享内存

c - 使用 mkstemp() 打开多个临时文件是否安全

c++ - 使用高斯消元法计算矩阵的逆时非常不准确

python - 从二维列表中删除重复项( float )

c - 为什么把变量的地址赋给指针是错误的?

c# - C# 中的数字数据类型比较

python - 如何在 Python 中检查字符串中是否包含数值?

mysql - 在表中存储状态标志值的最佳方式(char 或 int)

javascript - JavaScript 中的极大数字