如何通过函数编写温度换算表?

温度转换只不过是将华氏温度转换为摄氏温度或将摄氏温度转换为华氏温度。

在此编程中,我们将说明如何将华氏温度转换为摄氏温度,以及如何使用函数以表的形式表示华氏温度。

示例

以下是温度转换的C程序-

#include<stdio.h>
float conversion(float);
int main(){
   float fh,cl;
   int begin=0,stop=300;
   printf("Fahrenheit \t Celsius\n");// 显示转换表标题
   printf("----------\t-----------\n");
   fh=begin;
   while(fh<=stop){
      cl=conversion(fh); //调用功能
      printf("%3.0f\t\t%6.lf\n",fh,cl);
      fh=fh+20;
   }
   return 0;
}
float conversion(float fh) //称为函数{
   float cl;
   cl= (fh - 32) * 5 / 9;
   return cl;
}
输出结果

执行以上程序后,将产生以下结果-

Fahrenheit Celsius
---------- -----------
   0       -18
   20       -7
   40       4
   60       16
   80       27
  100       38
  120       49
  140       60
  160       71
  180       82
  200       93
  220       104
  240       116
  260       127
  280       138
  300       149

以类似的方式,您可以编写将摄氏温度转换为华氏温度的程序

只需将方程式更改为

华氏度=(摄氏* 9/5)+ 32。