21xrx.com
2024-06-03 03:46:41 Monday
登录
文章检索 我的文章 写文章
C++编写华氏温度转摄氏温度程序
2023-07-03 11:30:55 深夜i     --     --
C++语言 编程 华氏温度 摄氏温度 转换

C++是一种高效的编程语言,它具有广泛的应用领域。本文将介绍如何使用C++编写一个华氏温度转摄氏温度的程序。

首先,了解一下华氏温度和摄氏温度的转换公式。华氏温度转摄氏温度的公式为:C = 5 / 9 * (F - 32),其中F表示华氏温度,C表示摄氏温度。

接下来,让我们来编写C++代码,实现将华氏温度转换为摄氏温度的功能。

一、定义变量


#include <iostream>

using namespace std;

int main()

{

  double fahrenheit, celsius;

  cout << "Enter the temperature in Fahrenheit: ";

  cin >> fahrenheit;

此处我们定义了两个变量,分别是华氏温度(fahrenheit)和摄氏温度(celsius)。

二、运算


celsius = 5.0 / 9.0 * (fahrenheit - 32.0);

使用上文提到的公式进行运算,将华氏温度转换为摄氏温度。

三、输出结果


cout << "The temperature in Celsius is: " << celsius << endl;

return 0;

}

最后,输出结果,程序结束。

完整代码如下:


#include <iostream>

using namespace std;

int main()

{

  double fahrenheit, celsius;

  cout << "Enter the temperature in Fahrenheit: ";

  cin >> fahrenheit;

  celsius = 5.0 / 9.0 * (fahrenheit - 32.0);

  cout << "The temperature in Celsius is: " << celsius << endl;

  return 0;

}

现在,你已经了解了使用C++编写一个简单的华氏温度转摄氏温度的代码了。值得一提的是,以上代码只是一个基础的例子,C++还可以做更多更复杂的计算。欢迎大家继续深入学习,探索更多的C++编程技巧。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复