输入华氏温度,转化为摄氏温度后输出Cpp
时间: 2023-04-03 07:03:25 浏览: 73
我可以回答这个问题。以下是将华氏温度转换为摄氏温度的 C++ 代码:
#include <iostream>
using namespace std;
int main() {
double fahrenheit, celsius;
cout << "请输入华氏温度: ";
cin >> fahrenheit;
celsius = (fahrenheit - 32) * 5 / 9;
cout << "摄氏温度为: " << celsius << " 度" << endl;
return 0;
}
相关问题
用c++编程华氏温度转化摄氏温度
要用C++编程实现华氏温度转摄氏温度,可以按照以下步骤进行:
1. 首先,需要包含头文件iostream,以便使用输入输出流。
2. 创建一个主函数main(),作为程序的入口。
3. 在main()函数中,声明一个变量fahrenheit,用于存储输入的华氏温度值。
4. 使用std::cout输出提示信息,要求用户输入华氏温度值,并使用std::cin接收用户的输入,将其存储到fahrenheit变量中。
5. 声明一个变量celsius,用于存储转换后的摄氏温度值。
6. 使用转换公式 celsius = (fahrenheit - 32) * 5 / 9 将华氏温度转换为摄氏温度。
7. 使用std::cout输出转换后的摄氏温度值。
8. 返回0,表示程序正常结束。
下面是一个示例代码:
```cpp
#include <iostream>
int main() {
double fahrenheit;
std::cout << "请输入华氏温度值:";
std::cin >> fahrenheit;
double celsius = (fahrenheit - 32) * 5 / 9;
std::cout << "摄氏温度值为:" << celsius << std::endl;
return 0;
}
```
编写c++函数实现华氏温度转换为摄氏温度
下面是一个简单的 C++ 函数实现华氏温度转换为摄氏温度的代码:
```cpp
#include <iostream>
using namespace std;
double fahrenheitToCelsius(double fahrenheit)
{
double celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
int main()
{
double fahrenheit, celsius;
cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;
celsius = fahrenheitToCelsius(fahrenheit);
cout << "The temperature in Celsius is: " << celsius << endl;
return 0;
}
```
在这个例子中,我们定义了一个名为 `fahrenheitToCelsius` 的函数,它接受一个表示华氏温度的参数,并返回相应的摄氏温度。在 `main` 函数中,我们首先要求用户输入一个华氏温度,然后调用 `fahrenheitToCelsius` 函数将其转换为摄氏温度,并将结果打印到控制台上。