摄氏华氏温度转换c++代码
时间: 2023-10-24 11:34:51 浏览: 54
以下是将摄氏度转换为华氏度的C代码:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("请输入摄氏温度:");
scanf("%f", &celsius);
fahrenheit = (celsius * 1.8) + 32;
printf("%.2f 摄氏度 = %.2f 华氏度\n", celsius, fahrenheit);
return 0;
}
相关问题
摄氏华氏互相转换温度转换c++代码
以下是摄氏华氏互相转换温度的Python代码:
摄氏度转华氏度
```python
celsius = float(input("请输入摄氏度: "))
fahrenheit = (celsius * 1.8) + 32
print("华氏度为: ", fahrenheit)
```
华氏度转摄氏度
```python
fahrenheit = float(input("请输入华氏度: "))
celsius = (fahrenheit - 32) / 1.8
print("摄氏度为: ", celsius)
```
希望这能帮到您。
用c++编写函数,实现摄氏华氏温度转换
好的,这是一个简单的C++函数,可以实现摄氏温度和华氏温度之间的相互转换:
```c++
#include <iostream>
using namespace std;
double celsius_to_fahrenheit(double celsius) {
double fahrenheit = (9.0 / 5) * celsius + 32;
return fahrenheit;
}
double fahrenheit_to_celsius(double fahrenheit) {
double celsius = (5.0 / 9) * (fahrenheit - 32);
return celsius;
}
int main() {
double celsius, fahrenheit;
cout << "请输入摄氏温度:";
cin >> celsius;
fahrenheit = celsius_to_fahrenheit(celsius);
cout << "华氏温度:" << fahrenheit << endl;
cout << "请输入华氏温度:";
cin >> fahrenheit;
celsius = fahrenheit_to_celsius(fahrenheit);
cout << "摄氏温度:" << celsius << endl;
return 0;
}
```
该程序定义了两个函数,celsius_to_fahrenheit 和 fahrenheit_to_celsius,分别用于将摄氏温度转换为华氏温度,和将华氏温度转换为摄氏温度。
在主函数中,首先通过标准输入获取摄氏温度的值,然后调用 celsius_to_fahrenheit 函数将其转换为华氏温度,并将结果输出到控制台。接着,获取华氏温度的值,调用 fahrenheit_to_celsius 函数将其转换为摄氏温度,并将结果输出到控制台。
需要注意的是,在计算华氏温度时,乘数和加数都需要使用浮点数,否则将会出现精度问题。