写一个华氏度和摄氏度相互转换的程序
时间: 2023-03-25 11:01:48 浏览: 67
您好,我可以回答这个问题。以下是一个华氏度和摄氏度相互转换的程序:
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("请输入华氏度: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("%.2f 华氏度 = %.2f 摄氏度\n", fahrenheit, celsius);
printf("请输入摄氏度: ");
scanf("%f", &celsius);
fahrenheit = celsius * 9 / 5 + 32;
printf("%.2f 摄氏度 = %.2f 华氏度\n", celsius, fahrenheit);
return ;
}
希望对您有帮助。
相关问题
c++用if语句实现华氏度和摄氏度相互转换
可以使用以下的C++代码实现华氏度和摄氏度相互转换:
```c++
#include <iostream>
using namespace std;
int main()
{
char type; // 温度类型
double temp, result; // 温度值和结果
cout << "请输入温度类型(F或C):";
cin >> type;
cout << "请输入温度值:";
cin >> temp;
if (type == 'F' || type == 'f') // 华氏度转摄氏度
{
result = (temp - 32) * 5 / 9;
cout << "转换后的温度为:" << result << "C" << endl;
}
else if (type == 'C' || type == 'c') // 摄氏度转华氏度
{
result = temp * 9 / 5 + 32;
cout << "转换后的温度为:" << result << "F" << endl;
}
else // 输入错误
{
cout << "输入错误,请输入F或C!" << endl;
}
return 0;
}
```
这个程序会根据用户输入的温度类型进行华氏度和摄氏度的转换,并输出转换后的结果。
用python写华氏温度和摄氏温度相互换算的程序
当然可以,下面是一个简单的代码示例,可以实现华氏温度和摄氏温度之间的相互转换:
```python
# 将华氏温度转换为摄氏温度
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5 / 9
return celsius
# 将摄氏温度转换为华氏温度
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9 / 5 + 32
return fahrenheit
# 测试代码
print("32华氏度 = %.2f摄氏度" % fahrenheit_to_celsius(32))
print("0摄氏度 = %.2f华氏度" % celsius_to_fahrenheit(0))
```
运行结果为:
```
32华氏度 = 0.00摄氏度
0摄氏度 = 32.00华氏度
```
以上代码中,fahrenheit_to_celsius()函数用于将华氏温度转换为摄氏温度,celsius_to_fahrenheit()函数用于将摄氏温度转换为华氏温度。你可以根据需要修改输入和输出的格式。