输入华氏温度F,计算输出对应的摄氏温度。这段代码用c++怎么写
时间: 2023-04-01 13:02:03 浏览: 71
您好,以下是计算华氏温度对应摄氏温度的 C 代码:
```c
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("请输入华氏温度:");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("对应的摄氏温度为:%.2f\n", celsius);
return ;
}
```
感谢您的提问!
相关问题
编写温度换算程序,输入摄氏温度C,输出对应华氏温度F c➕➕代码
以下是一个简单的 C++ 温度换算程序,可以将摄氏温度转换为华氏温度:
```cpp
#include <iostream>
using namespace std;
int main() {
double celsius, fahrenheit;
cout << "请输入摄氏温度:";
cin >> celsius;
fahrenheit = celsius * 1.8 + 32;
cout << "华氏温度为:" << fahrenheit << endl;
return 0;
}
```
在这个程序中,我们首先定义了两个变量 `celsius` 和 `fahrenheit`,分别用于存储输入的摄氏温度和计算得到的华氏温度。然后,我们使用 `cout` 和 `cin` 分别输出和输入温度值。最后,我们根据摄氏温度的公式将其转换为华氏温度,并输出结果。
希望这个程序能够帮到你!
华氏温度转换为摄氏温度计算c++代码
下面是一个使用内联函数实现华氏温度转换为摄氏温度的C++代码:
```
#include <iostream>
using namespace std;
inline double f2c(double f) {
return (f - 32) * 5 / 9;
}
int main() {
double f;
cout << "请输入华氏温度:";
cin >> f;
cout << "华氏温度为:" << f << ",对应的摄氏温度为:" << f2c(f) << endl;
return 0;
}
```
其中,`inline`关键字用于定义内联函数,可以提高函数调用的效率。函数`f2c`用于将华氏温度转换为摄氏温度,输入华氏温度后即可输出对应的摄氏温度。
另外,如果需要将摄氏温度转换为华氏温度,可以使用如下代码:
```
inline double c2f(double c) {
return c * 9 / 5 + 32;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)